Search in sources :

Example 1 with SchemaImpl

use of fish.payara.microprofile.openapi.impl.model.media.SchemaImpl in project Payara by payara.

the class ParameterImpl method merge.

public static void merge(Parameter from, Parameter to, boolean override, ApiContext context) {
    if (from == null) {
        return;
    }
    if (from.getRef() != null && !from.getRef().isEmpty()) {
        applyReference(to, from.getRef());
        return;
    }
    to.setName(mergeProperty(to.getName(), from.getName(), override));
    to.setDescription(mergeProperty(to.getDescription(), from.getDescription(), override));
    if (from.getIn() != null) {
        to.setIn(mergeProperty(to.getIn(), from.getIn(), override));
    }
    to.setRequired(mergeProperty(to.getRequired(), from.getRequired(), override));
    to.setDeprecated(mergeProperty(to.getDeprecated(), from.getDeprecated(), override));
    to.setAllowEmptyValue(mergeProperty(to.getAllowEmptyValue(), from.getAllowEmptyValue(), override));
    if (from.getStyle() != null) {
        to.setStyle(mergeProperty(to.getStyle(), from.getStyle(), override));
    }
    if (from.getExplode() != null) {
        to.setExplode(mergeProperty(to.getExplode(), false, override));
    }
    to.setAllowReserved(mergeProperty(to.getAllowReserved(), from.getAllowReserved(), override));
    if (from.getSchema() != null) {
        if (to.getSchema() == null) {
            to.setSchema(new SchemaImpl());
        }
        SchemaImpl.merge(from.getSchema(), to.getSchema(), override, context);
    }
    to.setExample(mergeProperty(to.getExample(), from.getExample(), override));
    if (from.getExamples() != null) {
        for (String exampleName : from.getExamples().keySet()) {
            if (exampleName != null) {
                Example example = new ExampleImpl();
                ExampleImpl.merge(from.getExamples().get(exampleName), example, override);
                to.addExample(exampleName, example);
            }
        }
    }
    if (from instanceof ParameterImpl) {
        ParameterImpl fromImpl = (ParameterImpl) from;
        if (fromImpl.getContents() != null) {
            if (to.getContent() == null) {
                to.setContent(new ContentImpl());
            }
            for (ContentImpl content : fromImpl.getContents()) {
                ContentImpl.merge(content, to.getContent(), override, context);
            }
        }
    }
    if (from.getContent() != null) {
        if (to.getContent() == null) {
            to.setContent(new ContentImpl());
        }
        ContentImpl.merge((ContentImpl) from.getContent(), to.getContent(), override, context);
    }
}
Also used : SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) Example(org.eclipse.microprofile.openapi.models.examples.Example) ContentImpl(fish.payara.microprofile.openapi.impl.model.media.ContentImpl) ExampleImpl(fish.payara.microprofile.openapi.impl.model.examples.ExampleImpl)

Example 2 with SchemaImpl

use of fish.payara.microprofile.openapi.impl.model.media.SchemaImpl 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 3 with SchemaImpl

use of fish.payara.microprofile.openapi.impl.model.media.SchemaImpl in project Payara by payara.

the class ApplicationProcessor method getArraySchema.

private static SchemaImpl getArraySchema(AnnotatedElement element, ApiContext context) {
    SchemaImpl arraySchema = new SchemaImpl();
    List<ParameterizedType> parameterizedType;
    if (element instanceof org.glassfish.hk2.classmodel.reflect.Parameter) {
        org.glassfish.hk2.classmodel.reflect.Parameter parameter = (org.glassfish.hk2.classmodel.reflect.Parameter) element;
        parameterizedType = parameter.getParameterizedTypes();
    } else {
        FieldModel field = (FieldModel) element;
        parameterizedType = field.getParameterizedTypes();
    }
    arraySchema.setType(ModelUtils.getSchemaType(parameterizedType.get(0).getTypeName(), context));
    return arraySchema;
}
Also used : ParameterizedType(org.glassfish.hk2.classmodel.reflect.ParameterizedType) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) Parameter(org.eclipse.microprofile.openapi.models.parameters.Parameter) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel)

Example 4 with SchemaImpl

use of fish.payara.microprofile.openapi.impl.model.media.SchemaImpl in project Payara by payara.

the class ApplicationProcessor method insertObjectReference.

/**
 * Replace the object in the referee with a reference, and create the
 * reference in the API.
 *
 * @param context the API context.
 * @param referee the object containing the reference.
 * @param referenceClass the class of the object being referenced.
 * @return if the reference has been created.
 */
private boolean insertObjectReference(ApiContext context, Reference<?> referee, AnnotatedElement referenceClass, String referenceClassName) {
    // Firstly check if it's been already defined (i.e. config property definition)
    for (Entry<String, Schema> schemaEntry : context.getApi().getComponents().getSchemas().entrySet()) {
        final Schema entryValue = schemaEntry.getValue();
        if (entryValue instanceof SchemaImpl) {
            final SchemaImpl entryValueImpl = (SchemaImpl) entryValue;
            final String implementationClass = entryValueImpl.getImplementation();
            if (implementationClass != null && implementationClass.equals(referenceClassName)) {
                referee.setRef(schemaEntry.getKey());
                return true;
            }
        }
    }
    // If the object is a java core class
    if (referenceClassName == null || referenceClassName.startsWith("java.")) {
        return false;
    }
    // If the object is a Java EE object type
    if (referenceClassName.startsWith("javax.")) {
        return false;
    }
    // Check the class exists in the application
    if (!context.isApplicationType(referenceClassName)) {
        return false;
    }
    if (referenceClass != null && referenceClass instanceof ExtensibleType) {
        ExtensibleType referenceClassType = (ExtensibleType) referenceClass;
        final AnnotationModel schemaAnnotation = context.getAnnotationInfo(referenceClassType).getAnnotation(org.eclipse.microprofile.openapi.annotations.media.Schema.class);
        String schemaName = ModelUtils.getSchemaName(context, referenceClass);
        // Set the reference name
        referee.setRef(schemaName);
        Schema schema = context.getApi().getComponents().getSchemas().get(schemaName);
        if (schema == null) {
            // Create the schema
            if (context.isAllowedType(referenceClassType)) {
                visitSchema(schemaAnnotation, referenceClassType, context);
            } else if (referenceClassType instanceof ClassModel) {
                apiWalker.processAnnotation((ClassModel) referenceClassType, this);
            } else {
                LOGGER.log(FINE, "Unrecognised schema {0} class found.", new Object[] { referenceClassName });
            }
        }
        return true;
    }
    return false;
}
Also used : SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) ClassModel(org.glassfish.hk2.classmodel.reflect.ClassModel) Schema(org.eclipse.microprofile.openapi.models.media.Schema) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) ExtensibleType(org.glassfish.hk2.classmodel.reflect.ExtensibleType)

Example 5 with SchemaImpl

use of fish.payara.microprofile.openapi.impl.model.media.SchemaImpl in project Payara by payara.

the class ApplicationProcessor method addParameter.

private static void addParameter(AnnotatedElement element, ApiContext context, String name, In in, Boolean required) {
    Boolean hidden = false;
    AnnotationModel paramAnnotation = element.getAnnotation(org.eclipse.microprofile.openapi.annotations.parameters.Parameter.class.getName());
    if (paramAnnotation != null) {
        hidden = paramAnnotation.getValue("hidden", Boolean.class);
    }
    if (hidden != null && hidden) {
        return;
    }
    Parameter newParameter = new ParameterImpl();
    newParameter.setName(name);
    newParameter.setIn(in);
    newParameter.setRequired(required);
    SchemaImpl schema = new SchemaImpl();
    String defaultValue = getDefaultValueIfPresent(element);
    if (element instanceof org.glassfish.hk2.classmodel.reflect.Parameter) {
        org.glassfish.hk2.classmodel.reflect.Parameter parameter = (org.glassfish.hk2.classmodel.reflect.Parameter) element;
        schema.setType(ModelUtils.getSchemaType(parameter.getTypeName(), context));
    } else {
        FieldModel field = (FieldModel) element;
        schema.setType(ModelUtils.getSchemaType(field.getTypeName(), context));
    }
    if (schema.getType() == SchemaType.ARRAY) {
        schema.setItems(getArraySchema(element, context));
        if (defaultValue != null) {
            schema.getItems().setDefaultValue(defaultValue);
        }
    } else if (defaultValue != null) {
        schema.setDefaultValue(defaultValue);
    }
    newParameter.setSchema(schema);
    final Operation workingOperation = context.getWorkingOperation();
    if (workingOperation != null) {
        for (Parameter parameter : workingOperation.getParameters()) {
            final String parameterName = parameter.getName();
            if (parameterName != null && parameterName.equals(newParameter.getName())) {
                ParameterImpl.merge(newParameter, parameter, false, context);
                return;
            }
        }
        workingOperation.addParameter(newParameter);
    } else {
        LOGGER.log(SEVERE, "Couldn''t add {0} parameter, \"{1}\" to the OpenAPI Document. This is usually caused by declaring parameter under a method with an unsupported annotation.", new Object[] { newParameter.getIn(), newParameter.getName() });
    }
}
Also used : Operation(org.eclipse.microprofile.openapi.models.Operation) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) ParameterImpl(fish.payara.microprofile.openapi.impl.model.parameters.ParameterImpl) AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) Parameter(org.eclipse.microprofile.openapi.models.parameters.Parameter) FieldModel(org.glassfish.hk2.classmodel.reflect.FieldModel)

Aggregations

SchemaImpl (fish.payara.microprofile.openapi.impl.model.media.SchemaImpl)19 Schema (org.eclipse.microprofile.openapi.models.media.Schema)10 Parameter (org.eclipse.microprofile.openapi.models.parameters.Parameter)7 ExampleImpl (fish.payara.microprofile.openapi.impl.model.examples.ExampleImpl)5 RequestBodyImpl (fish.payara.microprofile.openapi.impl.model.parameters.RequestBodyImpl)5 MediaType (org.eclipse.microprofile.openapi.models.media.MediaType)5 AnnotationModel (org.glassfish.hk2.classmodel.reflect.AnnotationModel)5 FieldModel (org.glassfish.hk2.classmodel.reflect.FieldModel)5 ContentImpl (fish.payara.microprofile.openapi.impl.model.media.ContentImpl)4 ParameterImpl (fish.payara.microprofile.openapi.impl.model.parameters.ParameterImpl)4 Components (org.eclipse.microprofile.openapi.models.Components)4 Example (org.eclipse.microprofile.openapi.models.examples.Example)4 SchemaType (org.eclipse.microprofile.openapi.models.media.Schema.SchemaType)4 CallbackImpl (fish.payara.microprofile.openapi.impl.model.callbacks.CallbackImpl)3 LinkImpl (fish.payara.microprofile.openapi.impl.model.links.LinkImpl)3 APIResponseImpl (fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl)3 SecuritySchemeImpl (fish.payara.microprofile.openapi.impl.model.security.SecuritySchemeImpl)3 Operation (org.eclipse.microprofile.openapi.models.Operation)3 RequestBody (org.eclipse.microprofile.openapi.models.parameters.RequestBody)3 MethodModel (org.glassfish.hk2.classmodel.reflect.MethodModel)3