Issue with boolean cast to float64
damwiw opened this issue · comments
Hi,
Thanks for your big work to convert OVH api files to OpenAPI format.
I had a small issue when using it, coming from boolean values of readOnly and canBeNull fields.
~/go/bin/ovhapi2openapi -i Examples/OVHAPI-EU.yaml -o openapi/ovh-eu.yaml
--- Load the API informations from the file: Examples/OVHAPI-EU.yaml
done
--- Create the Open API struct
done
--- Get the routes from API: https://eu.api.ovh.com/1.0
done
--- Get the definitions from API: https://eu.api.ovh.com/1.0/auth.json
panic: interface conversion: interface {} is bool, not float64goroutine 1 [running]:
main.stringFromIntOrNil(0x688b40, 0x84d5c1, 0x6f552b, 0x8)
/home/vagrant/go/src/ovhapi2openapi/OVHAPI.go:259 +0x105
main.newOVHAPITypeObjectProperties(0xc420259650, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
/home/vagrant/go/src/ovhapi2openapi/OVHAPI.go:239 +0x1e6
main.newOVHAPITypeObject(0xc4202595f0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
/home/vagrant/go/src/ovhapi2openapi/OVHAPI.go:227 +0x39c
main.(*OVHAPI).GetDefinition(0xc420055f08, 0xc4202b2c70, 0xa, 0x0, 0x0, 0x0)
/home/vagrant/go/src/ovhapi2openapi/OVHAPI.go:186 +0x455
main.main()
/home/vagrant/go/src/ovhapi2openapi/main.go:110 +0x5fd
Issue was for readOnly and canBeNull fields. Don't know if it's coming from a change in OVH files or from go behavior. Anyway, here is my fix proposal:
diff --git a/OVHAPI.go b/OVHAPI.go
index 2ebd237..75dc66c 100644
--- a/OVHAPI.go
+++ b/OVHAPI.go
@@ -236,8 +236,8 @@ func newOVHAPITypeObjectProperties(parameter map[string]interface{}) OVHAPITypeO
properties.FullType = stringFromStringOrNil(parameter["fullType"])
properties.Type = stringFromStringOrNil(parameter["type"])
properties.Description = stringFromStringOrNil(parameter["description"])
- properties.ReadOnly = stringFromIntOrNil(parameter["readOnly"])
- properties.CanBeNull = stringFromIntOrNil(parameter["canBeNull"])
+ properties.ReadOnly = stringFromBoolOrNil(parameter["readOnly"])
+ properties.CanBeNull = stringFromBoolOrNil(parameter["canBeNull"])
return properties
}
@@ -251,6 +251,19 @@ func stringFromStringOrNil(value interface{}) string {
return value.(string)
}
+func stringFromBoolOrNil(value interface{}) string {
+ var valfloat float64
+ if value == nil {
+ return ""
+ } else if value == true {
+ valfloat = 1
+ } else {
+ valfloat = 0
+ }
+
+ return fmt.Sprintf("%.0f", valfloat)
+}
+
func stringFromIntOrNil(value interface{}) string {
if value == nil {
return ""
Seems to work. I've checked boolean values on original and converted files; it was ok.
Let you decide if you want to patch your project.
Have a nice day.
Damien
Hi,
looking good for me, please open a pull request.