Search in sources :

Example 1 with SchemaDefinitionNamingStrategy

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);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) SchemaDefinitionNamingStrategy(com.github.victools.jsonschema.generator.naming.SchemaDefinitionNamingStrategy) DefinitionKey(com.github.victools.jsonschema.generator.impl.DefinitionKey) ResolvedType(com.fasterxml.classmate.ResolvedType) Parameters(junitparams.Parameters) Test(org.junit.Test)

Aggregations

ResolvedType (com.fasterxml.classmate.ResolvedType)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 DefinitionKey (com.github.victools.jsonschema.generator.impl.DefinitionKey)1 SchemaDefinitionNamingStrategy (com.github.victools.jsonschema.generator.naming.SchemaDefinitionNamingStrategy)1 Parameters (junitparams.Parameters)1 Test (org.junit.Test)1