Skip to content

Incoherence between serialized object and generated schema for enum with @JsonValue #128

@matthieu-honel-vectaury

Description

@matthieu-honel-vectaury

A class has a field which is an enum.
The enum type is annotated with @JsonFormat(shape = JsonFormat.Shape.NUMBER_INT) so it is serialized as an integer.
The enum type has an int field which is annotated with @JsonValue so this value is used instead of the ordinal of the enum constant.

Expected behavior:
The enum field of an instance of the class is serialized as an integer (number).
The schema generated for the enum field of the class describes an integer type with an enum array containing integers.

Actual behavior:
The enum field of an instance of the class is serialized as an integer (number).
The schema generated for the enum field of the class describes a string type with an enum array containing string.

As a result, trying to validate a serialized instance with the JSON Schema gives an error (test not included in code below).

Test code:

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator;

public class Generator {
    public static void main(String... args) throws JsonProcessingException {
        // instantiate class
        ClassWithEnumField a = new ClassWithEnumField();
        a.numberEnumWithJsonValue = NumberEnumWithJsonValue.VALUE_1;

        // mapper with pretty print feature
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.enable(SerializationFeature.INDENT_OUTPUT);

        // serialize class instance
        System.out.println("Serialized instance:");
        System.out.println(objectMapper.writeValueAsString(a));

        // generate and serialize schema
        JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator(objectMapper);
        JsonSchema jsonSchema = jsonSchemaGenerator.generateSchema(ClassWithEnumField.class);
        System.out.println("Serialized schema:");
        System.out.print(objectMapper.writeValueAsString(jsonSchema));
    }

    static class ClassWithEnumField {
        @JsonProperty("numberEnumWithJsonValue")
        NumberEnumWithJsonValue numberEnumWithJsonValue;
    }

    @JsonFormat(shape = JsonFormat.Shape.NUMBER_INT)
    enum NumberEnumWithJsonValue {
        VALUE_1(1),
        VALUE_2(2);

        @JsonValue
        public final int value;

        NumberEnumWithJsonValue(int value) {
            this.value = value;
        }

    }

}

Output:

Serialized instance:
{
  "numberEnumWithJsonValue" : 1
}
Serialized schema:
{
  "type" : "object",
  "id" : "urn:jsonschema:Generator:ClassWithEnumField",
  "properties" : {
    "numberEnumWithJsonValue" : {
      "type" : "string",
      "enum" : [ "1", "2" ]
    }
  }
}

Expected output:

Serialized instance:
{
  "numberEnumWithJsonValue" : 1
}
Serialized schema:
{
  "type" : "object",
  "id" : "urn:jsonschema:Generator:ClassWithEnumField",
  "properties" : {
    "numberEnumWithJsonValue" : {
      "type" : "integer",
      "enum" : [ 1, 2 ]
    }
  }
}

Using com.fasterxml.jackson.module:jackson-module-jsonSchema:2.9.4 as a gradle dependency.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions