swagger-codegen
swagger-codegen copied to clipboard
undefined method build_from_hash in Enum data type
Hi, Im having an issue. I am generating a json swagger into a ruby gem, the swagger JSOn contains Enum data type. The generated class that has an Enum data type does not have build_from_hash method so it always get NoMethodError. the convert_to_type method always call build_from_hash method
this is the convert_to_type method
this is the AccountCategory class that has Enum data type
can somebody help me?
@ydgiannp do you mind sharing the spec (e.g. via gist.github.com) so that we can more easily reproduce the issue?
here's the spec :) https://gist.github.com/ydgiannp/fd5178bd88b9da10bea12a14ae00ca4a . My swagger has 3 keys that has Enum attribute and all of these don't have build_from_hash method. So I think those models must respond to build_from_hash
i have encountered the same issue - my current solution is in the swagger.json - replace the $ref which refers to the enum object by the definition of the $ref itself - regenerate your ruby gem and it should work!
Here is the workaround -
swagger.json before:
...
"enumObject": {
"type": "string",
"enum": [
"VALUE1",
"VALUE2",
"VALUE3",
"VALUE4"
],
"default": "VALUE1"
},
"someObjectUsingEnum": {
"type": "object",
"properties": {
"myEnum": {
"$ref": "#/definitions/enumObject"
},
"myOtherProperty": {
"type": "string"
}
}
}
...
swagger.json after:
...
"enumObject": {
"type": "string",
"enum": [
"VALUE1",
"VALUE2",
"VALUE3",
"VALUE4"
],
"default": "VALUE1"
},
"someObjectUsingEnum": {
"type": "object",
"properties": {
"myEnum": {
"type": "string",
"enum": [
"VALUE1",
"VALUE2",
"VALUE3",
"VALUE4"
],
"default": "VALUE1"
},
"myOtherProperty": {
"type": "string"
}
}
},
...
@ydgiannp @prsnca if you guys have time to contribute the fix, let me know and I can show you some good starting points.
@prsnca thanks for sharing the workaround.
i've researched it today a bit on all levels (from the protobuf to the ruby gem :) ) and it i think that at some point the $ref should be looked into and tagged as an enum... what do you think @wing328? i did not dig in the code to where the enum tagging is made.
How do you think it should be fixed?
- Add
build_from_hash
method to enum class. It should contain validation logic, return enum or raise Exception if value doesn't exist. - Modify
convert_to_type
to check if class is enum end move validation logic there. - A mix
I think the first option is better, because enum validation should be defined together with enum. On the other hand, build_from_hash
is not the best name, because we return enum from string. So maybe some similar method, but named value_of
, and some extra code in convert_to_type
to differentiate between enums and regular models?
My apologies for late reply on this. I would vote for (1) adding build_from_hash
Hi I stumbeld across this issue, while working on a project.
I'm currently using this code
def build_from_hash(value)
consantValues = {{classname}}.constants.select{|c| c.to_s == value}
raise "Invalid ENUM value #{value} for class #{{{classname}}}" if consantValues.empty?
value
end
Perhaps you can expand on this.
@NicoEigenmannCW do you mind submitting a PR with the suggested fix (so that the credit goes back to you)?
This issue can perhaps be closed. /cc @wing328