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
Generating schemas for classes that have double-valued fields annotated with @JsonSchemaExamples annotation causes an exception to be thrown. For instance, schema generation for this class with an int-valued field works (note that the example value doesn't even have to adhere to the syntax of the field type):
It fails regardless of the specific example (I've tried with, for instance, "2", "2.3", and "foobar", and even an empty array of examples, i.e., @JsonSchemaExamples({})). Here's a test case that reproduces the problem.
importjava.io.IOException;
importorg.junit.jupiter.api.Test;
importcom.fasterxml.jackson.databind.JsonNode;
importcom.fasterxml.jackson.databind.ObjectMapper;
importcom.fasterxml.jackson.databind.SerializationFeature;
importcom.kjetland.jackson.jsonSchema.JsonSchemaGenerator;
importcom.kjetland.jackson.jsonSchema.annotations.JsonSchemaExamples;
publicclassTestDoubleExamples {
publicstaticclassDoubleHolder {
@JsonSchemaExamples("2.3")
publicdoublesomeDouble;
}
publicstaticclassIntHolder {
@JsonSchemaExamples("2")
publicintsomeInt;
}
/** * Generate a schema and write it to standard output. * * @param clazz the class to generate a scheam for * @throws IOException if an I/O error occurs */voidgenerateSchema(finalClass<?> clazz) throwsIOException {
finalObjectMapperom = newObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
finalJsonSchemaGeneratorgen = newJsonSchemaGenerator(om);
finalJsonNodeschema = gen.generateJsonSchema(clazz);
finalStringtxt = om.writeValueAsString(schema);
System.out.println(txt);
}
/** * Prints * * <pre> { "$schema" : "http://json-schema.org/draft-04/schema#", "title" : "Int Holder", "type" : "object", "additionalProperties" : false, "properties" : { "someInt" : { "type" : "integer", "examples" : [ "2" ] } }, "required" : [ "someInt" ] } * </pre> * * @throws IOException */@TestpublicvoidgetWithInt() throwsIOException {
generateSchema(IntHolder.class);
}
/** * Fails with trace: * * <pre>java.lang.ClassCastException: class com.fasterxml.jackson.databind.node.ObjectNode cannot be cast to class scala.runtime.Nothing$ (com.fasterxml.jackson.databind.node.ObjectNode and scala.runtime.Nothing$ are in unnamed module of loader 'app') at com.kjetland.jackson.jsonSchema.JsonSchemaGenerator$MyJsonFormatVisitorWrapper.$anonfun$expectNumberFormat$8(JsonSchemaGenerator.scala:710) at scala.Option.map(Option.scala:230) at com.kjetland.jackson.jsonSchema.JsonSchemaGenerator$MyJsonFormatVisitorWrapper.$anonfun$expectNumberFormat$2(JsonSchemaGenerator.scala:705) at scala.Option.map(Option.scala:230) at com.kjetland.jackson.jsonSchema.JsonSchemaGenerator$MyJsonFormatVisitorWrapper.expectNumberFormat(JsonSchemaGenerator.scala:678) at com.fasterxml.jackson.databind.ser.std.StdSerializer.visitFloatFormat(StdSerializer.java:250) at com.fasterxml.jackson.databind.ser.std.NumberSerializers$Base.acceptJsonFormatVisitor(NumberSerializers.java:94) at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.acceptJsonFormatVisitor(DefaultSerializerProvider.java:588) at com.fasterxml.jackson.databind.ObjectMapper.acceptJsonFormatVisitor(ObjectMapper.java:4744) at com.kjetland.jackson.jsonSchema.JsonSchemaGenerator$MyJsonFormatVisitorWrapper$$anon$9.myPropertyHandler(JsonSchemaGenerator.scala:1191) at com.kjetland.jackson.jsonSchema.JsonSchemaGenerator$MyJsonFormatVisitorWrapper$$anon$9.optionalProperty(JsonSchemaGenerator.scala:1265) at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.depositSchemaProperty(BeanPropertyWriter.java:843) at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.acceptJsonFormatVisitor(BeanSerializerBase.java:912) at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.acceptJsonFormatVisitor(DefaultSerializerProvider.java:588) at com.fasterxml.jackson.databind.ObjectMapper.acceptJsonFormatVisitor(ObjectMapper.java:4744) at com.kjetland.jackson.jsonSchema.JsonSchemaGenerator.generateJsonSchema(JsonSchemaGenerator.scala:1477) at com.kjetland.jackson.jsonSchema.JsonSchemaGenerator.generateJsonSchema(JsonSchemaGenerator.scala:1440) at com.kjetland.jackson.jsonSchema.JsonSchemaGenerator.generateJsonSchema(JsonSchemaGenerator.scala:1415) at TestDoubleExamples.generateSchema(TestDoubleExamples.java:32) at TestDoubleExamples.getWithDouble(TestDoubleExamples.java:97) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) * </pre> * * @throws IOException */@TestpublicvoidgetWithDouble() throwsIOException {
generateSchema(DoubleHolder.class);
}
}
If I remove the annotation from the DoubleHolder class, then a schema is generated correctly:
The examples keyword is a place to provide an array of examples that validate against the schema. This isn't used for validation, but may help with explaining the effect and purpose of the schema to a reader. Each entry should validate against the schema in which it resides, but that isn't strictly required. There is no need to duplicate the default value in the examples array, since default will be treated as another example.
The default value and examples, then, really ought be of the right type, and thus a string-valued example for a double-valued field isn't ideal (though it's allowed, since it isn't "strictly" required). Using the @JsonSchemaInject annotation, I'm able to get a working examples field, and with a correctly typed value, at the cost of some beauty in the source code:
This may be a useful workaround, especially since the typing requirements in Java's annotations would require either multiple fields in the @JsonSchemaExamples annotation, e.g., to be able to write @JsonSchemaExamples(doubles = { 2.3 }, strings = {"some string"}) and so on. Alternatively, different variants could be used, I suppose, e.g., @JsonSchemaExamplesDoubles(2.3) and @JsonSchemaExamplesStrings("a string"), but that seems worse.
Description
Generating schemas for classes that have double-valued fields annotated with @JsonSchemaExamples annotation causes an exception to be thrown. For instance, schema generation for this class with an int-valued field works (note that the example value doesn't even have to adhere to the syntax of the field type):
But schema generation for this field fails:
It fails regardless of the specific example (I've tried with, for instance, "2", "2.3", and "foobar", and even an empty array of examples, i.e.,
@JsonSchemaExamples({})
). Here's a test case that reproduces the problem.If I remove the annotation from the DoubleHolder class, then a schema is generated correctly:
Platform
Java
Platform
Maven dependencies
The text was updated successfully, but these errors were encountered: