Search in sources :

Example 1 with SchemaType

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

the class ApplicationProcessor method createSchema.

private Schema createSchema(Schema schema, ApiContext context, ParameterizedType type, ExtensibleType clazz, Collection<ParameterizedInterfaceModel> classParameterizedTypes) {
    if (schema == null) {
        schema = new SchemaImpl();
    }
    SchemaType schemaType = ModelUtils.getSchemaType(type, context);
    // If the annotated element is the same type as the reference class, return a null schema
    if (schemaType == SchemaType.OBJECT && type.getType() != null && type.getType().equals(clazz)) {
        schema.setType(null);
        schema.setItems(null);
        return schema;
    }
    if (type.getType() == null) {
        ParameterizedInterfaceModel classParameterizedType = findParameterizedModelFromGenerics(clazz, classParameterizedTypes, type);
        String typeName = null;
        if (type.getTypeName() != null) {
            typeName = type.getTypeName();
        }
        if ((typeName == null || Object.class.getName().equals(typeName)) && classParameterizedType != null) {
            typeName = classParameterizedType.getRawInterfaceName();
        }
        schemaType = ModelUtils.getSchemaType(typeName, context);
        if (schema.getType() == null) {
            schema.setType(schemaType);
        }
        Schema containerSchema = schema;
        if (schemaType == SchemaType.ARRAY) {
            containerSchema = new SchemaImpl();
            schema.setItems(containerSchema);
        }
        if (classParameterizedType != null) {
            Collection<ParameterizedInterfaceModel> genericTypes = classParameterizedType.getParametizedTypes();
            if (genericTypes.isEmpty()) {
                if (insertObjectReference(context, containerSchema, classParameterizedType.getRawInterface(), classParameterizedType.getRawInterfaceName())) {
                    containerSchema.setType(null);
                    containerSchema.setItems(null);
                }
            } else if (classParameterizedType.getRawInterface() instanceof ClassModel) {
                visitSchemaClass(containerSchema, null, (ClassModel) classParameterizedType.getRawInterface(), genericTypes, context);
            } else {
                LOGGER.log(FINE, "Unrecognised schema {0} class found.", new Object[] { classParameterizedType.getRawInterface() });
            }
        } else if (!type.getParameterizedTypes().isEmpty()) {
            List<ParameterizedType> genericTypes = type.getParameterizedTypes();
            if (ModelUtils.isMap(typeName, context) && genericTypes.size() == 2) {
                createSchema(containerSchema, context, genericTypes.get(0), clazz, classParameterizedTypes);
                containerSchema = new SchemaImpl();
                schema.setAdditionalPropertiesSchema(containerSchema);
                createSchema(containerSchema, context, genericTypes.get(1), clazz, classParameterizedTypes);
            } else {
                createSchema(containerSchema, context, genericTypes.get(0), clazz, classParameterizedTypes);
            }
        } else {
            return createSchema(containerSchema, context, type);
        }
        return schema;
    }
    return createSchema(schema, context, type);
}
Also used : SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) ClassModel(org.glassfish.hk2.classmodel.reflect.ClassModel) ParameterizedInterfaceModel(org.glassfish.hk2.classmodel.reflect.ParameterizedInterfaceModel) Schema(org.eclipse.microprofile.openapi.models.media.Schema) List(java.util.List) ArrayList(java.util.ArrayList) SchemaType(org.eclipse.microprofile.openapi.models.media.Schema.SchemaType)

Example 2 with SchemaType

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

the class ApplicationProcessor method createSchema.

private Schema createSchema(Schema schema, ApiContext context, ParameterizedType type) {
    String typeName = type.getTypeName();
    List<ParameterizedType> genericTypes = type.getParameterizedTypes();
    SchemaType schemaType = ModelUtils.getSchemaType(type, context);
    if (schema == null) {
        schema = new SchemaImpl();
        schema.setType(schemaType);
    }
    // Set the subtype if it's an array (for example an array of ints)
    if (schemaType == SchemaType.ARRAY) {
        if (type.isArray()) {
            schemaType = ModelUtils.getSchemaType(type.getTypeName(), context);
            schema.setType(schemaType);
        } else if (!genericTypes.isEmpty()) {
            // should be something Iterable
            schema.setItems(createSchema(context, genericTypes.get(0)));
        }
    }
    // If the schema is an object, insert the reference
    if (schemaType == SchemaType.OBJECT) {
        if (insertObjectReference(context, schema, type.getType(), typeName)) {
            schema.setType(null);
            schema.setItems(null);
        }
    }
    if (type instanceof AnnotatedElement) {
        AnnotatedElement element = (AnnotatedElement) type;
        final AnnotationModel schemaAnnotation = element.getAnnotation(org.eclipse.microprofile.openapi.annotations.media.Schema.class.getName());
        if (schemaAnnotation != null) {
            SchemaImpl.merge(SchemaImpl.createInstance(schemaAnnotation, context), schema, false, context);
        }
    }
    return schema;
}
Also used : ParameterizedType(org.glassfish.hk2.classmodel.reflect.ParameterizedType) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) Schema(org.eclipse.microprofile.openapi.models.media.Schema) AnnotatedElement(org.glassfish.hk2.classmodel.reflect.AnnotatedElement) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) SchemaType(org.eclipse.microprofile.openapi.models.media.Schema.SchemaType)

Example 3 with SchemaType

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

the class ApplicationProcessor method visitFormParam.

@Override
public void visitFormParam(AnnotationModel param, AnnotatedElement element, ApiContext context) {
    // Find the aggregate schema type of all the parameters
    SchemaType formSchemaType = null;
    if (element instanceof org.glassfish.hk2.classmodel.reflect.Parameter) {
        List<org.glassfish.hk2.classmodel.reflect.Parameter> parameters = ((org.glassfish.hk2.classmodel.reflect.Parameter) element).getMethod().getParameters();
        for (org.glassfish.hk2.classmodel.reflect.Parameter methodParam : parameters) {
            if (methodParam.getAnnotation(FormParam.class.getName()) != null) {
                formSchemaType = ModelUtils.getParentSchemaType(formSchemaType, ModelUtils.getSchemaType(methodParam, context));
            }
        }
    }
    final Operation workingOperation = context.getWorkingOperation();
    if (workingOperation != null) {
        // If there's no request body, fill out a new one right down to the schema
        if (workingOperation.getRequestBody() == null) {
            workingOperation.setRequestBody(new RequestBodyImpl().content(new ContentImpl().addMediaType(javax.ws.rs.core.MediaType.WILDCARD, new MediaTypeImpl().schema(new SchemaImpl()))));
        }
        for (MediaType mediaType : workingOperation.getRequestBody().getContent().getMediaTypes().values()) {
            final Schema schema = mediaType.getSchema();
            if (schema != null) {
                schema.setType(formSchemaType);
            }
        }
    }
}
Also used : Schema(org.eclipse.microprofile.openapi.models.media.Schema) RequestBodyImpl(fish.payara.microprofile.openapi.impl.model.parameters.RequestBodyImpl) Operation(org.eclipse.microprofile.openapi.models.Operation) ContentImpl(fish.payara.microprofile.openapi.impl.model.media.ContentImpl) SchemaType(org.eclipse.microprofile.openapi.models.media.Schema.SchemaType) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) MediaTypeImpl(fish.payara.microprofile.openapi.impl.model.media.MediaTypeImpl) Parameter(org.eclipse.microprofile.openapi.models.parameters.Parameter) MediaType(org.eclipse.microprofile.openapi.models.media.MediaType)

Example 4 with SchemaType

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

the class ApplicationProcessor method visitParameter.

@Override
public void visitParameter(AnnotationModel annotation, AnnotatedElement element, ApiContext context) {
    Parameter matchedParam = null;
    Boolean hidden = annotation.getValue("hidden", Boolean.class);
    if (hidden != null && hidden) {
        return;
    }
    Parameter parameter = ParameterImpl.createInstance(annotation, context);
    if (element instanceof org.glassfish.hk2.classmodel.reflect.Parameter) {
        matchedParam = findOperationParameterFor((org.glassfish.hk2.classmodel.reflect.Parameter) element, context);
    }
    if (element instanceof MethodModel) {
        matchedParam = findOperationParameterFor(parameter, (MethodModel) element, context);
    }
    if (matchedParam != null) {
        ParameterImpl.merge(parameter, matchedParam, true, context);
        // If a content was added, and a schema type exists, reconfigure the schema type
        if (matchedParam.getContent() != null && !matchedParam.getContent().getMediaTypes().isEmpty() && matchedParam.getSchema() != null && matchedParam.getSchema().getType() != null) {
            SchemaType type = matchedParam.getSchema().getType();
            matchedParam.setSchema(null);
            for (MediaType mediaType : matchedParam.getContent().getMediaTypes().values()) {
                if (mediaType.getSchema() == null) {
                    mediaType.setSchema(new SchemaImpl());
                }
                mediaType.getSchema().setType(ModelUtils.mergeProperty(mediaType.getSchema().getType(), type, false));
            }
        }
    }
}
Also used : MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) Parameter(org.eclipse.microprofile.openapi.models.parameters.Parameter) MediaType(org.eclipse.microprofile.openapi.models.media.MediaType) SchemaType(org.eclipse.microprofile.openapi.models.media.Schema.SchemaType)

Aggregations

SchemaImpl (fish.payara.microprofile.openapi.impl.model.media.SchemaImpl)4 SchemaType (org.eclipse.microprofile.openapi.models.media.Schema.SchemaType)4 Schema (org.eclipse.microprofile.openapi.models.media.Schema)3 MediaType (org.eclipse.microprofile.openapi.models.media.MediaType)2 Parameter (org.eclipse.microprofile.openapi.models.parameters.Parameter)2 ContentImpl (fish.payara.microprofile.openapi.impl.model.media.ContentImpl)1 MediaTypeImpl (fish.payara.microprofile.openapi.impl.model.media.MediaTypeImpl)1 RequestBodyImpl (fish.payara.microprofile.openapi.impl.model.parameters.RequestBodyImpl)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Operation (org.eclipse.microprofile.openapi.models.Operation)1 AnnotatedElement (org.glassfish.hk2.classmodel.reflect.AnnotatedElement)1 AnnotationModel (org.glassfish.hk2.classmodel.reflect.AnnotationModel)1 ClassModel (org.glassfish.hk2.classmodel.reflect.ClassModel)1 MethodModel (org.glassfish.hk2.classmodel.reflect.MethodModel)1 ParameterizedInterfaceModel (org.glassfish.hk2.classmodel.reflect.ParameterizedInterfaceModel)1 ParameterizedType (org.glassfish.hk2.classmodel.reflect.ParameterizedType)1