victools / jsonschema-generator

Java JSON Schema Generator – creating JSON Schema (Draft 6, Draft 7, Draft 2019-09, or Draft 2020-12) from Java classes
https://victools.github.io/jsonschema-generator
Apache License 2.0
398 stars 58 forks source link

JAXB annotation support #429

Closed Thomasator closed 9 months ago

Thomasator commented 9 months ago

Hi,

I am not able to get this tool to work to convert java classes (generated from xsd using xjc) and to get the required parametrs (@XmlElement(required=true) to json schema. Is this tool able to do this? Or is it possible to add it?

Thx for the responce Tomas

CarstenWickner commented 9 months ago

Hi @Thomasator,

In the existing standard modules, annotations related to XML structures are not considered for the generated JSON schema. That being said, it's certainly possible to configure those to be picked up. For your given example, refer to: https://victools.github.io/jsonschema-generator/#quot-required-quot-keyword You could adapt the given example to something like this:

configBuilder.forFields()
    .withRequiredCheck(field -> Optional.ofNullable(field.getAnnotationConsideringFieldAndGetter(XmlElement.class))
            .map(a -> a.required()).filter(required -> required).orElse(null));