Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #625
Closed
Open
Issue created Jul 23, 2018 by Administrator@rootContributor

[Java] new option: Throw an exception for invalid enum values instead of using null

Created by: jmini

I have a similar problem than the one reported in https://github.com/swagger-api/swagger-codegen/issues/5950

When working with enum in a java server, if the enum value sent by the client is unknown, after deserialization, the value is set to null.

This might be useful in some cases (the OpenAPI Spec does not always contains all the possible values) but is not the behavior expected in most of the cases.


After having looked at the thread https://github.com/swagger-api/swagger-codegen/issues/5950 I propose to add a new option: useNullForUnknownEnumValue:

  • true (current behavior 3.1.x)
  • false

In my opinion (and as other participants in the original thread) starting with 3.2.0 the new default should be false.

With useNullForUnknownEnumValue == true:

https://github.com/OpenAPITools/openapi-generator/blob/af3ca293e4ff3defcbfd4a32b82f7d3229a29d48/samples/server/petstore/jaxrs/jersey2/src/gen/java/org/openapitools/model/EnumTest.java#L52-L61

With useNullForUnknownEnumValue == false:

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

In the thread there is also a discussion about removing fromValue and the @JsonCreator to let Jackson handling deserialisation.

This is not possible in my opinion:

  • There are cases where the Java enum name is not the enum value. (Lowercase/uppercase, values with spaces are allowed in OAS3, …)
  • The type of the value is not always a string.

cc: @TehBakker, @slarti-b, @wing328, @cbornet

Java Technical Committee: @bbdouglas (2017/07) @JFCote (2017/08) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01)

Assignee
Assign to
Time tracking