OpenAPITools / OpenAPITools/openapi-generator
[BUG][PHP] ENUM is not rendered correctly as default value
Nobody has claimed this yet.
- 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.phpand 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
- https://github.com/OpenAPITools/openapi-generator/pull/3524
- https://github.com/OpenAPITools/openapi-generator/issues/1475
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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