Search in sources :

Example 6 with SchemaImpl

use of org.wildfly.swarm.microprofile.openapi.api.models.media.SchemaImpl in project wildfly-swarm by wildfly-swarm.

the class OpenApiDataObjectScanner method preProcessSpecial.

private Schema preProcessSpecial(Type type, TypeResolver typeResolver, Schema parentSchema, PathEntry currentPathEntry) {
    Schema fieldSchema = new SchemaImpl();
    AnnotationInstance schemaAnno = TypeUtil.getSchemaAnnotation(type);
    if (schemaAnno != null) {
        // 1. Handle field annotated with @Schema.
        return readSchemaAnnotatedField(schemaAnno, type.name().toString(), type, typeResolver, parentSchema, fieldSchema, currentPathEntry);
    } else {
        // 2. Handle unannotated field and just do simple inference.
        readUnannotatedField(typeResolver, type, fieldSchema, currentPathEntry);
        // Unannotated won't result in substitution, so just return field schema.
        return fieldSchema;
    }
}
Also used : SchemaImpl(org.wildfly.swarm.microprofile.openapi.api.models.media.SchemaImpl) Schema(org.eclipse.microprofile.openapi.models.media.Schema) AnnotationInstance(org.jboss.jandex.AnnotationInstance)

Example 7 with SchemaImpl

use of org.wildfly.swarm.microprofile.openapi.api.models.media.SchemaImpl in project wildfly-swarm by wildfly-swarm.

the class OpenApiAnnotationScanner method introspectClassToSchema.

/**
 * Introspects the given class type to generate a Schema model.  The boolean indicates
 * whether this class type should be turned into a reference.
 * @param ctype
 * @param schemaReferenceSupported
 */
private Schema introspectClassToSchema(ClassType ctype, boolean schemaReferenceSupported) {
    if (ctype.name().equals(OpenApiConstants.DOTNAME_RESPONSE)) {
        return null;
    }
    if (schemaReferenceSupported && this.schemaRegistry.has(ctype)) {
        GeneratedSchemaInfo schemaInfo = this.schemaRegistry.lookup(ctype);
        Schema rval = new SchemaImpl();
        rval.setRef(schemaInfo.$ref);
        return rval;
    } else {
        Schema schema = OpenApiDataObjectScanner.process(index, ctype);
        if (schemaReferenceSupported && schema != null && this.index.getClassByName(ctype.name()) != null) {
            GeneratedSchemaInfo schemaInfo = this.schemaRegistry.register(ctype, schema);
            ModelUtil.components(oai).addSchema(schemaInfo.name, schema);
            Schema rval = new SchemaImpl();
            rval.setRef(schemaInfo.$ref);
            return rval;
        } else {
            return schema;
        }
    }
}
Also used : SchemaImpl(org.wildfly.swarm.microprofile.openapi.api.models.media.SchemaImpl) Schema(org.eclipse.microprofile.openapi.models.media.Schema)

Example 8 with SchemaImpl

use of org.wildfly.swarm.microprofile.openapi.api.models.media.SchemaImpl in project wildfly-swarm by wildfly-swarm.

the class OpenApiAnnotationScanner method readSchema.

/**
 * Reads a Schema annotation into a model.
 * @param annotation
 */
@SuppressWarnings("unchecked")
private Schema readSchema(AnnotationInstance annotation) {
    if (annotation == null) {
        return null;
    }
    LOG.debug("Processing a single @Schema annotation.");
    // Schemas can be hidden. Skip if that's the case.
    Boolean isHidden = JandexUtil.booleanValue(annotation, OpenApiConstants.PROP_HIDDEN);
    if (isHidden != null && isHidden == Boolean.TRUE) {
        return null;
    }
    Schema schema = new SchemaImpl();
    schema.setNot(readClassSchema(annotation.value(OpenApiConstants.PROP_NOT), true));
    schema.setOneOf(readClassSchemas(annotation.value(OpenApiConstants.PROP_ONE_OF)));
    schema.setAnyOf(readClassSchemas(annotation.value(OpenApiConstants.PROP_ANY_OF)));
    schema.setAllOf(readClassSchemas(annotation.value(OpenApiConstants.PROP_ALL_OF)));
    schema.setTitle(JandexUtil.stringValue(annotation, OpenApiConstants.PROP_TITLE));
    schema.setMultipleOf(JandexUtil.bigDecimalValue(annotation, OpenApiConstants.PROP_MULTIPLE_OF));
    schema.setMaximum(JandexUtil.bigDecimalValue(annotation, OpenApiConstants.PROP_MAXIMUM));
    schema.setExclusiveMaximum(JandexUtil.booleanValue(annotation, OpenApiConstants.PROP_EXCLUSIVE_MAXIMUM));
    schema.setMinimum(JandexUtil.bigDecimalValue(annotation, OpenApiConstants.PROP_MINIMUM));
    schema.setExclusiveMinimum(JandexUtil.booleanValue(annotation, OpenApiConstants.PROP_EXCLUSIVE_MINIMUM));
    schema.setMaxLength(JandexUtil.intValue(annotation, OpenApiConstants.PROP_MAX_LENGTH));
    schema.setMinLength(JandexUtil.intValue(annotation, OpenApiConstants.PROP_MIN_LENGTH));
    schema.setPattern(JandexUtil.stringValue(annotation, OpenApiConstants.PROP_PATTERN));
    schema.setMaxProperties(JandexUtil.intValue(annotation, OpenApiConstants.PROP_MAX_PROPERTIES));
    schema.setMinProperties(JandexUtil.intValue(annotation, OpenApiConstants.PROP_MIN_PROPERTIES));
    schema.setRequired(JandexUtil.stringListValue(annotation, OpenApiConstants.PROP_REQUIRED_PROPERTIES));
    schema.setDescription(JandexUtil.stringValue(annotation, OpenApiConstants.PROP_DESCRIPTION));
    schema.setFormat(JandexUtil.stringValue(annotation, OpenApiConstants.PROP_FORMAT));
    schema.setRef(JandexUtil.refValue(annotation, RefType.Schema));
    schema.setNullable(JandexUtil.booleanValue(annotation, OpenApiConstants.PROP_NULLABLE));
    schema.setReadOnly(JandexUtil.booleanValue(annotation, OpenApiConstants.PROP_READ_ONLY));
    schema.setWriteOnly(JandexUtil.booleanValue(annotation, OpenApiConstants.PROP_WRITE_ONLY));
    schema.setExample(JandexUtil.stringValue(annotation, OpenApiConstants.PROP_EXAMPLE));
    schema.setExternalDocs(readExternalDocs(annotation.value(OpenApiConstants.PROP_EXTERNAL_DOCS)));
    schema.setDeprecated(JandexUtil.booleanValue(annotation, OpenApiConstants.PROP_DEPRECATED));
    schema.setType(JandexUtil.enumValue(annotation, OpenApiConstants.PROP_TYPE, org.eclipse.microprofile.openapi.models.media.Schema.SchemaType.class));
    schema.setEnumeration((List<Object>) (Object) JandexUtil.stringListValue(annotation, OpenApiConstants.PROP_ENUMERATION));
    schema.setDefaultValue(JandexUtil.stringValue(annotation, OpenApiConstants.PROP_DEFAULT_VALUE));
    schema.setDiscriminator(readDiscriminatorMappings(annotation.value(OpenApiConstants.PROP_DISCRIMINATOR_MAPPING)));
    schema.setMaxItems(JandexUtil.intValue(annotation, OpenApiConstants.PROP_MAX_ITEMS));
    schema.setMinItems(JandexUtil.intValue(annotation, OpenApiConstants.PROP_MIN_ITEMS));
    schema.setUniqueItems(JandexUtil.booleanValue(annotation, OpenApiConstants.PROP_UNIQUE_ITEMS));
    if (JandexUtil.isSimpleClassSchema(annotation)) {
        Schema implSchema = readClassSchema(annotation.value(OpenApiConstants.PROP_IMPLEMENTATION), true);
        schema = MergeUtil.mergeObjects(implSchema, schema);
    } else if (JandexUtil.isSimpleArraySchema(annotation)) {
        Schema implSchema = readClassSchema(annotation.value(OpenApiConstants.PROP_IMPLEMENTATION), true);
        // If the @Schema annotation indicates an array type, then use the Schema
        // generated from the implementation Class as the "items" for the array.
        schema.setItems(implSchema);
    } else {
        Schema implSchema = readClassSchema(annotation.value(OpenApiConstants.PROP_IMPLEMENTATION), false);
        // If there is an impl class - merge the @Schema properties *onto* the schema
        // generated from the Class so that the annotation properties override the class
        // properties (as required by the MP+OAI spec).
        schema = MergeUtil.mergeObjects(implSchema, schema);
    }
    return schema;
}
Also used : SchemaImpl(org.wildfly.swarm.microprofile.openapi.api.models.media.SchemaImpl) Schema(org.eclipse.microprofile.openapi.models.media.Schema)

Example 9 with SchemaImpl

use of org.wildfly.swarm.microprofile.openapi.api.models.media.SchemaImpl in project wildfly-swarm by wildfly-swarm.

the class OpenApiDataObjectScanner method processField.

private Schema processField(FieldInfo field, TypeResolver typeResolver, Schema parentSchema, PathEntry currentPathEntry) {
    Schema fieldSchema = new SchemaImpl();
    parentSchema.addProperty(field.name(), fieldSchema);
    AnnotationInstance schemaAnno = TypeUtil.getSchemaAnnotation(field);
    if (schemaAnno != null) {
        // 1. Handle field annotated with @Schema.
        return readSchemaAnnotatedField(schemaAnno, field.name(), field.type(), typeResolver, parentSchema, fieldSchema, currentPathEntry);
    } else {
        // 2. Handle unannotated field and just do simple inference.
        readUnannotatedField(typeResolver, field.type(), fieldSchema, currentPathEntry);
        // Unannotated won't result in substitution, so just return field schema.
        return fieldSchema;
    }
}
Also used : SchemaImpl(org.wildfly.swarm.microprofile.openapi.api.models.media.SchemaImpl) Schema(org.eclipse.microprofile.openapi.models.media.Schema) AnnotationInstance(org.jboss.jandex.AnnotationInstance)

Aggregations

SchemaImpl (org.wildfly.swarm.microprofile.openapi.api.models.media.SchemaImpl)9 Schema (org.eclipse.microprofile.openapi.models.media.Schema)5 TypeUtil (org.wildfly.swarm.microprofile.openapi.runtime.util.TypeUtil)3 AnnotationInstance (org.jboss.jandex.AnnotationInstance)2 ArrayType (org.jboss.jandex.ArrayType)2 ClassInfo (org.jboss.jandex.ClassInfo)2 FieldInfo (org.jboss.jandex.FieldInfo)1 ParameterizedType (org.jboss.jandex.ParameterizedType)1 PrimitiveType (org.jboss.jandex.PrimitiveType)1 Type (org.jboss.jandex.Type)1