OpenAPITools / OpenAPITools/openapi-generator

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

Open
#9,038 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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}};

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the provided OpenAPI YAML and the documented Docker CLI command using the PHP generator. Inspect the generated Notification.php constructor and the generator template involved in the workaround; done means an enum default is rendered with self:: and the generated PHP client no longer uses an undefined constant.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.