Incoherence between serialized object and generated schema for enum with @JsonValue
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
- Domain
- backend-api-design
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from FasterXML/jackson-module-jsonSchema
-
Difficulty 4/5 3-5 days Newbie friendliness 38/100
FasterXML/jackson-module-jsonSchema#160 · 3 comments ·
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
-
pr-needed
Difficulty 3/5 1-2 days Newbie friendliness 30/100
FasterXML/jackson-module-jsonSchema#136 · 10 comments ·
-
Difficulty 5/5 Over a week Newbie friendliness 30/100
FasterXML/jackson-module-jsonSchema#135 · 2 comments ·
-
Difficulty 5/5 Over a week Newbie friendliness 20/100
FasterXML/jackson-module-jsonSchema#134 · 2 comments · 1 reaction ·
All issues in FasterXML/jackson-module-jsonSchema
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 60/100
Maintainers usually reply within 1 day
-
[BUG] S3 CORS responses omit Access-Control-Allow-Credentials for matched originsPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
floci-io/floci#5369 · 1 comment ·
Maintainers usually reply within 1 day
-
securityHeaders replaces a route's own Content-Security-Policy (0.9.9; weakens embedders' pages)Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
sqlcipher/sqlcipher-android#97 · 1 comment ·
-
area-integrations
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day