openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG][jaxrs-spec] enumUnknownDefaultCase not deserialized

Open PhilippParis opened this issue 3 years ago • 0 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [ ] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

using the jaxrs-spec (and probably other jaxrs) generator the 'enumUnknownDefaultCase' property is not working correctly: altough the additional enum value is created, the value is not used in the deserialization of the enum

Expected Output
  @JsonCreator
  public static ColorDto fromValue(String value) {
    for (ColorDto b : ColorDto.values()) {
      if (b.value.equals(value)) {
        return b;
      }
    }
    return UNKNOWN_DEFAULT_OPEN_API;
  }
Actual Output
  @JsonCreator
  public static ColorDto fromValue(String value) {
    for (ColorDto b : ColorDto.values()) {
      if (b.value.equals(value)) {
        return b;
      }
    }
    throw new IllegalArgumentException("Unexpected value '" + value + "'");
  }
openapi-generator version

6.1.0

OpenAPI declaration file content or url
openapi: "3.0.0"
info:
  version: 2.0.0
  title: test
paths:
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      responses:
        '200':
          description: OK
components:
  schemas:
    Color:
      type: string
      enum: [RED, BLUE, GREEN]
Generation Details

using the openapi-maven-generator plugin with configOption

<enumUnknownDefaultCase>true</enumUnknownDefaultCase>
Steps to reproduce

generate code

Related issues/PRs

In https://github.com/OpenAPITools/openapi-generator/issues/11886 the same problem was fixed for the 'java' generator

Suggest a fix

in 'modelEnum.class', 'enumClass.mustache' and 'enumOuterClass.mustache' in resources/JavaJaxRs and all subfolders:

replace

{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}

with

{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}{{#enumUnknownDefaultCase}}{{#allowableValues}}{{#enumVars}}{{#-last}}return {{{name}}};{{/-last}}{{/enumVars}}{{/allowableValues}}{{/enumUnknownDefaultCase}}{{^enumUnknownDefaultCase}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/enumUnknownDefaultCase}}{{/isNullable}}

PhilippParis avatar Sep 16 '22 06:09 PhilippParis