You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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;
}
}
}
I am experiencing the same issue. Has there been or will there be any attempt to develop a fix for this? Do you need additional contributors?
I am willing to attempt to correct this, ideally, if you provide direction as to what should be modified so I can make the appropriate changes and issue a pull request.
If someone had worked on it, there would be a note here. I don't think anyone is working on this module at this point. It is fair to say this module is an orphan, similar to Scala and Hibernate modules.
As to how to fix that, I do not know details. But the way schema generation works is that JsonSerializer instances for type have method acceptJsonFormatVisitor() which calls methods on visitor object given; and then this module provides visitors that generate schema based on callbacks.
Problem(s) involved may thereby be either on jackson-databind side (EnumSerializer, most likely), or this module, or possibly, both.
And a common challenge is that sometimes information passed by databind is incomplete so fix might require a change to introspection part, which is difficult from backwards compatibility perspective.
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:
Output:
Expected output:
Using
com.fasterxml.jackson.module:jackson-module-jsonSchema:2.9.4
as a gradle dependency.The text was updated successfully, but these errors were encountered: