Skip to content

[JAVA] Enum mapping make wrong value as null instead of throwing an exception #5950

@TehBakker

Description

@TehBakker
Description

Short : If I've a value that does not match my enum, the JsonCreator of the enum make it null, where I would want it to throw a validation exception.
This is following #1919 changes

Swagger-codegen version

2.2.3-SNAPSHOT

Swagger declaration file content or url
 MyBean:
      type: object
      required:
        - enumList
      description: Enum list attribute
      properties:
        enumList:
          type: array
          minItems: 1
          items:
            $ref: '#/definitions/MyEnum'

 MyEnum:
      type: string
      description: blabla my enum
      enum:
        - VALUE1
        - VALUE2

The generated code is

public enum MyEnum {
  
  VALUE1("VALUE1"),
  
  VALUE2("VALUE1");

  private String value;

  MyEnum(String value) {
    this.value = value;
  }

  @Override
  @JsonValue
  public String toString() {
    return String.valueOf(value);
  }

  @JsonCreator
  public static MyEnum fromValue(String text) {
    for (MyEnum b : MyEnum.values()) {
      if (String.valueOf(b.value).equals(text)) {
        return b;
      }
    }
    return null;
  }
}
Related issues

#1919

Suggest a Fix

What I would like is to be able to say if in case of unknown value, the @JsonCreator should return null like today or throw an exception.
Since my Enum is in a list attribut, I cannot just use @NotNull (and the exception message wouldn't be correct with what is really going on anyway).

Also is there a way to tell a list attribut that it should not contains any null value ?
Such as Jackson READ_UNKNOWN_ENUM_VALUES_AS_NULL property allow to declare ?

Please let me know if I'm not clear enough.

Regards

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions