Closed hleb-albau closed 5 years ago
Hi @hleb-albau,
the validations are picked up correctly. You don't see because the OptionPreset.PLAIN_JSON
does not include the Option.NULLABLE_FIELDS_BY_DEFAULT
, i.e. all fields are assumed to be not-nullable.
If you simply add this call: configBuilder.with(Option.NULLABLE_FIELDS_BY_DEFAULT)
you will see the difference between the @NotNull private String value
on the ListElement
and the nullable childForm
field on the RootForm
.
To make it a bit more obvious you can use another kind of validation on the ListElement
's value
, e.g. with the following adjusted example:
public class ListElementTest {
@Test
public void test() {
ObjectMapper objectMapper = new ObjectMapper();
JavaxValidationModule module = new JavaxValidationModule();
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(objectMapper, OptionPreset.PLAIN_JSON)
.with(module)
.with(Option.NULLABLE_FIELDS_BY_DEFAULT);
SchemaGeneratorConfig config = configBuilder.build();
SchemaGenerator generator = new SchemaGenerator(config);
JsonNode jsonSchema = generator.generateSchema(RootForm.class);
System.out.println(jsonSchema.toString());
}
private static class RootForm {
@NotEmpty
private String name;
@Valid
private ChildForm childForm;
}
private static class ChildForm {
@Valid
private List<ListElement> elements;
@Min(2)
private Integer count;
}
private static class ListElement {
@NotNull
@Size(max = 100)
private String value;
}
}
You get this JSON Schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"childForm": {
"type": ["object", "null"],
"properties": {
"count": {
"type": ["integer", "null"],
"minimum": 2
},
"elements": {
"type": ["array", "null"],
"items": {
"type": "object",
"properties": {
"value": {
"type": "string",
"maxLength": 100
}
}
}
}
}
},
"name": {
"type": "string",
"minLength": 1
}
}
}
Looks correct to me.
@CarstenWickner Oh, I see. Thanks for fast response!
Hi, test below do not return constraint description for
ListElement
values.