Skip to content

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

Open
@sleipi

Description

@sleipi

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
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
  • Generate Client from the provided example using the CLI Command from the Generation Details section.
  • open Notification.php and see the error in Line 207
    public function __construct(array $data = null)
    {
        $this->container['severity'] = $data['severity'] ?? SEVERITY_INFO; // <-- this must be a self::SEVERITY_INFO
    }
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}};

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions