Search in sources :

Example 1 with SecurityScheme

use of org.eclipse.microprofile.openapi.models.security.SecurityScheme in project wildfly-swarm by wildfly-swarm.

the class OpenApiAnnotationScanner method jaxRsApplicationToOpenApi.

/**
 * Processes a JAX-RS {@link Application} and creates an {@link OpenAPI} model.  Performs
 * annotation scanning and other processing.  Returns a model unique to that single JAX-RS
 * app.
 * @param applicationClass
 */
private OpenAPIImpl jaxRsApplicationToOpenApi(ClassInfo applicationClass) {
    OpenAPIImpl oai = new OpenAPIImpl();
    oai.setOpenapi(OpenApiConstants.OPEN_API_VERSION);
    // Get the @ApplicationPath info and save it for later (also support @Path which seems nonstandard but common).
    // //////////////////////////////////////
    AnnotationInstance appPathAnno = JandexUtil.getClassAnnotation(applicationClass, OpenApiConstants.DOTNAME_APPLICATION_PATH);
    if (appPathAnno == null) {
        appPathAnno = JandexUtil.getClassAnnotation(applicationClass, OpenApiConstants.DOTNAME_PATH);
    }
    if (appPathAnno != null) {
        this.currentAppPath = appPathAnno.value().asString();
    } else {
        this.currentAppPath = "/";
    }
    // Get the @OpenAPIDefinition annotation and process it.
    // //////////////////////////////////////
    AnnotationInstance openApiDefAnno = JandexUtil.getClassAnnotation(applicationClass, OpenApiConstants.DOTNAME_OPEN_API_DEFINITION);
    if (openApiDefAnno != null) {
        processDefinition(oai, openApiDefAnno);
    }
    // Process @SecurityScheme annotations
    // //////////////////////////////////////
    List<AnnotationInstance> securitySchemeAnnotations = JandexUtil.getRepeatableAnnotation(applicationClass, OpenApiConstants.DOTNAME_SECURITY_SCHEME, OpenApiConstants.DOTNAME_SECURITY_SCHEMES);
    for (AnnotationInstance annotation : securitySchemeAnnotations) {
        String name = JandexUtil.stringValue(annotation, OpenApiConstants.PROP_SECURITY_SCHEME_NAME);
        if (name == null && JandexUtil.isRef(annotation)) {
            name = JandexUtil.nameFromRef(annotation);
        }
        if (name != null) {
            SecurityScheme securityScheme = readSecurityScheme(annotation);
            Components components = ModelUtil.components(oai);
            components.addSecurityScheme(name, securityScheme);
        }
    }
    // Process @Server annotations
    // /////////////////////////////////
    List<AnnotationInstance> serverAnnotations = JandexUtil.getRepeatableAnnotation(applicationClass, OpenApiConstants.DOTNAME_SERVER, OpenApiConstants.DOTNAME_SERVERS);
    for (AnnotationInstance annotation : serverAnnotations) {
        Server server = readServer(annotation);
        oai.addServer(server);
    }
    return oai;
}
Also used : Components(org.eclipse.microprofile.openapi.models.Components) Server(org.eclipse.microprofile.openapi.models.servers.Server) OpenAPIImpl(org.wildfly.swarm.microprofile.openapi.api.models.OpenAPIImpl) SecurityScheme(org.eclipse.microprofile.openapi.models.security.SecurityScheme) AnnotationInstance(org.jboss.jandex.AnnotationInstance)

Example 2 with SecurityScheme

use of org.eclipse.microprofile.openapi.models.security.SecurityScheme in project wildfly-swarm by wildfly-swarm.

the class OpenApiAnnotationScanner method readSecurityScheme.

/**
 * Reads a SecurityScheme annotation into a model.
 * @param annotation
 */
private SecurityScheme readSecurityScheme(AnnotationInstance annotation) {
    if (annotation == null) {
        return null;
    }
    LOG.debug("Processing a single @SecurityScheme annotation.");
    SecurityScheme securityScheme = new SecuritySchemeImpl();
    securityScheme.setType(JandexUtil.enumValue(annotation, OpenApiConstants.PROP_TYPE, org.eclipse.microprofile.openapi.models.security.SecurityScheme.Type.class));
    securityScheme.setDescription(JandexUtil.stringValue(annotation, OpenApiConstants.PROP_DESCRIPTION));
    securityScheme.setName(JandexUtil.stringValue(annotation, OpenApiConstants.PROP_API_KEY_NAME));
    securityScheme.setIn(JandexUtil.enumValue(annotation, OpenApiConstants.PROP_IN, org.eclipse.microprofile.openapi.models.security.SecurityScheme.In.class));
    securityScheme.setScheme(JandexUtil.stringValue(annotation, OpenApiConstants.PROP_SCHEME));
    securityScheme.setBearerFormat(JandexUtil.stringValue(annotation, OpenApiConstants.PROP_BEARER_FORMAT));
    securityScheme.setFlows(readOAuthFlows(annotation.value(OpenApiConstants.PROP_FLOWS)));
    securityScheme.setOpenIdConnectUrl(JandexUtil.stringValue(annotation, OpenApiConstants.PROP_OPEN_ID_CONNECT_URL));
    securityScheme.setRef(JandexUtil.refValue(annotation, RefType.SecurityScheme));
    return securityScheme;
}
Also used : ClassType(org.jboss.jandex.ClassType) RefType(org.wildfly.swarm.microprofile.openapi.runtime.util.JandexUtil.RefType) MediaType(org.eclipse.microprofile.openapi.models.media.MediaType) Type(org.jboss.jandex.Type) In(org.eclipse.microprofile.openapi.models.parameters.Parameter.In) SecuritySchemeImpl(org.wildfly.swarm.microprofile.openapi.api.models.security.SecuritySchemeImpl) SecurityScheme(org.eclipse.microprofile.openapi.models.security.SecurityScheme)

Example 3 with SecurityScheme

use of org.eclipse.microprofile.openapi.models.security.SecurityScheme in project wildfly-swarm by wildfly-swarm.

the class FilterUtil method filterSecuritySchemes.

/**
 * Filters the given models.
 * @param filter
 * @param models
 */
private static void filterSecuritySchemes(OASFilter filter, Map<String, SecurityScheme> models) {
    if (models == null) {
        return;
    }
    Collection<String> keys = new ArrayList<>(models.keySet());
    for (String key : keys) {
        SecurityScheme model = models.get(key);
        filterSecurityScheme(filter, model);
        if (filter.filterSecurityScheme(model) == null) {
            models.remove(key);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) SecurityScheme(org.eclipse.microprofile.openapi.models.security.SecurityScheme)

Example 4 with SecurityScheme

use of org.eclipse.microprofile.openapi.models.security.SecurityScheme 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)

Example 5 with SecurityScheme

use of org.eclipse.microprofile.openapi.models.security.SecurityScheme in project Payara by payara.

the class ApplicationProcessor method visitSecurityScheme.

@Override
public void visitSecurityScheme(AnnotationModel annotation, AnnotatedElement element, ApiContext context) {
    String securitySchemeName = annotation.getValue("securitySchemeName", String.class);
    SecurityScheme securityScheme = SecuritySchemeImpl.createInstance(annotation, context);
    if (securitySchemeName != null && !securitySchemeName.isEmpty()) {
        SecurityScheme newScheme = context.getApi().getComponents().getSecuritySchemes().getOrDefault(securitySchemeName, new SecuritySchemeImpl());
        context.getApi().getComponents().addSecurityScheme(securitySchemeName, newScheme);
        SecuritySchemeImpl.merge(securityScheme, newScheme, true);
    }
}
Also used : SecuritySchemeImpl(fish.payara.microprofile.openapi.impl.model.security.SecuritySchemeImpl) SecurityScheme(org.eclipse.microprofile.openapi.models.security.SecurityScheme)

Aggregations

SecurityScheme (org.eclipse.microprofile.openapi.models.security.SecurityScheme)9 SecuritySchemeImpl (fish.payara.microprofile.openapi.impl.model.security.SecuritySchemeImpl)3 Components (org.eclipse.microprofile.openapi.models.Components)3 AnnotationInstance (org.jboss.jandex.AnnotationInstance)3 CallbackImpl (fish.payara.microprofile.openapi.impl.model.callbacks.CallbackImpl)2 ExampleImpl (fish.payara.microprofile.openapi.impl.model.examples.ExampleImpl)2 HeaderImpl (fish.payara.microprofile.openapi.impl.model.headers.HeaderImpl)2 LinkImpl (fish.payara.microprofile.openapi.impl.model.links.LinkImpl)2 SchemaImpl (fish.payara.microprofile.openapi.impl.model.media.SchemaImpl)2 ParameterImpl (fish.payara.microprofile.openapi.impl.model.parameters.ParameterImpl)2 RequestBodyImpl (fish.payara.microprofile.openapi.impl.model.parameters.RequestBodyImpl)2 APIResponseImpl (fish.payara.microprofile.openapi.impl.model.responses.APIResponseImpl)2 LinkedHashMap (java.util.LinkedHashMap)2 Callback (org.eclipse.microprofile.openapi.models.callbacks.Callback)2 Tag (org.eclipse.microprofile.openapi.models.tags.Tag)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ContactImpl (fish.payara.microprofile.openapi.impl.model.info.ContactImpl)1 InfoImpl (fish.payara.microprofile.openapi.impl.model.info.InfoImpl)1 LicenseImpl (fish.payara.microprofile.openapi.impl.model.info.LicenseImpl)1 ContentImpl (fish.payara.microprofile.openapi.impl.model.media.ContentImpl)1