OpenAPITools / OpenAPITools/openapi-generator

[BUG][PHP] Setters for referenced enums are unusable

Open
#4,328 0 comments 3 reactions 0 assignees View on GitHub

@hinrik is already working on this.

Since Nov 14, 2019.

  • #4488 by @hinrik — open
Client: PHP Issue: Bug Server: PHP
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

I'm using version 4.1.3.

Consider this definition:

components:
  schemas:
    Foo:
      type: object
      properties:
        inline_enum:
          type: string
          enum: [one, two, three]
        referenced_enum:
            $ref: '#/components/schemas/ReferencedEnum'
    ReferencedEnum:
      type: string
      enum: [one, two, three]

For inline_enum, Foo's setInlineEnum() method accepts a string, the enum values are constants on the Foo class, and the value being set is validated to be one of the constants.

However, for referenced_enum, setReferencedEnum() accepts a ReferencedEnum object. But that class only has the constants along with a getAllowableEnumValues() function. It carries no state, making it impossible to call setReferencedEnum() with a meaningful value.

I've seen that at least the Java generator handles the referenced_enum case correctly, as it uses a real enum type just like it does for inline_enum.

I would very much like to use the referenced_enum style in my project because it would allow using the same enum definition in multiple places. This is especially handy when one of the consumers of the API definition is a Java application, where this would mean a single enum class instead of multiple duplicate ones where you might have to convert values between each.

Was this just an oversight? If so I can try to whip up a PR to make setReferencedEnum() accept a string, and include the same validation that setInlineEnum() has (but using the values from the ReferencedEnum class).

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

Compare the generated PHP handling of inline_enum with referenced_enum in the Foo and ReferencedEnum examples from the issue. Check that the referenced setter accepts a meaningful enum value and validates it against ReferencedEnum's allowable values, then verify the generated behavior against the existing open pull request.

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.