Search in sources :

Example 41 with Schema

use of org.eclipse.microprofile.openapi.models.media.Schema 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 42 with Schema

use of org.eclipse.microprofile.openapi.models.media.Schema in project wildfly-swarm by wildfly-swarm.

the class OpenApiParser method readSchemas.

/**
 * Reads the {@link Schema} OpenAPI nodes.
 * @param node
 */
private Map<String, Schema> readSchemas(JsonNode node) {
    if (node == null || !node.isObject()) {
        return null;
    }
    Map<String, Schema> models = new LinkedHashMap<>();
    for (Iterator<String> fieldNames = node.fieldNames(); fieldNames.hasNext(); ) {
        String fieldName = fieldNames.next();
        JsonNode childNode = node.get(fieldName);
        models.put(fieldName, readSchema(childNode));
    }
    return models;
}
Also used : Schema(org.eclipse.microprofile.openapi.models.media.Schema) JsonNode(com.fasterxml.jackson.databind.JsonNode) LinkedHashMap(java.util.LinkedHashMap)

Example 43 with Schema

use of org.eclipse.microprofile.openapi.models.media.Schema in project wildfly-swarm by wildfly-swarm.

the class OpenApiDataObjectScanner method dfs.

// Scan depth first.
private void dfs(PathEntry rootNode) {
    PathEntry currentPathEntry = rootNode;
    while (!path.isEmpty()) {
        ClassInfo currentClass = currentPathEntry.clazz;
        Schema currentSchema = currentPathEntry.schema;
        Type currentType = currentPathEntry.clazzType;
        // First, handle class annotations.
        currentSchema = readKlass(currentClass, currentSchema);
        LOG.debugv("Getting all fields for: {0} in class: {1}", currentType, currentClass);
        // Get all fields *including* inherited.
        Map<FieldInfo, TypeResolver> allFields = TypeResolver.getAllFields(index, currentType, currentClass);
        // Handle fields
        for (Map.Entry<FieldInfo, TypeResolver> entry : allFields.entrySet()) {
            FieldInfo field = entry.getKey();
            TypeResolver resolver = entry.getValue();
            if (!Modifier.isStatic(field.flags())) {
                LOG.tracev("Iterating field {0}", field);
                processField(field, resolver, currentSchema, currentPathEntry);
            }
        }
        currentPathEntry = path.pop();
    // Handle methods
    // TODO put it here!
    }
}
Also used : PrimitiveType(org.jboss.jandex.PrimitiveType) Type(org.jboss.jandex.Type) ParameterizedType(org.jboss.jandex.ParameterizedType) ArrayType(org.jboss.jandex.ArrayType) Schema(org.eclipse.microprofile.openapi.models.media.Schema) HashMap(java.util.HashMap) Map(java.util.Map) FieldInfo(org.jboss.jandex.FieldInfo) ClassInfo(org.jboss.jandex.ClassInfo)

Example 44 with Schema

use of org.eclipse.microprofile.openapi.models.media.Schema 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)

Example 45 with Schema

use of org.eclipse.microprofile.openapi.models.media.Schema in project wildfly-swarm by wildfly-swarm.

the class SchemaFactory method readClassSchemas.

private static List<Schema> readClassSchemas(IndexView index, AnnotationValue value) {
    if (value == null) {
        return null;
    }
    Type[] classArray = value.asClassArray();
    List<Schema> schemas = new ArrayList<>(classArray.length);
    for (Type type : classArray) {
        ClassType ctype = (ClassType) type;
        Schema schema = introspectClassToSchema(index, ctype);
        schemas.add(schema);
    }
    return schemas;
}
Also used : Type(org.jboss.jandex.Type) ClassType(org.jboss.jandex.ClassType) Schema(org.eclipse.microprofile.openapi.models.media.Schema) ArrayList(java.util.ArrayList) ClassType(org.jboss.jandex.ClassType)

Aggregations

Schema (org.eclipse.microprofile.openapi.models.media.Schema)51 Type (org.jboss.jandex.Type)15 Test (org.junit.Test)15 SchemaImpl (fish.payara.microprofile.openapi.impl.model.media.SchemaImpl)10 ClassType (org.jboss.jandex.ClassType)10 MediaType (org.eclipse.microprofile.openapi.models.media.MediaType)7 ArrayList (java.util.ArrayList)5 Parameter (org.eclipse.microprofile.openapi.models.parameters.Parameter)5 AnnotationModel (org.glassfish.hk2.classmodel.reflect.AnnotationModel)5 SchemaImpl (org.wildfly.swarm.microprofile.openapi.api.models.media.SchemaImpl)5 RequestBodyImpl (fish.payara.microprofile.openapi.impl.model.parameters.RequestBodyImpl)4 LinkedHashMap (java.util.LinkedHashMap)4 List (java.util.List)4 DMNType (org.kie.dmn.api.core.DMNType)4 Components (org.eclipse.microprofile.openapi.models.Components)3 Operation (org.eclipse.microprofile.openapi.models.Operation)3 Callback (org.eclipse.microprofile.openapi.models.callbacks.Callback)3 SchemaType (org.eclipse.microprofile.openapi.models.media.Schema.SchemaType)3 APIResponse (org.eclipse.microprofile.openapi.models.responses.APIResponse)3 APIResponses (org.eclipse.microprofile.openapi.models.responses.APIResponses)3