OpenAPITools / OpenAPITools/openapi-generator

Default value of Enum type

Open
#11,110 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

I'm trying to provide a default value for a generated class that uses an enum. Currently type enum is always defaulted to INVALID_VALUE_OPENAPI_GENERATED, despite the field default being specified. Is this a bug?

I have tried experimenting with the mustache templates to output the default with {{{defaultValue}}} for the specified enum, where I could then modify the templates to provide the default, however this variable is always empty for this use case.

openapi-generator version

5.3.0 C++ Pistache. - I am not sure if this is specific to the C++ Pistache Generator.

Command line used for generation

Example snippet from .yaml definition:

..
Codec:
    type: string
    example: JPEG
    default: JPEG
    enum:
        - JPEG
        - PNG
..
Steps to reproduce
  1. prepare a .yaml which contains the above class
  2. generate the code with the C++ Pistache generator

Notice that the header of the generated code contains the following:
Codec::eCodec m_value = Codec::eCodec::INVALID_VALUE_OPENAPI_GENERATED;

I would expect the code would be:
Codec::eCodec m_value = Codec::eCodec::JPEG;

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 supplied YAML with the C++ Pistache generator, then trace how enum defaults reach the Mustache templates and generated header. Done means a field with default: JPEG initializes to Codec::eCodec::JPEG instead of INVALID_VALUE_OPENAPI_GENERATED.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.