How would I reuse definitions from objects? #301
-
Hi, imagine I have the following 2 classes in one package
and I run the maven-plugin defining the package
What currently happens, that two seperate schema are generated for Foo and Bar. But in Bar everything is generated for Foo as well instead of referencing to the Foo-Schema. Is it possible to configure this in a way? Instead for Bar.schema as it is generated right now
it should become:
Thanks for your help |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @digital-h, Fortunately, I already answered a very similar question here: #246 (comment) More generically speaking: What you ask for requires some custom configuration, as per this FAQ entry:
configBuilder.forTypesInGeneral()
.withCustomDefinitionProvider((javaType, context) -> {
if (javaType.getErasedType() != MyExternalType.class) {
// other types should be treated normally
return null;
}
// define your custom reference value
String refValue = "./" + javaType.getErasedType().getSimpleName();
// produce the sub-schema that only contains your custom reference
ObjectNode customNode = context.getGeneratorConfig().createObjectNode()
.put(context.getKeyword(SchemaKeyword.TAG_REF), refValue);
return new CustomDefinition(customNode,
// avoid the creation of a reference to your custom reference schema
CustomDefinition.DefinitionType.INLINE,
// still allow for collected schema attributes to be added
CustomDefinition.AttributeInclusion.YES);
}); If you want this kind of custom configuration to be applied by the Maven plugin, you'd have to wrap it into a
<modules>
<module>
<className>com.myOrg.myApp.CustomModule</className>
</module>
</modules> |
Beta Was this translation helpful? Give feedback.
Hi @digital-h,
Fortunately, I already answered a very similar question here: #246 (comment)
That includes a complete code example.
More generically speaking: What you ask for requires some custom configuration, as per this FAQ entry:
https://victools.github.io/jsonschema-generator/#how-to-reference-a-separate-schema-file