0

I am using OpenAPI spec with version 3.0.1 and trying to generate the client code using the openapi-generator-maven-plugin version 7.1.0.

TransactionType:
  type: string
  x-extensible-enum:
    - "AUTHORIZE"
    - "CAPTURE"
    - "REFUND"
    - "VOID"

But instead of generating an enum class, it gives back a string.

Could someone please help?

1

2 Answers 2

1

OpenAPI Generator does not support x-extensible-enum, that's why it generates that field as just a string. Here's a somewhat related feature request: [REQ] Extensible enums - how to model and generate.

You can try customizing the built-in templates to process the x-extensible-enum extension according to your needs, e.g. generate the desired enum class.


According to the documentation, the only enum-related extensions supported by OpenAPI Generator out-of-the-box are x-enum-varnames and x-enum-descriptions.

Sign up to request clarification or add additional context in comments.

Comments

0

TLDR: Generating a Java enum type from an x-extensible-enum defeats the purpose of it – it's not extensible anymore.

An OpenAPI (and JSON schema) enum means that the provider guarantees there won't be any values which are not listed. This means a consumer can rely on it, and e.g. map it to a Java enum class. If we then get a different value, this mapping to an enum value will break – with e.g. an IllegalArgumentException on converting.

So extending an enum is an incompatible change. If an API provider wants to avoid breaking their clients, they need to align the change with them, giving them a chance to extend their enums (and code using it) before starting to actually send such values.

The idea of x-extensible-enum was to avoid this breakage by indicating already in the schema "these are the values we have right now, but please be prepared for additional values being added without notice in the future".

Thus a consumer needs to have a fallback behavior for values other than the known ones, so it doesn't break. If you chose to map it to a strongly-typed thing like a Java enum, you also need some specific handling for when what you have doesn't match the known values (e.g. fall back to a default value, or to null or use some kind of union type of the enum and a string). This is not trivial to do in a generic way, so generally generators for strongly typed languages don't interpret x-extensible-enum, and just treat it as a normal string.

For a consumer, the interpretation would thus be similar to the later added examples from JSON schema.

(I'm one of the original authors of the specification of x-extensible-enum for Zalando's API guidelines. The current rule is in SHOULD use open-ended list of values (x-extensible-enum) for enumeration types [112], though we currently think about deprecating it.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.