Search in sources :

Example 1 with FieldScope

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

the class SchemaGenerationContextImpl method populateFieldSchema.

/**
 * Preparation Step: create a node for a schema representing the given field's associated value type.
 *
 * @param field field/property to populate the schema node for
 * @return schema node representing the given field/property
 */
private ObjectNode populateFieldSchema(FieldScope field) {
    List<ResolvedType> typeOverrides = this.generatorConfig.resolveTargetTypeOverrides(field);
    if (typeOverrides == null) {
        typeOverrides = this.generatorConfig.resolveSubtypes(field.getType(), this);
    }
    List<FieldScope> fieldOptions;
    if (typeOverrides == null || typeOverrides.isEmpty()) {
        fieldOptions = Collections.singletonList(field);
    } else {
        fieldOptions = typeOverrides.stream().map(field::withOverriddenType).collect(Collectors.toList());
    }
    // consider declared type (instead of overridden one) for determining null-ability
    boolean isNullable = !field.getRawMember().isEnumConstant() && (!field.isFakeContainerItemScope() || this.generatorConfig.shouldAllowNullableArrayItems()) && this.generatorConfig.isNullable(field);
    if (fieldOptions.size() == 1) {
        return this.createFieldSchema(fieldOptions.get(0), isNullable, false, null);
    }
    ObjectNode subSchema = this.generatorConfig.createObjectNode();
    ArrayNode anyOfArray = subSchema.withArray(this.getKeyword(SchemaKeyword.TAG_ANYOF));
    if (isNullable) {
        anyOfArray.addObject().put(this.getKeyword(SchemaKeyword.TAG_TYPE), this.getKeyword(SchemaKeyword.TAG_TYPE_NULL));
    }
    fieldOptions.forEach(option -> anyOfArray.add(this.createFieldSchema(option, false, false, null)));
    return subSchema;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) FieldScope(com.github.victools.jsonschema.generator.FieldScope) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ResolvedType(com.fasterxml.classmate.ResolvedType)

Example 2 with FieldScope

use of com.github.victools.jsonschema.generator.FieldScope 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 FieldScope

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

the class JacksonModuleTest method testPropertyNameOverride.

@Test
@Parameters
public void testPropertyNameOverride(String fieldName, String expectedOverrideValue, String kebabCaseName) throws Exception {
    new JacksonModule().applyToConfigBuilder(this.configBuilder);
    ArgumentCaptor<ConfigFunction<FieldScope, String>> captor = ArgumentCaptor.forClass(ConfigFunction.class);
    Mockito.verify(this.fieldConfigPart, Mockito.times(2)).withPropertyNameOverrideResolver(captor.capture());
    FieldScope field = new TestType(TestClassForPropertyNameOverride.class).getMemberField(fieldName);
    List<String> overrideValues = captor.getAllValues().stream().map(nameOverride -> nameOverride.apply(field)).collect(Collectors.toList());
    Assert.assertEquals(expectedOverrideValue, overrideValues.get(0));
    Assert.assertEquals(kebabCaseName, overrideValues.get(1));
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) FieldScope(com.github.victools.jsonschema.generator.FieldScope) MethodScope(com.github.victools.jsonschema.generator.MethodScope) JsonPropertyDescription(com.fasterxml.jackson.annotation.JsonPropertyDescription) SchemaGeneratorGeneralConfigPart(com.github.victools.jsonschema.generator.SchemaGeneratorGeneralConfigPart) SchemaGeneratorConfigPart(com.github.victools.jsonschema.generator.SchemaGeneratorConfigPart) SchemaGeneratorConfigBuilder(com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder) RunWith(org.junit.runner.RunWith) TypeScope(com.github.victools.jsonschema.generator.TypeScope) Test(org.junit.Test) Collectors(java.util.stream.Collectors) PropertyNamingStrategies(com.fasterxml.jackson.databind.PropertyNamingStrategies) Mockito(org.mockito.Mockito) List(java.util.List) ArgumentCaptor(org.mockito.ArgumentCaptor) JsonNaming(com.fasterxml.jackson.databind.annotation.JsonNaming) JsonClassDescription(com.fasterxml.jackson.annotation.JsonClassDescription) JUnitParamsRunner(junitparams.JUnitParamsRunner) ConfigFunction(com.github.victools.jsonschema.generator.ConfigFunction) Assert(org.junit.Assert) Parameters(junitparams.Parameters) Before(org.junit.Before) FieldScope(com.github.victools.jsonschema.generator.FieldScope) ConfigFunction(com.github.victools.jsonschema.generator.ConfigFunction) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 4 with FieldScope

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

the class JacksonModuleTest method testReadOnlyWriteOnly.

@Test
@Parameters
public void testReadOnlyWriteOnly(String fieldName, boolean expectedReadOnly, boolean expectedWriteOnly) {
    new JacksonModule().applyToConfigBuilder(this.configBuilder);
    FieldScope field = new TestType(TestClassWithRequiredAnnotatedFields.class).getMemberField(fieldName);
    Assert.assertEquals(this.fieldConfigPart.isReadOnly(field), expectedReadOnly);
    Assert.assertEquals(this.fieldConfigPart.isWriteOnly(field), expectedWriteOnly);
}
Also used : FieldScope(com.github.victools.jsonschema.generator.FieldScope) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 5 with FieldScope

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

the class JacksonModuleTest method testDescriptionForTypeResolver.

@Test
@Parameters
public void testDescriptionForTypeResolver(String fieldName, String expectedDescription) throws Exception {
    new JacksonModule().applyToConfigBuilder(this.configBuilder);
    FieldScope field = new TestType(TestClassForDescription.class).getMemberField(fieldName);
    ArgumentCaptor<ConfigFunction<TypeScope, String>> captor = ArgumentCaptor.forClass(ConfigFunction.class);
    Mockito.verify(this.typesInGeneralConfigPart).withDescriptionResolver(captor.capture());
    String description = captor.getValue().apply(field);
    Assert.assertEquals(expectedDescription, description);
}
Also used : FieldScope(com.github.victools.jsonschema.generator.FieldScope) ConfigFunction(com.github.victools.jsonschema.generator.ConfigFunction) Parameters(junitparams.Parameters) Test(org.junit.Test)

Aggregations

FieldScope (com.github.victools.jsonschema.generator.FieldScope)41 Test (org.junit.Test)25 ConfigFunction (com.github.victools.jsonschema.generator.ConfigFunction)20 Parameters (junitparams.Parameters)19 AbstractTypeAwareTest (com.github.victools.jsonschema.generator.AbstractTypeAwareTest)11 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)10 MethodScope (com.github.victools.jsonschema.generator.MethodScope)5 ResolvedType (com.fasterxml.classmate.ResolvedType)4 CustomDefinition (com.github.victools.jsonschema.generator.CustomDefinition)4 CustomPropertyDefinition (com.github.victools.jsonschema.generator.CustomPropertyDefinition)4 BigInteger (java.math.BigInteger)4 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)3 BigDecimal (java.math.BigDecimal)3 TestCaseName (junitparams.naming.TestCaseName)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 MemberScope (com.github.victools.jsonschema.generator.MemberScope)2 SchemaGeneratorGeneralConfigPart (com.github.victools.jsonschema.generator.SchemaGeneratorGeneralConfigPart)2 TypeScope (com.github.victools.jsonschema.generator.TypeScope)2 JsonClassDescription (com.fasterxml.jackson.annotation.JsonClassDescription)1 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1