Search in sources :

Example 11 with MediaType

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

the class ContentImpl method createInstance.

public static ContentImpl createInstance(AnnotationModel annotation, ApiContext context) {
    ContentImpl from = new ContentImpl();
    String typeName = annotation.getValue("mediaType", String.class);
    if (typeName == null || typeName.isEmpty()) {
        typeName = javax.ws.rs.core.MediaType.WILDCARD;
    }
    MediaType mediaType = new MediaTypeImpl();
    from.addMediaType(typeName, mediaType);
    extractAnnotations(annotation, context, "examples", "name", ExampleImpl::createInstance, mediaType::addExample);
    mediaType.setExample(annotation.getValue("example", String.class));
    AnnotationModel schemaAnnotation = annotation.getValue("schema", AnnotationModel.class);
    if (schemaAnnotation != null) {
        Boolean hidden = schemaAnnotation.getValue("hidden", Boolean.class);
        if (hidden == null || !hidden) {
            mediaType.setSchema(SchemaImpl.createInstance(schemaAnnotation, context));
        }
    }
    extractAnnotations(annotation, context, "encoding", "name", EncodingImpl::createInstance, mediaType::addEncoding);
    return from;
}
Also used : AnnotationModel(org.glassfish.hk2.classmodel.reflect.AnnotationModel) MediaType(org.eclipse.microprofile.openapi.models.media.MediaType) ExampleImpl(fish.payara.microprofile.openapi.impl.model.examples.ExampleImpl)

Example 12 with MediaType

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

the class ApplicationProcessor method insertDefaultRequestBody.

// PRIVATE METHODS
private RequestBody insertDefaultRequestBody(ApiContext context, Operation operation, MethodModel method) {
    RequestBody requestBody = new RequestBodyImpl();
    // Get the request body type of the method
    org.glassfish.hk2.classmodel.reflect.ParameterizedType bodyType = null;
    for (org.glassfish.hk2.classmodel.reflect.Parameter methodParam : method.getParameters()) {
        if (ModelUtils.isRequestBody(context, methodParam)) {
            bodyType = methodParam;
            break;
        }
    }
    if (bodyType == null) {
        return null;
    }
    // Create the default request body with a wildcard mediatype
    MediaType mediaType = new MediaTypeImpl().schema(createSchema(context, bodyType));
    requestBody.getContent().addMediaType(javax.ws.rs.core.MediaType.WILDCARD, mediaType);
    operation.setRequestBody(requestBody);
    return requestBody;
}
Also used : MediaTypeImpl(fish.payara.microprofile.openapi.impl.model.media.MediaTypeImpl) ParameterizedType(org.glassfish.hk2.classmodel.reflect.ParameterizedType) MediaType(org.eclipse.microprofile.openapi.models.media.MediaType) RequestBodyImpl(fish.payara.microprofile.openapi.impl.model.parameters.RequestBodyImpl) RequestBody(org.eclipse.microprofile.openapi.models.parameters.RequestBody)

Example 13 with MediaType

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

the class ApplicationProcessor method visitRequestBodySchema.

@Override
public void visitRequestBodySchema(AnnotationModel requestBodySchema, AnnotatedElement element, ApiContext context) {
    if (element instanceof MethodModel || element instanceof org.glassfish.hk2.classmodel.reflect.Parameter) {
        final RequestBody currentRequestBody = context.getWorkingOperation().getRequestBody();
        if (currentRequestBody != null) {
            final String implementationClass = requestBodySchema.getValue("value", String.class);
            final SchemaImpl schema = SchemaImpl.fromImplementation(implementationClass, context);
            for (MediaType mediaType : currentRequestBody.getContent().getMediaTypes().values()) {
                mediaType.setSchema(schema);
            }
        }
    }
}
Also used : MethodModel(org.glassfish.hk2.classmodel.reflect.MethodModel) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) MediaType(org.eclipse.microprofile.openapi.models.media.MediaType) RequestBody(org.eclipse.microprofile.openapi.models.parameters.RequestBody)

Example 14 with MediaType

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

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

the class ModelInvariantsTest method addKeyValueIgnoresNull.

@Test
public void addKeyValueIgnoresNull() {
    BiPredicate<Extensible<?>, String> hasExtension = (obj, key) -> obj.getExtensions().containsKey(key);
    assertAddIgnoresNull(new CallbackImpl(), Callback::addPathItem, Callback::hasPathItem);
    assertAddIgnoresNull(new CallbackImpl(), Callback::addExtension, hasExtension);
    assertAddIgnoresNull(new ExampleImpl(), Example::addExtension, hasExtension);
    assertAddIgnoresNull(new HeaderImpl(), Header::addExample, (obj, key) -> obj.getExamples().containsKey(key));
    assertAddIgnoresNull(new HeaderImpl(), Header::addExtension, hasExtension);
    assertAddIgnoresNull(new ContactImpl(), Contact::addExtension, hasExtension);
    assertAddIgnoresNull(new InfoImpl(), Info::addExtension, hasExtension);
    assertAddIgnoresNull(new LicenseImpl(), License::addExtension, hasExtension);
    assertAddIgnoresNull(new LinkImpl(), Link::addParameter, (obj, key) -> obj.getParameters().containsKey(key));
    assertAddIgnoresNull(new LinkImpl(), Link::addExtension, hasExtension);
    assertAddIgnoresNull(new ContentImpl(), Content::addMediaType, Content::hasMediaType);
    assertAddIgnoresNull(new DiscriminatorImpl(), Discriminator::addMapping, (obj, key) -> obj.getMapping().containsKey(key));
    assertAddIgnoresNull(new EncodingImpl(), Encoding::addHeader, (obj, key) -> obj.getHeaders().containsKey(key));
    assertAddIgnoresNull(new EncodingImpl(), Encoding::addExtension, hasExtension);
    assertAddIgnoresNull(new MediaTypeImpl(), MediaType::addEncoding, (obj, key) -> obj.getEncoding().containsKey(key));
    assertAddIgnoresNull(new MediaTypeImpl(), MediaType::addExample, (obj, key) -> obj.getExamples().containsKey(key));
    assertAddIgnoresNull(new MediaTypeImpl(), MediaType::addExtension, hasExtension);
    assertAddIgnoresNull(new SchemaImpl(), Schema::addProperty, (obj, key) -> obj.getProperties().containsKey(key));
    assertAddIgnoresNull(new SchemaImpl(), Schema::addExtension, hasExtension);
    assertAddIgnoresNull(new XMLImpl(), XML::addExtension, hasExtension);
    assertAddIgnoresNull(new ParameterImpl(), Parameter::addExample, (obj, key) -> obj.getExamples().containsKey(key));
    assertAddIgnoresNull(new ParameterImpl(), Parameter::addExtension, hasExtension);
    assertAddIgnoresNull(new RequestBodyImpl(), RequestBody::addExtension, hasExtension);
    assertAddIgnoresNull(new APIResponseImpl(), APIResponse::addHeader, (obj, key) -> obj.getHeaders().containsKey(key));
    assertAddIgnoresNull(new APIResponseImpl(), APIResponse::addLink, (obj, key) -> obj.getLinks().containsKey(key));
    assertAddIgnoresNull(new APIResponseImpl(), APIResponse::addExtension, hasExtension);
    assertAddIgnoresNull(new APIResponsesImpl(), APIResponses::addAPIResponse, APIResponses::hasAPIResponse);
    assertAddIgnoresNull(new APIResponsesImpl(), APIResponses::addExtension, hasExtension);
    assertAddIgnoresNull(new OAuthFlowImpl(), OAuthFlow::addExtension, hasExtension);
    assertAddIgnoresNull(new OAuthFlowsImpl(), OAuthFlows::addExtension, hasExtension);
    assertAddIgnoresNull(new SecuritySchemeImpl(), SecurityScheme::addExtension, hasExtension);
    assertAddIgnoresNull(new ServerImpl(), Server::addExtension, hasExtension);
    assertAddIgnoresNull(new ServerVariableImpl(), ServerVariable::addExtension, hasExtension);
    assertAddIgnoresNull(new TagImpl(), Tag::addExtension, hasExtension);
    assertAddIgnoresNull(new ComponentsImpl(), Components::addCallback, (obj, key) -> obj.getCallbacks().containsKey(key));
    assertAddIgnoresNull(new ComponentsImpl(), Components::addExample, (obj, key) -> obj.getExamples().containsKey(key));
    assertAddIgnoresNull(new ComponentsImpl(), Components::addHeader, (obj, key) -> obj.getHeaders().containsKey(key));
    assertAddIgnoresNull(new ComponentsImpl(), Components::addLink, (obj, key) -> obj.getLinks().containsKey(key));
    assertAddIgnoresNull(new ComponentsImpl(), Components::addParameter, (obj, key) -> obj.getParameters().containsKey(key));
    assertAddIgnoresNull(new ComponentsImpl(), Components::addRequestBody, (obj, key) -> obj.getRequestBodies().containsKey(key));
    assertAddIgnoresNull(new ComponentsImpl(), Components::addResponse, (obj, key) -> obj.getResponses().containsKey(key));
    assertAddIgnoresNull(new ComponentsImpl(), Components::addSchema, (obj, key) -> obj.getSchemas().containsKey(key));
    assertAddIgnoresNull(new ComponentsImpl(), Components::addSecurityScheme, (obj, key) -> obj.getSecuritySchemes().containsKey(key));
    assertAddIgnoresNull(new ComponentsImpl(), Components::addExtension, hasExtension);
    assertAddIgnoresNull(new ExternalDocumentationImpl(), ExternalDocumentation::addExtension, hasExtension);
    assertAddIgnoresNull(new OpenAPIImpl(), OpenAPI::addExtension, hasExtension);
    assertAddIgnoresNull(new OperationImpl(), Operation::addCallback, (obj, key) -> obj.getCallbacks().containsKey(key));
    assertAddIgnoresNull(new OperationImpl(), Operation::addExtension, hasExtension);
    assertAddIgnoresNull(new PathItemImpl(), PathItem::addExtension, hasExtension);
    assertAddIgnoresNull(new PathsImpl(), Paths::addPathItem, Paths::hasPathItem);
    assertAddIgnoresNull(new PathsImpl(), Paths::addExtension, hasExtension);
}
Also used : RequestBodyImpl(fish.payara.microprofile.openapi.impl.model.parameters.RequestBodyImpl) Components(org.eclipse.microprofile.openapi.models.Components) Discriminator(org.eclipse.microprofile.openapi.models.media.Discriminator) Info(org.eclipse.microprofile.openapi.models.info.Info) ExampleImpl(fish.payara.microprofile.openapi.impl.model.examples.ExampleImpl) EncodingImpl(fish.payara.microprofile.openapi.impl.model.media.EncodingImpl) HeaderImpl(fish.payara.microprofile.openapi.impl.model.headers.HeaderImpl) License(org.eclipse.microprofile.openapi.models.info.License) PathItem(org.eclipse.microprofile.openapi.models.PathItem) Tag(org.eclipse.microprofile.openapi.models.tags.Tag) OAuthFlowsImpl(fish.payara.microprofile.openapi.impl.model.security.OAuthFlowsImpl) Header(org.eclipse.microprofile.openapi.models.headers.Header) MediaTypeImpl(fish.payara.microprofile.openapi.impl.model.media.MediaTypeImpl) RequestBody(org.eclipse.microprofile.openapi.models.parameters.RequestBody) SecurityScheme(org.eclipse.microprofile.openapi.models.security.SecurityScheme) Contact(org.eclipse.microprofile.openapi.models.info.Contact) OAuthFlows(org.eclipse.microprofile.openapi.models.security.OAuthFlows) OpenAPI(org.eclipse.microprofile.openapi.models.OpenAPI) List(java.util.List) ServerImpl(fish.payara.microprofile.openapi.impl.model.servers.ServerImpl) TagImpl(fish.payara.microprofile.openapi.impl.model.tags.TagImpl) APIResponseImpl(fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl) ExternalDocumentation(org.eclipse.microprofile.openapi.models.ExternalDocumentation) Assert.assertFalse(org.junit.Assert.assertFalse) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) Server(org.eclipse.microprofile.openapi.models.servers.Server) LicenseImpl(fish.payara.microprofile.openapi.impl.model.info.LicenseImpl) ContentImpl(fish.payara.microprofile.openapi.impl.model.media.ContentImpl) ServerVariableImpl(fish.payara.microprofile.openapi.impl.model.servers.ServerVariableImpl) Example(org.eclipse.microprofile.openapi.models.examples.Example) SecuritySchemeImpl(fish.payara.microprofile.openapi.impl.model.security.SecuritySchemeImpl) SecurityRequirement(org.eclipse.microprofile.openapi.models.security.SecurityRequirement) Paths(org.eclipse.microprofile.openapi.models.Paths) APIResponse(org.eclipse.microprofile.openapi.models.responses.APIResponse) MediaType(org.eclipse.microprofile.openapi.models.media.MediaType) OAuthFlow(org.eclipse.microprofile.openapi.models.security.OAuthFlow) DiscriminatorImpl(fish.payara.microprofile.openapi.impl.model.media.DiscriminatorImpl) Encoding(org.eclipse.microprofile.openapi.models.media.Encoding) ServerVariable(org.eclipse.microprofile.openapi.models.servers.ServerVariable) LinkImpl(fish.payara.microprofile.openapi.impl.model.links.LinkImpl) SecurityRequirementImpl(fish.payara.microprofile.openapi.impl.model.security.SecurityRequirementImpl) Assert.assertSame(org.junit.Assert.assertSame) BiPredicate(java.util.function.BiPredicate) InfoImpl(fish.payara.microprofile.openapi.impl.model.info.InfoImpl) Operation(org.eclipse.microprofile.openapi.models.Operation) Schema(org.eclipse.microprofile.openapi.models.media.Schema) Callback(org.eclipse.microprofile.openapi.models.callbacks.Callback) Assert.assertNotNull(org.junit.Assert.assertNotNull) Content(org.eclipse.microprofile.openapi.models.media.Content) CallbackImpl(fish.payara.microprofile.openapi.impl.model.callbacks.CallbackImpl) APIResponsesImpl(fish.payara.microprofile.openapi.impl.model.responses.APIResponsesImpl) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Link(org.eclipse.microprofile.openapi.models.links.Link) Parameter(org.eclipse.microprofile.openapi.models.parameters.Parameter) ParameterImpl(fish.payara.microprofile.openapi.impl.model.parameters.ParameterImpl) APIResponses(org.eclipse.microprofile.openapi.models.responses.APIResponses) OAuthFlowImpl(fish.payara.microprofile.openapi.impl.model.security.OAuthFlowImpl) ContactImpl(fish.payara.microprofile.openapi.impl.model.info.ContactImpl) XMLImpl(fish.payara.microprofile.openapi.impl.model.media.XMLImpl) Assert.assertEquals(org.junit.Assert.assertEquals) Extensible(org.eclipse.microprofile.openapi.models.Extensible) XML(org.eclipse.microprofile.openapi.models.media.XML) OAuthFlowsImpl(fish.payara.microprofile.openapi.impl.model.security.OAuthFlowsImpl) Server(org.eclipse.microprofile.openapi.models.servers.Server) Schema(org.eclipse.microprofile.openapi.models.media.Schema) SecuritySchemeImpl(fish.payara.microprofile.openapi.impl.model.security.SecuritySchemeImpl) License(org.eclipse.microprofile.openapi.models.info.License) ServerVariable(org.eclipse.microprofile.openapi.models.servers.ServerVariable) Components(org.eclipse.microprofile.openapi.models.Components) SchemaImpl(fish.payara.microprofile.openapi.impl.model.media.SchemaImpl) PathItem(org.eclipse.microprofile.openapi.models.PathItem) EncodingImpl(fish.payara.microprofile.openapi.impl.model.media.EncodingImpl) ExternalDocumentation(org.eclipse.microprofile.openapi.models.ExternalDocumentation) ContactImpl(fish.payara.microprofile.openapi.impl.model.info.ContactImpl) Example(org.eclipse.microprofile.openapi.models.examples.Example) MediaType(org.eclipse.microprofile.openapi.models.media.MediaType) ExampleImpl(fish.payara.microprofile.openapi.impl.model.examples.ExampleImpl) SecurityScheme(org.eclipse.microprofile.openapi.models.security.SecurityScheme) Encoding(org.eclipse.microprofile.openapi.models.media.Encoding) RequestBodyImpl(fish.payara.microprofile.openapi.impl.model.parameters.RequestBodyImpl) ContentImpl(fish.payara.microprofile.openapi.impl.model.media.ContentImpl) Header(org.eclipse.microprofile.openapi.models.headers.Header) ParameterImpl(fish.payara.microprofile.openapi.impl.model.parameters.ParameterImpl) OAuthFlow(org.eclipse.microprofile.openapi.models.security.OAuthFlow) Content(org.eclipse.microprofile.openapi.models.media.Content) MediaTypeImpl(fish.payara.microprofile.openapi.impl.model.media.MediaTypeImpl) OpenAPI(org.eclipse.microprofile.openapi.models.OpenAPI) Link(org.eclipse.microprofile.openapi.models.links.Link) 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) APIResponsesImpl(fish.payara.microprofile.openapi.impl.model.responses.APIResponsesImpl) Operation(org.eclipse.microprofile.openapi.models.Operation) LicenseImpl(fish.payara.microprofile.openapi.impl.model.info.LicenseImpl) OAuthFlowImpl(fish.payara.microprofile.openapi.impl.model.security.OAuthFlowImpl) ServerImpl(fish.payara.microprofile.openapi.impl.model.servers.ServerImpl) LinkImpl(fish.payara.microprofile.openapi.impl.model.links.LinkImpl) APIResponses(org.eclipse.microprofile.openapi.models.responses.APIResponses) Paths(org.eclipse.microprofile.openapi.models.Paths) RequestBody(org.eclipse.microprofile.openapi.models.parameters.RequestBody) DiscriminatorImpl(fish.payara.microprofile.openapi.impl.model.media.DiscriminatorImpl) Extensible(org.eclipse.microprofile.openapi.models.Extensible) OAuthFlows(org.eclipse.microprofile.openapi.models.security.OAuthFlows) TagImpl(fish.payara.microprofile.openapi.impl.model.tags.TagImpl) APIResponseImpl(fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl) Info(org.eclipse.microprofile.openapi.models.info.Info) XMLImpl(fish.payara.microprofile.openapi.impl.model.media.XMLImpl) Discriminator(org.eclipse.microprofile.openapi.models.media.Discriminator) Contact(org.eclipse.microprofile.openapi.models.info.Contact) ServerVariableImpl(fish.payara.microprofile.openapi.impl.model.servers.ServerVariableImpl) Callback(org.eclipse.microprofile.openapi.models.callbacks.Callback) XML(org.eclipse.microprofile.openapi.models.media.XML) Parameter(org.eclipse.microprofile.openapi.models.parameters.Parameter) Tag(org.eclipse.microprofile.openapi.models.tags.Tag) InfoImpl(fish.payara.microprofile.openapi.impl.model.info.InfoImpl) Test(org.junit.Test)

Aggregations

MediaType (org.eclipse.microprofile.openapi.models.media.MediaType)18 SchemaImpl (fish.payara.microprofile.openapi.impl.model.media.SchemaImpl)5 Schema (org.eclipse.microprofile.openapi.models.media.Schema)5 MediaTypeImpl (fish.payara.microprofile.openapi.impl.model.media.MediaTypeImpl)4 RequestBodyImpl (fish.payara.microprofile.openapi.impl.model.parameters.RequestBodyImpl)4 Content (org.eclipse.microprofile.openapi.models.media.Content)4 Parameter (org.eclipse.microprofile.openapi.models.parameters.Parameter)4 RequestBody (org.eclipse.microprofile.openapi.models.parameters.RequestBody)4 APIResponse (org.eclipse.microprofile.openapi.models.responses.APIResponse)4 MethodModel (org.glassfish.hk2.classmodel.reflect.MethodModel)4 MediaTypeImpl (org.wildfly.swarm.microprofile.openapi.api.models.media.MediaTypeImpl)4 ContentImpl (org.wildfly.swarm.microprofile.openapi.api.models.media.ContentImpl)3 ExampleImpl (fish.payara.microprofile.openapi.impl.model.examples.ExampleImpl)2 ContentImpl (fish.payara.microprofile.openapi.impl.model.media.ContentImpl)2 APIResponseImpl (fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl)2 APIResponsesImpl (fish.payara.microprofile.openapi.impl.model.responses.APIResponsesImpl)2 Encoding (org.eclipse.microprofile.openapi.models.media.Encoding)2 SchemaType (org.eclipse.microprofile.openapi.models.media.Schema.SchemaType)2 APIResponses (org.eclipse.microprofile.openapi.models.responses.APIResponses)2 CallbackImpl (fish.payara.microprofile.openapi.impl.model.callbacks.CallbackImpl)1