Add null to enum definition #212
-
Hello I am trying to validate a json based on a schema generated from the class below and I was wondering if there is a way to add null to the enum definition during the generation without modifying the enum definition? I tried to use @null on field, but has no impact on enum definition. Thanks in advance for you support. Class and enums definitions
Generated schema
Input JSON for validate
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
HI @enthimema, In what way is the enum generated? {
"anyOf": [
{
"type": "null"
},
{
"type": "string",
"enum": [
"fieldNameAndAddress",
"fieldName",
"fieldAddress"
]
}
]
} Perhaps the automatic clean-up logic is not correct there. |
Beta Was this translation helpful? Give feedback.
-
Hello again @enthimema, First of all, as already hinted at above (and described explicitly on #213), the handling of Secondly, your configuration is not quite right yet.
I've adjusted your given example to make it work (now against the new library version 4.21.0): Type Definitionspublic class UiField {
@NotNull
public String name;
@NotNull
public boolean mandatory;
@NotNull
public UiSubField subfield;
public UiFieldDisplayType displayType;
public UiFieldContentType contentType;
}
public class UiSubField {
public String subField;
@NotNull
public boolean enabled;
}
public enum UiFieldContentType {
combobox, checkbox, text;
}
public enum UiFieldDisplayType {
fieldNameAndAddress, fieldName, fieldAddress;
} Test CodeJakartaValidationModule module = new JakartaValidationModule(JakartaValidationOption.INCLUDE_PATTERN_EXPRESSIONS);
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_7, OptionPreset.PLAIN_JSON)
.with(Option.NULLABLE_FIELDS_BY_DEFAULT)
.with(module);
SchemaGeneratorConfig config = configBuilder.build();
SchemaGenerator generator = new SchemaGenerator(config);
JsonNode jsonSchema = generator.generateSchema(UiField.class);
System.out.println(jsonSchema.toPrettyString() + "\n————————————————————");
ObjectMapper mapper = new ObjectMapper();
JsonNode curValue = mapper.readTree("{\n"
+ " \"name\": \"FieldName\",\n"
+ " \"mandatory\": true,\n"
+ " \"subfield\": {\n"
+ " \"subField\": null,\n"
+ " \"enabled\": false\n"
+ " },\n"
+ " \"displayType\": null,\n"
+ " \"contentType\": \"text\"\n"
+ "}");
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
JsonSchema schema = factory.getSchema(jsonSchema);
Set<ValidationMessage> errors = schema.validate(curValue);
System.out.println(errors); Generated Schema{
"$schema" : "http://json-schema.org/draft-07/schema#",
"type" : "object",
"properties" : {
"contentType" : {
"anyOf" : [ {
"type" : "null"
}, {
"type" : "string",
"enum" : [ "combobox", "checkbox", "text" ]
} ]
},
"displayType" : {
"anyOf" : [ {
"type" : "null"
}, {
"type" : "string",
"enum" : [ "fieldNameAndAddress", "fieldName", "fieldAddress" ]
} ]
},
"mandatory" : {
"type" : "boolean"
},
"name" : {
"type" : "string"
},
"subfield" : {
"type" : "object",
"properties" : {
"enabled" : {
"type" : "boolean"
},
"subField" : {
"type" : [ "string", "null" ]
}
}
}
}
} |
Beta Was this translation helpful? Give feedback.
Hello again @enthimema,
First of all, as already hinted at above (and described explicitly on #213), the handling of
null
values on enum types had a bug.A bug fix was created and just released in v4.21.0.
Therefore, please update the
jsonschema-generator
dependencies accordingly.Secondly, your configuration is not quite right yet.
@Null
annotation (according to its JavaDoc) is supposed to be used for fields that are alwaysnull
. It is therefore the incorrect choice (even though the resulting schema is not reflecting that interpretation, but I didn't bother "fixing" that now as well. There is a@javax.validation.Nullable
annotation, which Is probably what you had in mind, but that's…