Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] PHP generator produce a invalid php file when shema have an enum with default value. #10244

Closed
etremblay opened this issue Aug 24, 2021 · 2 comments · Fixed by #10273
Closed

Comments

@etremblay
Copy link
Contributor

Description

PHP generator produce a invalid php file when shema have an enum with default value.

PHP Warning:  Use of undefined constant TYPE_MOVE - assumed 'TYPE_MOVE' (this will throw an Error in a future version of PHP)

Tested with php 7.4

openapi-generator version

5.1.1, 5.2.1, master

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: 'Enum Bug'
  version: latest
paths:
  '/':
    get:
      operationId: demoBug
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/MoveMovement'
components:
  schemas:
    MoveMovement:
      required:
        - type
      properties:
        type:
          type: string
          default: MOVE
          enum:
            - MOVE
Generation Details
docker run --rm  -v ${PWD}:/local --user "${USER_ID}:${GROUP_ID}" openapitools/openapi-generator-cli:latest generate -i /local/model.yaml -g php -o /local/php
Steps to reproduce

Generate php model with an enum property with default value

The model constructor will have invalid php code

    const TYPE_MOVE = 'MOVE';
    
    ...

    public function __construct(array $data = null)
    {
        $this->container['type'] = $data['type'] ?? TYPE_MOVE;
    }

should be

    $this->container['type'] = $data['type'] ?? self::TYPE_MOVE;
Suggest a fix

https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPhpCodegen.java

    @Override
    public String toEnumDefaultValue(String value, String datatype) {
        return "self::" + datatype + "_" + value;
    }
@wing328
Copy link
Member

wing328 commented Aug 26, 2021

Can you please file a PR with the suggested fix?

etremblay pushed a commit to kronostechnologies/openapi-generator that referenced this issue Aug 26, 2021
@etremblay
Copy link
Contributor Author

@wing328 Now I did #10273

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants