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

[BUG][PHP] ENUM is not rendered correctly as default value #9038

Open sleipi opened 3 years ago

sleipi commented 3 years ago

Bug Report Checklist

Description

When using a property of type string with ENUM Values, and a default value the generated code will produce a incorrect usage of the default value - caused by a missing self Keyword

openapi-generator version

latest (docker image)

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: Buggy Model generation
  description: This demonstrate a Bug when using Default Values from ENUM Property
  version: 1.0.0
servers:
  - url: 'https://api.example.com'
paths:
  /bug:
    get:
      summary: A Bug.
      responses:
        '200':
          $ref: '#/components/responses/BuggyResponse'

components:

  responses:
    BuggyResponse:
      description: OK
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/Notification"

  schemas:
    Notification:
      type: object
      properties:
        severity:
          type: string
          enum:
            - INFO
            - WARNING
            - ERROR
          default: "INFO"
          example: "INFO"
        message:
          type: string
          example: "Hi There"
Generation Details
docker run --rm \
-v ${PWD}:/local openapitools/openapi-generator-cli:latest generate \
-g php \
-i local/api/enum_bug.yaml  \
-o /local/build/bug
Steps to reproduce
Related issues/PRs

No similar Bug Report Found, bug maybe related to

Suggest a fix

As a workaround we apply changes to the template itself (which is not very future proof, but will fix this behavior for us)

$this->container['{{name}}'] = $data['{{name}}'] ?? {{#defaultValue}}{{#isEnum}}self::{{/isEnum}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}};
bschf commented 3 years ago

this one might be the same:

6809