Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

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

Open
#128 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
java

Research direction

Start with JsonSchemaGenerator and the enum handling used for @JsonFormat(shape = NUMBER_INT) and @JsonValue, then reproduce the example with jackson-module-jsonSchema 2.9.4. Done means the generated schema describes the enum field as an integer with numeric values matching serialized output, with a regression test covering the example.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Java
Stars
387
Forks
137
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from FasterXML/jackson-module-jsonSchema

All issues in FasterXML/jackson-module-jsonSchema

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.