Search in sources :

Example 6 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) {
    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 7 with SchemaImpl

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

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

Example 9 with SchemaImpl

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

the class OpenApiWalker method addSchemasToPaths.

private void addSchemasToPaths() {
    OpenAPI api = context.getApi();
    api.getPaths().getPathItems().forEach((String s, PathItem t) -> {
        t.getOperations().forEach((PathItem.HttpMethod u, org.eclipse.microprofile.openapi.models.Operation v) -> {
            v.getResponses().getAPIResponses().forEach((String w, org.eclipse.microprofile.openapi.models.responses.APIResponse x) -> {
                if (x.getContent() != null) {
                    x.getContent().getMediaTypes().forEach((y, z) -> {
                        SchemaImpl.merge(z.getSchema(), z.getSchema(), true, context);
                        if (z.getSchema() instanceof SchemaImpl) {
                            SchemaImpl schema = (SchemaImpl) z.getSchema();
                            schema.setImplementation(null);
                        }
                    });
                }
            });
        });
    });
}
Also used : PathItem(org.eclipse.microprofile.openapi.models.PathItem) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) APIResponse(org.eclipse.microprofile.openapi.annotations.responses.APIResponse) Operation(org.eclipse.microprofile.openapi.annotations.Operation) OpenAPI(org.eclipse.microprofile.openapi.models.OpenAPI)

Example 10 with SchemaImpl

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

the class ComponentsImpl method merge.

public static void merge(Components from, Components to, boolean override, ApiContext context) {
    if (from == null) {
        return;
    }
    // Handle @Schema
    if (from.getSchemas() != null) {
        for (Entry<String, Schema> fromEntry : from.getSchemas().entrySet()) {
            final String schemaName = fromEntry.getKey();
            if (schemaName != null) {
                final Schema fromSchema = fromEntry.getValue();
                final Schema toSchema = to.getSchemas().getOrDefault(schemaName, new SchemaImpl());
                SchemaImpl.merge(fromSchema, toSchema, override, context);
                to.addSchema(schemaName, toSchema);
            }
        }
    }
    // Handle @Callback
    if (from.getCallbacks() != null) {
        for (String callbackName : from.getCallbacks().keySet()) {
            if (callbackName != null) {
                Callback newCallback = new CallbackImpl();
                CallbackImpl.merge(from.getCallbacks().get(callbackName), newCallback, override, context);
                to.addCallback(callbackName, newCallback);
            }
        }
    }
    // Handle @ExampleObject
    if (from.getExamples() != null) {
        for (String exampleName : from.getExamples().keySet()) {
            if (exampleName != null) {
                Example newExample = new ExampleImpl();
                ExampleImpl.merge(from.getExamples().get(exampleName), newExample, override);
                to.addExample(exampleName, newExample);
            }
        }
    }
    // Handle @Header
    if (from.getHeaders() != null) {
        for (String headerName : from.getHeaders().keySet()) {
            if (headerName != null) {
                Header newHeader = new HeaderImpl();
                HeaderImpl.merge(from.getHeaders().get(headerName), newHeader, override, context);
                to.addHeader(headerName, newHeader);
            }
        }
    }
    // Handle @Link
    if (from.getLinks() != null) {
        for (String linkName : from.getLinks().keySet()) {
            if (linkName != null) {
                Link newLink = new LinkImpl();
                LinkImpl.merge(from.getLinks().get(linkName), newLink, override);
                to.addLink(linkName, newLink);
            }
        }
    }
    // Handle @Parameter
    if (from.getParameters() != null) {
        for (String parameterName : from.getParameters().keySet()) {
            if (parameterName != null) {
                Parameter newParameter = new ParameterImpl();
                ParameterImpl.merge(from.getParameters().get(parameterName), newParameter, override, context);
                to.addParameter(parameterName, newParameter);
            }
        }
    }
    // Handle @RequestBody
    if (from.getRequestBodies() != null) {
        for (String requestBodyName : from.getRequestBodies().keySet()) {
            if (requestBodyName != null) {
                RequestBody newRequestBody = new RequestBodyImpl();
                RequestBodyImpl.merge(from.getRequestBodies().get(requestBodyName), newRequestBody, override, context);
                to.addRequestBody(requestBodyName, newRequestBody);
            }
        }
    }
    // Handle @APIResponse
    if (from.getResponses() != null) {
        for (String responseName : from.getResponses().keySet()) {
            if (responseName != null) {
                APIResponse newResponse = new APIResponseImpl();
                APIResponseImpl.merge(from.getResponses().get(responseName), newResponse, override, context);
                to.addResponse(responseName, newResponse);
            }
        }
    }
    // Handle @SecurityScheme
    if (from.getSecuritySchemes() != null) {
        for (String securitySchemeName : from.getSecuritySchemes().keySet()) {
            if (securitySchemeName != null) {
                SecurityScheme newSecurity = new SecuritySchemeImpl();
                SecuritySchemeImpl.merge(from.getSecuritySchemes().get(securitySchemeName), newSecurity, override);
                to.addSecurityScheme(securitySchemeName, newSecurity);
            }
        }
    }
}
Also used : APIResponse(org.eclipse.microprofile.openapi.models.responses.APIResponse) CallbackImpl(fish.payara.microprofile.openapi.impl.model.callbacks.CallbackImpl) HeaderImpl(fish.payara.microprofile.openapi.impl.model.headers.HeaderImpl) Schema(org.eclipse.microprofile.openapi.models.media.Schema) SecuritySchemeImpl(fish.payara.microprofile.openapi.impl.model.security.SecuritySchemeImpl) RequestBodyImpl(fish.payara.microprofile.openapi.impl.model.parameters.RequestBodyImpl) APIResponseImpl(fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) Callback(org.eclipse.microprofile.openapi.models.callbacks.Callback) Header(org.eclipse.microprofile.openapi.models.headers.Header) ParameterImpl(fish.payara.microprofile.openapi.impl.model.parameters.ParameterImpl) Example(org.eclipse.microprofile.openapi.models.examples.Example) LinkImpl(fish.payara.microprofile.openapi.impl.model.links.LinkImpl) Parameter(org.eclipse.microprofile.openapi.models.parameters.Parameter) ExampleImpl(fish.payara.microprofile.openapi.impl.model.examples.ExampleImpl) SecurityScheme(org.eclipse.microprofile.openapi.models.security.SecurityScheme) Link(org.eclipse.microprofile.openapi.models.links.Link) RequestBody(org.eclipse.microprofile.openapi.models.parameters.RequestBody)

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