Open tayloj opened 5 months ago
Just a note, in case anyone runs into the same problem, in light of the documentation about examples (emphasis added):
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:
@JsonSchemaInject(json = "{\"examples\":[2.3]}")
public double someDouble;
yields
"properties" : {
"someDouble" : {
"type" : "number",
"examples" : [ 2.3 ]
}
},
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