swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

undefined method build_from_hash in Enum data type

Open ydgiannp opened this issue 8 years ago • 12 comments

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

screen shot 2017-02-24 at 9 09 23 pm

this is the AccountCategory class that has Enum data type

screen shot 2017-02-24 at 9 09 39 pm

can somebody help me?

ydgiannp avatar Feb 24 '17 14:02 ydgiannp

@ydgiannp do you mind sharing the spec (e.g. via gist.github.com) so that we can more easily reproduce the issue?

wing328 avatar Feb 26 '17 09:02 wing328

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

ydgiannp avatar Mar 02 '17 02:03 ydgiannp

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!

prsnca avatar Mar 14 '17 15:03 prsnca

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"
    }
  }
},
...

prsnca avatar Mar 14 '17 16:03 prsnca

@ydgiannp @prsnca if you guys have time to contribute the fix, let me know and I can show you some good starting points.

wing328 avatar Mar 14 '17 16:03 wing328

@prsnca thanks for sharing the workaround.

wing328 avatar Mar 14 '17 16:03 wing328

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.

prsnca avatar Mar 14 '17 16:03 prsnca

How do you think it should be fixed?

  1. Add build_from_hash method to enum class. It should contain validation logic, return enum or raise Exception if value doesn't exist.
  2. Modify convert_to_type to check if class is enum end move validation logic there.
  3. 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?

sixers avatar Mar 29 '17 12:03 sixers

My apologies for late reply on this. I would vote for (1) adding build_from_hash

wing328 avatar Jul 13 '17 07:07 wing328

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 avatar Jul 20 '17 08:07 NicoEigenmannCW

@NicoEigenmannCW do you mind submitting a PR with the suggested fix (so that the credit goes back to you)?

wing328 avatar Jul 20 '17 15:07 wing328

This issue can perhaps be closed. /cc @wing328

isimluk avatar Oct 25 '23 09:10 isimluk