use of com.github.victools.jsonschema.generator.naming.SchemaDefinitionNamingStrategy in project jsonschema-generator by victools.
the class SchemaGeneratorCustomDefinitionsTest method testGenerateSchema_CustomStandardDefinition.
@Test
@Parameters(source = SchemaVersion.class)
public void testGenerateSchema_CustomStandardDefinition(SchemaVersion schemaVersion) throws Exception {
CustomDefinitionProviderV2 customDefinitionProviderOne = new CustomDefinitionProviderV2() {
@Override
public CustomDefinition provideCustomSchemaDefinition(ResolvedType javaType, SchemaGenerationContext context) {
ObjectNode customDefinition = context.getGeneratorConfig().createObjectNode().put(context.getKeyword(SchemaKeyword.TAG_TITLE), "Custom Definition #1 for " + context.getTypeContext().getSimpleTypeDescription(javaType));
// using SchemaGenerationContext.createStandardDefinitionReference() to avoid endless loop with this custom definition
customDefinition.withArray(context.getKeyword(SchemaKeyword.TAG_ANYOF)).add(context.createStandardDefinitionReference(javaType, this)).addObject().put(context.getKeyword(SchemaKeyword.TAG_TYPE), context.getKeyword(SchemaKeyword.TAG_TYPE_NULL));
return new CustomDefinition(customDefinition);
}
};
CustomDefinitionProviderV2 customDefinitionProviderTwo = new CustomDefinitionProviderV2() {
@Override
public CustomDefinition provideCustomSchemaDefinition(ResolvedType javaType, SchemaGenerationContext context) {
if (javaType.getErasedType() == String.class) {
return null;
}
ObjectNode customDefinition = context.getGeneratorConfig().createObjectNode().put(context.getKeyword(SchemaKeyword.TAG_TITLE), "Custom Definition #2 for " + context.getTypeContext().getFullTypeDescription(javaType));
// using SchemaGenerationContext.createStandardDefinitionReference() to avoid endless loop with this custom definition
customDefinition.withArray(context.getKeyword(SchemaKeyword.TAG_ANYOF)).add(context.createStandardDefinitionReference(javaType, this)).addObject().put(context.getKeyword(SchemaKeyword.TAG_TYPE), context.getKeyword(SchemaKeyword.TAG_TYPE_NULL));
return new CustomDefinition(customDefinition);
}
};
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(schemaVersion, OptionPreset.PLAIN_JSON).with(Option.DEFINITIONS_FOR_ALL_OBJECTS);
configBuilder.forTypesInGeneral().withCustomDefinitionProvider(customDefinitionProviderOne).withCustomDefinitionProvider(customDefinitionProviderTwo).withDefinitionNamingStrategy(new SchemaDefinitionNamingStrategy() {
@Override
public String getDefinitionNameForKey(DefinitionKey key, SchemaGenerationContext generationContext) {
return key.getType().getErasedType().getSimpleName().toLowerCase();
}
@Override
public void adjustDuplicateNames(Map<DefinitionKey, String> duplicateNames, SchemaGenerationContext context) {
char suffix = 'a';
for (Map.Entry<DefinitionKey, String> singleEntry : duplicateNames.entrySet()) {
singleEntry.setValue(singleEntry.getValue() + " (" + suffix + ")");
suffix++;
}
}
});
SchemaGenerator generator = new SchemaGenerator(configBuilder.build());
JsonNode result = generator.generateSchema(TestDirectCircularClass.class);
JSONAssert.assertEquals('\n' + result.toString() + '\n', TestUtils.loadResource(this.getClass(), "multiple-definitions-one-type-" + schemaVersion.name() + ".json"), result.toString(), JSONCompareMode.STRICT);
}
Aggregations