Search in sources :

Example 1 with MemberScope

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

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

the class SchemaGenerationContextImpl method generateObjectDefinition.

/**
 * Preparation Step: add the given targetType (which was previously determined to be anything but an array type).
 *
 * @param targetType object type to add
 * @param definition node in the JSON schema to which all collected attributes should be added
 */
private void generateObjectDefinition(ResolvedType targetType, ObjectNode definition) {
    definition.put(this.getKeyword(SchemaKeyword.TAG_TYPE), this.getKeyword(SchemaKeyword.TAG_TYPE_OBJECT));
    /*
         * Collecting fields and methods according to their order in the Java Byte Code.
         * Depending on the compiler implementation, that order may or may not match the declaration order in the source code.
         */
    final Map<String, MemberScope<?, ?>> targetProperties = new LinkedHashMap<>();
    final Set<String> requiredProperties = new HashSet<>();
    this.collectObjectProperties(targetType, targetProperties, requiredProperties);
    if (!targetProperties.isEmpty()) {
        ObjectNode propertiesNode = this.generatorConfig.createObjectNode();
        List<MemberScope<?, ?>> sortedProperties = targetProperties.values().stream().sorted(this.generatorConfig::sortProperties).collect(Collectors.toList());
        for (MemberScope<?, ?> property : sortedProperties) {
            JsonNode subSchema;
            if (property instanceof FieldScope) {
                subSchema = this.populateFieldSchema((FieldScope) property);
            } else if (property instanceof MethodScope) {
                subSchema = this.populateMethodSchema((MethodScope) property);
            } else {
                throw new IllegalStateException("Unsupported member scope of type " + property.getClass());
            }
            propertiesNode.set(property.getSchemaPropertyName(), subSchema);
        }
        definition.set(this.getKeyword(SchemaKeyword.TAG_PROPERTIES), propertiesNode);
        if (!requiredProperties.isEmpty()) {
            ArrayNode requiredNode = this.generatorConfig.createArrayNode();
            // list required properties in the same order as the property
            sortedProperties.stream().map(MemberScope::getSchemaPropertyName).filter(requiredProperties::contains).forEach(requiredNode::add);
            definition.set(this.getKeyword(SchemaKeyword.TAG_REQUIRED), requiredNode);
        }
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) MethodScope(com.github.victools.jsonschema.generator.MethodScope) MemberScope(com.github.victools.jsonschema.generator.MemberScope) LinkedHashMap(java.util.LinkedHashMap) FieldScope(com.github.victools.jsonschema.generator.FieldScope) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) HashSet(java.util.HashSet)

Example 3 with MemberScope

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

the class Swagger2Module method overrideInstanceAttributes.

/**
 * Consider various remaining aspects.
 * <ul>
 * <li>{@code @Schema(not = ...)}</li>
 * <li>{@code @Schema(allOf = ...)}</li>
 * <li>{@code @Schema(minProperties = ...)}</li>
 * <li>{@code @Schema(maxProperties = ...)}</li>
 * <li>{@code @Schema(requiredProperties = ...)}</li>
 * </ul>
 *
 * @param memberAttributes already collected schema for the field/method
 * @param member targeted field/method
 * @param context generation context
 */
protected void overrideInstanceAttributes(ObjectNode memberAttributes, MemberScope<?, ?> member, SchemaGenerationContext context) {
    Schema annotation = this.getSchemaAnnotationValue(member, Function.identity(), x -> true).orElse(null);
    if (annotation == null) {
        return;
    }
    if (annotation.not() != Void.class) {
        memberAttributes.set(context.getKeyword(SchemaKeyword.TAG_NOT), context.createDefinitionReference(context.getTypeContext().resolve(annotation.not())));
    }
    if (annotation.allOf().length > 0) {
        Stream.of(annotation.allOf()).map(rawType -> context.getTypeContext().resolve(rawType)).map(context::createDefinitionReference).forEach(memberAttributes.withArray(context.getKeyword(SchemaKeyword.TAG_ALLOF))::add);
    }
    if (annotation.minProperties() > 0) {
        memberAttributes.put(context.getKeyword(SchemaKeyword.TAG_PROPERTIES_MIN), annotation.minProperties());
    }
    if (annotation.maxProperties() > 0) {
        memberAttributes.put(context.getKeyword(SchemaKeyword.TAG_PROPERTIES_MAX), annotation.maxProperties());
    }
    if (annotation.requiredProperties().length > 0) {
        Set<String> alreadyMentionedRequiredFields = new HashSet<>();
        ArrayNode requiredFieldNames = memberAttributes.withArray(context.getKeyword(SchemaKeyword.TAG_REQUIRED));
        requiredFieldNames.forEach(arrayItem -> alreadyMentionedRequiredFields.add(arrayItem.asText()));
        Stream.of(annotation.requiredProperties()).filter(field -> !alreadyMentionedRequiredFields.contains(field)).forEach(requiredFieldNames::add);
    }
}
Also used : Arrays(java.util.Arrays) MemberScope(com.github.victools.jsonschema.generator.MemberScope) SchemaGeneratorGeneralConfigPart(com.github.victools.jsonschema.generator.SchemaGeneratorGeneralConfigPart) SchemaGeneratorConfigBuilder(com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder) ResolvedType(com.fasterxml.classmate.ResolvedType) Function(java.util.function.Function) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HashSet(java.util.HashSet) Module(com.github.victools.jsonschema.generator.Module) BigDecimal(java.math.BigDecimal) CustomDefinition(com.github.victools.jsonschema.generator.CustomDefinition) ConfigFunction(com.github.victools.jsonschema.generator.ConfigFunction) Schema(io.swagger.v3.oas.annotations.media.Schema) SchemaGeneratorConfigPart(com.github.victools.jsonschema.generator.SchemaGeneratorConfigPart) Predicate(java.util.function.Predicate) SchemaGenerationContext(com.github.victools.jsonschema.generator.SchemaGenerationContext) Set(java.util.Set) TypeScope(com.github.victools.jsonschema.generator.TypeScope) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ArraySchema(io.swagger.v3.oas.annotations.media.ArraySchema) List(java.util.List) Stream(java.util.stream.Stream) CustomPropertyDefinition(com.github.victools.jsonschema.generator.CustomPropertyDefinition) Optional(java.util.Optional) Collections(java.util.Collections) SchemaKeyword(com.github.victools.jsonschema.generator.SchemaKeyword) Schema(io.swagger.v3.oas.annotations.media.Schema) ArraySchema(io.swagger.v3.oas.annotations.media.ArraySchema) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) HashSet(java.util.HashSet)

Aggregations

ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 MemberScope (com.github.victools.jsonschema.generator.MemberScope)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 FieldScope (com.github.victools.jsonschema.generator.FieldScope)2 MethodScope (com.github.victools.jsonschema.generator.MethodScope)2 HashSet (java.util.HashSet)2 ResolvedType (com.fasterxml.classmate.ResolvedType)1 ConfigFunction (com.github.victools.jsonschema.generator.ConfigFunction)1 CustomDefinition (com.github.victools.jsonschema.generator.CustomDefinition)1 CustomPropertyDefinition (com.github.victools.jsonschema.generator.CustomPropertyDefinition)1 Module (com.github.victools.jsonschema.generator.Module)1 SchemaGenerationContext (com.github.victools.jsonschema.generator.SchemaGenerationContext)1 SchemaGeneratorConfigBuilder (com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder)1 SchemaGeneratorConfigPart (com.github.victools.jsonschema.generator.SchemaGeneratorConfigPart)1 SchemaGeneratorGeneralConfigPart (com.github.victools.jsonschema.generator.SchemaGeneratorGeneralConfigPart)1 SchemaKeyword (com.github.victools.jsonschema.generator.SchemaKeyword)1 TypeScope (com.github.victools.jsonschema.generator.TypeScope)1 ArraySchema (io.swagger.v3.oas.annotations.media.ArraySchema)1 Schema (io.swagger.v3.oas.annotations.media.Schema)1