Search in sources :

Example 1 with MethodScope

use of com.github.victools.jsonschema.generator.MethodScope in project jsonschema-generator by victools.

the class SchemaGenerationContextImpl method collectMethod.

/**
 * Preparation Step: add the given method to the specified {@link Map}.
 *
 * @param method declared method that should be added to the specified node
 * @param collectedProperties collection of fields/methods to be added as properties
 * @param requiredProperties set of properties value required
 */
private void collectMethod(MethodScope method, Map<String, MemberScope<?, ?>> collectedProperties, Set<String> requiredProperties) {
    final MethodScope methodWithNameOverride;
    final String propertyName;
    if (method.isFakeContainerItemScope()) {
        methodWithNameOverride = method;
        propertyName = method.getSchemaPropertyName();
    } else {
        String propertyNameOverride = this.generatorConfig.resolvePropertyNameOverride(method);
        methodWithNameOverride = propertyNameOverride == null ? method : method.withOverriddenName(propertyNameOverride);
        propertyName = methodWithNameOverride.getSchemaPropertyName();
        if (this.generatorConfig.isRequired(method)) {
            requiredProperties.add(propertyName);
        }
        if (collectedProperties.containsKey(propertyName)) {
            logger.debug("ignoring overridden {}.{}", methodWithNameOverride.getDeclaringType(), methodWithNameOverride.getDeclaredName());
            return;
        }
    }
    collectedProperties.put(propertyName, methodWithNameOverride);
}
Also used : MethodScope(com.github.victools.jsonschema.generator.MethodScope)

Example 2 with MethodScope

use of com.github.victools.jsonschema.generator.MethodScope in project jsonschema-generator by victools.

the class SchemaGenerationContextImpl method generateArrayDefinition.

/**
 * Preparation Step: add the given targetType (which was previously determined to be an array type).
 *
 * @param targetScope (possibly generic) array type to add
 * @param definition node in the JSON schema to which all collected attributes should be added
 * @param isNullable whether the field/method's return value the targetType refers to is allowed to be null in the declaring type
 */
private void generateArrayDefinition(TypeScope targetScope, ObjectNode definition, boolean isNullable) {
    if (isNullable) {
        ArrayNode typeArray = this.generatorConfig.createArrayNode().add(this.getKeyword(SchemaKeyword.TAG_TYPE_ARRAY)).add(this.getKeyword(SchemaKeyword.TAG_TYPE_NULL));
        definition.set(this.getKeyword(SchemaKeyword.TAG_TYPE), typeArray);
    } else {
        definition.put(this.getKeyword(SchemaKeyword.TAG_TYPE), this.getKeyword(SchemaKeyword.TAG_TYPE_ARRAY));
    }
    if (targetScope instanceof MemberScope<?, ?> && !((MemberScope<?, ?>) targetScope).isFakeContainerItemScope()) {
        MemberScope<?, ?> fakeArrayItemMember = ((MemberScope<?, ?>) targetScope).asFakeContainerItemScope();
        JsonNode fakeItemDefinition;
        if (targetScope instanceof FieldScope) {
            fakeItemDefinition = this.populateFieldSchema((FieldScope) fakeArrayItemMember);
        } else if (targetScope instanceof MethodScope) {
            fakeItemDefinition = this.populateMethodSchema((MethodScope) fakeArrayItemMember);
        } else {
            throw new IllegalStateException("Unsupported member type: " + targetScope.getClass().getName());
        }
        definition.set(this.getKeyword(SchemaKeyword.TAG_ITEMS), fakeItemDefinition);
    } else {
        ObjectNode arrayItemTypeRef = this.generatorConfig.createObjectNode();
        definition.set(this.getKeyword(SchemaKeyword.TAG_ITEMS), arrayItemTypeRef);
        this.traverseGenericType(targetScope.getContainerItemType(), arrayItemTypeRef, false);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) FieldScope(com.github.victools.jsonschema.generator.FieldScope) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) MethodScope(com.github.victools.jsonschema.generator.MethodScope) MemberScope(com.github.victools.jsonschema.generator.MemberScope)

Example 3 with MethodScope

use of com.github.victools.jsonschema.generator.MethodScope in project jsonschema-generator by victools.

the class SchemaGenerationContextImplTest method testCreateStandardDefinitionReferenceForMethod_noCustomDefinition.

@Test
public void testCreateStandardDefinitionReferenceForMethod_noCustomDefinition() {
    MethodScope targetMethod = this.getTestClassMethod("isBooleanField");
    JsonNode result = this.contextImpl.createStandardDefinitionReference(targetMethod, null);
    Assert.assertEquals("{\"allOf\":[{},{\"title\":\"Method Title\"}]}", result.toString());
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) MethodScope(com.github.victools.jsonschema.generator.MethodScope) Test(org.junit.Test) AbstractTypeAwareTest(com.github.victools.jsonschema.generator.AbstractTypeAwareTest)

Example 4 with MethodScope

use of com.github.victools.jsonschema.generator.MethodScope in project jsonschema-generator by victools.

the class SchemaGenerationContextImplTest method testCreateStandardDefinitionReferenceForMethod_withCustomTypeDefinition.

@Test
public void testCreateStandardDefinitionReferenceForMethod_withCustomTypeDefinition() {
    Mockito.doReturn(new CustomDefinition(this.contextImpl.getGeneratorConfig().createObjectNode().put("$comment", "custom type"), true)).when(this.contextImpl.getGeneratorConfig()).getCustomDefinition(Mockito.any(ResolvedType.class), Mockito.any(), Mockito.any());
    MethodScope targetMethod = this.getTestClassMethod("isBooleanField");
    JsonNode result = this.contextImpl.createStandardDefinitionReference(targetMethod, null);
    Assert.assertEquals("{\"allOf\":[{\"$comment\":\"custom type\",\"description\":\"Type Description\"},{\"title\":\"Method Title\"}]}", result.toString());
}
Also used : CustomDefinition(com.github.victools.jsonschema.generator.CustomDefinition) JsonNode(com.fasterxml.jackson.databind.JsonNode) MethodScope(com.github.victools.jsonschema.generator.MethodScope) ResolvedType(com.fasterxml.classmate.ResolvedType) Test(org.junit.Test) AbstractTypeAwareTest(com.github.victools.jsonschema.generator.AbstractTypeAwareTest)

Example 5 with MethodScope

use of com.github.victools.jsonschema.generator.MethodScope in project jsonschema-generator by victools.

the class MethodExclusionModuleTest method testIgnoreCheck.

@Test
@Parameters
@TestCaseName("{method}({1}: {0} => {2}) [{index}]")
public void testIgnoreCheck(String testMethodName, String supplierMethodName, boolean ignored) throws Exception {
    MethodExclusionModule moduleInstance = (MethodExclusionModule) MethodExclusionModule.class.getMethod(supplierMethodName).invoke(null);
    moduleInstance.applyToConfigBuilder(this.builder);
    MethodScope method = this.getTestClassMethod(testMethodName);
    Assert.assertEquals(ignored, this.methodConfigPart.shouldIgnore(method));
}
Also used : MethodScope(com.github.victools.jsonschema.generator.MethodScope) Parameters(junitparams.Parameters) Test(org.junit.Test) AbstractTypeAwareTest(com.github.victools.jsonschema.generator.AbstractTypeAwareTest) TestCaseName(junitparams.naming.TestCaseName)

Aggregations

MethodScope (com.github.victools.jsonschema.generator.MethodScope)16 Test (org.junit.Test)8 AbstractTypeAwareTest (com.github.victools.jsonschema.generator.AbstractTypeAwareTest)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 FieldScope (com.github.victools.jsonschema.generator.FieldScope)4 Parameters (junitparams.Parameters)4 ResolvedType (com.fasterxml.classmate.ResolvedType)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)3 CustomDefinition (com.github.victools.jsonschema.generator.CustomDefinition)3 CustomPropertyDefinition (com.github.victools.jsonschema.generator.CustomPropertyDefinition)3 ConfigFunction (com.github.victools.jsonschema.generator.ConfigFunction)2 MemberScope (com.github.victools.jsonschema.generator.MemberScope)2 JsonSubTypes (com.fasterxml.jackson.annotation.JsonSubTypes)1 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)1 SchemaGeneratorGeneralConfigPart (com.github.victools.jsonschema.generator.SchemaGeneratorGeneralConfigPart)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 TestCaseName (junitparams.naming.TestCaseName)1