OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.75k stars 6.56k forks source link

PHP Client does not handle default values correctly when they are ENUM references #6809

Open soundstripe opened 4 years ago

soundstripe commented 4 years ago

The default values set in the constructor for PHP Model classes does not correctly reference self::.

I believe the offending code is this line: https://github.com/OpenAPITools/openapi-generator/blob/d9a6f4d7261d523d933e4cd33e6abc48d4d0b5e2/modules/openapi-generator/src/main/resources/php/model_generic.mustache#L164

example generated code:

        $this->container['code'] = isset($data['code']) ? $data['code'] : CODE_200;

expected:

        $this->container['code'] = isset($data['code']) ? $data['code'] : self::CODE_200;
soundstripe commented 4 years ago

Looks like this applies only to numeric values. The following change to line 164 seems to fix it for me:

        $this->container['{{name}}'] = isset($data['{{name}}']) ? $data['{{name}}'] : {{#defaultValue}}{{#isEnum}}{{#isNumeric}}self::{{/isNumeric}}{{/isEnum}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}};
soundstripe commented 4 years ago

The fact that string default values are not referencing the defined enums (instead being passed through the template as literals) was disturbing so I traced that further.... may be a problem here:

https://github.com/OpenAPITools/openapi-generator/blob/6a61910df15447356a8b223fa465e37781c3fdac/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java#L5296

wing328 commented 4 years ago

Agreed https://github.com/OpenAPITools/openapi-generator/blob/6a61910df15447356a8b223fa465e37781c3fdac/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java#L5296 may be a problem. Thanks for the trace.

Looks like this applies only to numeric values. The following change to line 164 seems to fix it for me:

Can you please file a PR with the suggested fix?