How to customize standard json schema generation based on jaxb classes to set base64 contentEncoding #451
-
Hello there, I am trying to generate a json schema starting from jaxb classes generated through xjc and xjb custom bindings This is the pom fragment <configuration>
<classNames>org.foo.bar.Document</classNames>
<schemaVersion>DRAFT_2020_12</schemaVersion>
<schemaFileName>document-schema.json</schemaFileName>
<schemaFilePath>target/generated-sources/json-schema</schemaFilePath>
<modules>
<module>
<name>JakartaValidation</name>
<options>
<option>NOT_NULLABLE_FIELD_IS_REQUIRED</option>
<option>INCLUDE_PATTERN_EXPRESSIONS</option>
</options>
</module>
</modules>
</configuration>
Everything works perfectly but a XML tag defined xs:base64Binary in the xsd schema. @XmlElement(name = "Nclsr", required = true)
@JsonProperty(required = true)
@NotNull
@Size(min = 1, max = 10485760)
protected byte[] enclosure; and the json schema generator accordingly generates this (an array of string) "enclosure" : {
"minItems" : 1,
"maxItems" : 10485760,
"type" : "array",
"items" : {
"type" : "string"
}
}, Is there a way to customize the schema generation to get the following expected definition with content encoding and min/maxLength of a single string instead of an array of strings with min/maxItems? "enclosure" : {
"minLength" : 1,
"maxLength" : 10485760,
"type" : "string",
"items" : {
"type" : "string",
"contentEncoding" : "base64"
}
}, possibly inside maven pom. I have read the discussion How to programmatically generate JsonSchema representing binary content in Java #271, For the record that property will accept a URL base64 encoded and accordingly on the interbank network space the maxLength is quite lesser. But it's not a problem as the restriction won't be enforced by the json schema. Many thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @theseeker58, This is certainly possible. I've created an example under #452 for the required custom configuration: configBuilder.forFields()
.withTargetTypeOverridesResolver(this::treatByteArrayAsString)
.withInstanceAttributeOverride(this::addContentEncodingForByteArray); private List<ResolvedType> treatByteArrayAsString(MemberScope<?, ?> scope) {
ResolvedType type = scope.getType();
if (type != null && type.getErasedType().equals(byte[].class)) {
return Collections.singletonList(scope.getContext().resolve(String.class));
}
return null;
}
private void addContentEncodingForByteArray(ObjectNode schema, MemberScope<?, ?> scope, SchemaGenerationContext context) {
ResolvedType declaredType = scope.getDeclaredType();
if (declaredType != null && declaredType.getErasedType().equals(byte[].class)) {
schema.put("contentEncoding", "base64");
}
} For including such custom configuration in your Maven plugin configuration, you can follow the corresponding entry in the documentation: https://victools.github.io/jsonschema-generator/#further-configurations-through-modules |
Beta Was this translation helpful? Give feedback.
Hi @theseeker58,
This is certainly possible. I've created an example under #452 for the required custom configuration: