Skip to content

[BUG][Java] nullable not considered in Enum.fromValue on OpenAPI 3.1 specs #20753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
5 of 6 tasks
david0 opened this issue Feb 27, 2025 · 1 comment
Closed
5 of 6 tasks

Comments

@david0
Copy link
Contributor

david0 commented Feb 27, 2025

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

We try to update from OpenAPI 3.0 to 3.1 and face the issue that enums fromValue method behave different now

the enum type has nullable: true set.
With openapi 3.0.3 this is generated:

  @JsonCreator
  public static StatusEnum fromValue(String value) {
    for (StatusEnum b : StatusEnum.values()) {
      if (b.value.equals(value)) {
        return b;
      }
    }
    return null;
  }

when changing to 3.1.0 the null is not supported anymore:

  @JsonCreator
  public static StatusEnum fromValue(String value) {
    for (StatusEnum b : StatusEnum.values()) {
      if (b.value.equals(value)) {
        return b;
      }
    }
    throw new IllegalArgumentException("Unexpected value '" + value + "'");
  }

I'm not aware of any way to change that.

openapi-generator version

7.11.0

OpenAPI declaration file content or url
openapi: 3.1.0
info:
  title: Minimal API Example
  version: 1.0.0
paths:
  /items:
    get:
      summary: Get items
      responses:
        '200':
          description: A list of items
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Item'
components:
  schemas:
    Item:
      type: object
      properties:
        name:
          type: string
        status:
          $ref: '#/components/schemas/StatusEnum'

    StatusEnum:
      type: string
      nullable: true
      enum:
        - available
        - out_of_stock
        - discontinued
Generation Details

openapi-generator generate -g java --library webclient -i example.yaml

Steps to reproduce
  1. run command above.
  2. grep fromValue -A10 ./src/main/java/org/openapitools/client/model/StatusEnum.java
Related issues/PRs
Suggest a fix
@david0
Copy link
Contributor Author

david0 commented Feb 27, 2025

I found out that nullable is not a thing anymore and hence dropped when parsing with swagger-parser.

The alternative is pointed out here:
OAI/OpenAPI-Specification#2244 (comment)

Instead of nullable:true, change the type type: [string, 'null']

@david0 david0 closed this as completed Feb 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant