swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.01k stars 6.03k forks source link

[PHP] Can't deserialize enums #4032

Open theindexer opened 8 years ago

theindexer commented 8 years ago
Description

--Enum here refers to an enum declared in swagger, which turns into a php class.--

My generated php client api can't get an object that includes an enum property. I haven't checked serialization. Didn't find any related issued when I looked.

Swagger-codegen version

2.2.1

Swagger

Minimal json of the enclosing object (which is of course enclosed in a response)

"MyObj": {
  "type": "object",
  "properties" : {
    "MyEnum": {
      "type": "string",
      "enum": [
        "VALUE1",
        "VALUE2",
        "VALUE3"
      ]
    }
  }
}
Command line used for generation

bash, some variables omitted

java -jar $SWAGGERJAR generate -i $INPUT -l php -o $PHPDIR \
      --invoker-package My\\Client \
      --api-package My\\Client\\Api \
      --model-package My\\Client\\Model \
      --additional-properties packagePath=MyClient \
      || exit 1
Steps to reproduce

1) Run codegen, make sure you've got an enum in there 2) Make a get request whose response includes the object 3) Be unhappy 3) Hope that you're making a silly mistake

Suggest a Fix

ObjectSerializer in php looks like it assumes that all classes have a swaggerType method, but model_enum.mustache don't got one. This causes unhapiness.

wing328 commented 8 years ago

@theindexer thanks for reporting the issue. You're right that the issue was caused by top-level enum class missing swaggerType.

May I know if you have time to contribute the fix?

theindexer commented 8 years ago

Not at the moment, unfortunately :/

theindexer commented 8 years ago

I also ran into this issue with Ruby.

A quick hack fix when all your enums are strings (which I imagine is the usual case) is as follows, basically just have a way to differentiate them from classes when deserializing.

https://gist.github.com/theindexer/5b96fadfb3dfae1bfe1b521752592a64

Kinda silly though.

jasco commented 7 years ago

Another work-around, also for the case of string values, was to update the swaggerTypes in the models that reference the enum to declare the field type as string rather than as the enum class.