Search in sources :

Example 26 with Schema

use of org.eclipse.microprofile.openapi.models.media.Schema in project Payara by payara.

the class ApplicationProcessor method visitSchemaFieldOrMethod.

private void visitSchemaFieldOrMethod(AnnotationModel schemaAnnotation, AnnotatedElement fieldOrMethod, ExtensibleType<?> declaringType, String typeName, ApiContext context) {
    assert (fieldOrMethod instanceof FieldModel) || (fieldOrMethod instanceof MethodModel);
    Boolean hidden = schemaAnnotation.getValue("hidden", Boolean.class);
    if (hidden == null || !hidden) {
        // Get the schema object name
        String schemaName = ModelUtils.getSchemaName(context, fieldOrMethod);
        SchemaImpl schema = SchemaImpl.createInstance(schemaAnnotation, context);
        // Get the parent schema object name
        String parentName = null;
        AnnotationModel classSchemaAnnotation = context.getAnnotationInfo(declaringType).getAnnotation(org.eclipse.microprofile.openapi.annotations.media.Schema.class);
        if (classSchemaAnnotation != null) {
            parentName = classSchemaAnnotation.getValue("name", String.class);
        }
        if (parentName == null || parentName.isEmpty()) {
            parentName = declaringType.getSimpleName();
        }
        // Get or create the parent schema object
        final Components components = context.getApi().getComponents();
        Schema parentSchema = components.getSchemas().getOrDefault(parentName, new SchemaImpl());
        components.addSchema(parentName, parentSchema);
        Schema property = parentSchema.getProperties().getOrDefault(schemaName, new SchemaImpl());
        parentSchema.addProperty(schemaName, property);
        if (schema.isRequired()) {
            parentSchema.addRequired(schemaName);
        }
        if (property.getRef() == null) {
            property.setType(ModelUtils.getSchemaType(typeName, context));
        }
        SchemaImpl.merge(schema, property, true, context);
    }
}
Also used : Components(org.eclipse.microprofile.openapi.models.Components) MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) Schema(org.eclipse.microprofile.openapi.models.media.Schema) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel)

Example 27 with Schema

use of org.eclipse.microprofile.openapi.models.media.Schema in project Payara by payara.

the class ApplicationProcessor method visitSchemaClass.

private Schema visitSchemaClass(Schema schema, AnnotationModel schemaAnnotation, ClassModel clazz, Collection<ParameterizedInterfaceModel> parameterizedInterfaces, ApiContext context) {
    // Get the schema object name
    String schemaName = ModelUtils.getSchemaName(context, clazz);
    // Add a new schema
    if (schema == null) {
        final Components components = context.getApi().getComponents();
        schema = components.getSchemas().getOrDefault(schemaName, new SchemaImpl());
        components.addSchema(schemaName, schema);
    }
    // If there is an annotation, parse its configuration
    if (schemaAnnotation != null) {
        SchemaImpl.merge(SchemaImpl.createInstance(schemaAnnotation, context), schema, false, context);
    }
    for (FieldModel field : clazz.getFields()) {
        final String fieldName = field.getName();
        Boolean hidden = false;
        AnnotationModel fieldSchemaAnnotation = field.getAnnotation(org.eclipse.microprofile.openapi.annotations.media.Schema.class.getName());
        if (fieldSchemaAnnotation != null) {
            hidden = fieldSchemaAnnotation.getValue("hidden", Boolean.class);
        }
        if (!Boolean.TRUE.equals(hidden) && !field.isTransient() && !fieldName.startsWith("this$")) {
            final Schema existingProperty = schema.getProperties().get(fieldName);
            final Schema newProperty = createSchema(null, context, field, clazz, parameterizedInterfaces);
            if (existingProperty != null) {
                SchemaImpl.merge(existingProperty, newProperty, true, context);
            }
            schema.addProperty(fieldName, newProperty);
        }
    }
    if (schema.getType() == null) {
        schema.setType(ModelUtils.getSchemaType(clazz.getName(), context));
    }
    // If there is an extending class, add the data
    final ClassModel superClass = clazz.getParent();
    if (superClass != null && !superClass.getName().startsWith("java.")) {
        // Get the parent annotation
        AnnotationModel parentSchemAnnotation = context.getAnnotationInfo(superClass).getAnnotation(org.eclipse.microprofile.openapi.annotations.media.Schema.class);
        ParameterizedInterfaceModel parameterizedInterface = clazz.getParameterizedInterface(superClass);
        if (parameterizedInterface == null) {
            // Create a schema for the parent
            final Schema parentSchema = visitSchemaClass(null, parentSchemAnnotation, superClass, Collections.emptyList(), context);
            // Get the superclass schema name
            String parentSchemaName = ModelUtils.getSchemaName(context, superClass);
            // Link the schemas
            schema.addAllOf(new SchemaImpl().ref(parentSchemaName));
            // Add all the parent schema properties
            for (Entry<String, Schema> property : parentSchema.getProperties().entrySet()) {
                schema.addProperty(property.getKey(), property.getValue());
            }
        } else {
            visitSchemaClass(schema, parentSchemAnnotation, superClass, parameterizedInterface.getParametizedTypes(), context);
        }
    }
    return schema;
}
Also used : Schema(org.eclipse.microprofile.openapi.models.media.Schema) Components(org.eclipse.microprofile.openapi.models.Components) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) ClassModel(org.glassfish.hk2.classmodel.reflect.ClassModel) ParameterizedInterfaceModel(org.glassfish.hk2.classmodel.reflect.ParameterizedInterfaceModel) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel)

Example 28 with Schema

use of org.eclipse.microprofile.openapi.models.media.Schema in project Payara by payara.

the class ApplicationProcessor method vistEnumClass.

private void vistEnumClass(AnnotationModel schemaAnnotation, EnumType enumType, ApiContext context) {
    // Get the schema object name
    String schemaName = ModelUtils.getSchemaName(context, enumType);
    Schema schema = SchemaImpl.createInstance(schemaAnnotation, context);
    Schema newSchema = new SchemaImpl();
    context.getApi().getComponents().addSchema(schemaName, newSchema);
    if (schema != null) {
        SchemaImpl.merge(schema, newSchema, true, context);
    }
    if (schema == null || schema.getEnumeration() == null || schema.getEnumeration().isEmpty()) {
        // if the schema annotation does not specify enums, then all enum fields will be added
        for (FieldModel enumField : enumType.getStaticFields()) {
            final String enumValue = enumField.getName();
            if (!enumValue.contains("$VALUES")) {
                newSchema.addEnumeration(enumValue);
            }
        }
    }
}
Also used : SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) Schema(org.eclipse.microprofile.openapi.models.media.Schema) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel)

Example 29 with Schema

use of org.eclipse.microprofile.openapi.models.media.Schema in project Payara by payara.

the class ApplicationProcessor method visitSchemaParameter.

private static void visitSchemaParameter(AnnotationModel schemaAnnotation, org.glassfish.hk2.classmodel.reflect.Parameter parameter, ApiContext context) {
    // If this is being parsed at the start, ignore it as the path doesn't exist
    if (context.getWorkingOperation() == null) {
        return;
    }
    // Check if it's a request body
    if (ModelUtils.isRequestBody(context, parameter)) {
        if (context.getWorkingOperation().getRequestBody() == null) {
            context.getWorkingOperation().setRequestBody(new RequestBodyImpl());
        }
        // Insert the schema to the request body media type
        MediaType mediaType = context.getWorkingOperation().getRequestBody().getContent().getMediaType(javax.ws.rs.core.MediaType.WILDCARD);
        Schema schema = SchemaImpl.createInstance(schemaAnnotation, context);
        SchemaImpl.merge(schema, mediaType.getSchema(), true, context);
        if (schema.getRef() != null && !schema.getRef().isEmpty()) {
            mediaType.setSchema(new SchemaImpl().ref(schema.getRef()));
        }
    } else if (ModelUtils.getParameterType(context, parameter) != null) {
        for (Parameter param : context.getWorkingOperation().getParameters()) {
            if (param.getName().equals(ModelUtils.getParameterName(context, parameter))) {
                Schema schema = SchemaImpl.createInstance(schemaAnnotation, context);
                SchemaImpl.merge(schema, param.getSchema(), true, context);
                if (schema.getRef() != null && !schema.getRef().isEmpty()) {
                    param.setSchema(new SchemaImpl().ref(schema.getRef()));
                }
            }
        }
    }
}
Also used : SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) Schema(org.eclipse.microprofile.openapi.models.media.Schema) MediaType(org.eclipse.microprofile.openapi.models.media.MediaType) Parameter(org.eclipse.microprofile.openapi.models.parameters.Parameter) RequestBodyImpl(fish.payara.microprofile.openapi.impl.model.parameters.RequestBodyImpl)

Example 30 with Schema

use of org.eclipse.microprofile.openapi.models.media.Schema in project Payara by payara.

the class SchemaImpl method setImplementation.

private static void setImplementation(SchemaImpl schema, String implementationClass, boolean override, ApiContext context) {
    if (context.getApi().getComponents().getSchemas() != null) {
        if (schema instanceof SchemaImpl) {
            schema.setImplementation(mergeProperty(((SchemaImpl) schema).getImplementation(), implementationClass, override));
        }
        if (implementationClass.endsWith("[]")) {
            implementationClass = implementationClass.substring(0, implementationClass.length() - 2);
            final SchemaImpl itemSchema = new SchemaImpl();
            schema.setItems(itemSchema);
            schema.setType(SchemaType.ARRAY);
            schema = itemSchema;
        }
        if (!implementationClass.equals("java.lang.Void")) {
            Type type = context.getType(implementationClass);
            String schemaName;
            if (type != null) {
                schemaName = ModelUtils.getSchemaName(context, type);
            } else {
                schemaName = ModelUtils.getSimpleName(implementationClass);
            }
            // Get the schema reference, and copy it's values over to the new schema model if they are missing
            Schema copyFrom = context.getApi().getComponents().getSchemas().get(schemaName);
            if (copyFrom == null) {
                // If the class hasn't been parsed
                SchemaType schemaType = ModelUtils.getSchemaType(implementationClass, context);
                copyFrom = new SchemaImpl().type(schemaType);
            }
            if (schema.getType() == SchemaType.ARRAY) {
                schema.setItems(new SchemaImpl());
                ModelUtils.merge(copyFrom, schema.getItems(), false);
            } else {
                ModelUtils.merge(copyFrom, schema, false);
            }
            schema.setRef(null);
        }
    }
}
Also used : Type(org.glassfish.hk2.classmodel.reflect.Type) Schema(org.eclipse.microprofile.openapi.models.media.Schema)

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