Search in sources :

Example 1 with OpenAPIConfiguration

use of io.swagger.v3.oas.integration.api.OpenAPIConfiguration in project cxf by apache.

the class OpenApiCustomizer method customize.

public OpenAPIConfiguration customize(final OpenAPIConfiguration configuration) {
    if (configuration == null) {
        return configuration;
    }
    if (dynamicBasePath) {
        final MessageContext ctx = createMessageContext();
        // If the JAX-RS application with custom path is defined, it might be present twice, in the
        // request URI as well as in each resource operation URI. To properly represent server URL,
        // the application path should be removed from it.
        final String url = StringUtils.removeEnd(StringUtils.substringBeforeLast(ctx.getUriInfo().getRequestUri().toString(), "/"), applicationPath);
        final Collection<Server> servers = configuration.getOpenAPI().getServers();
        if (servers == null || servers.stream().noneMatch(s -> s.getUrl().equalsIgnoreCase(url))) {
            configuration.getOpenAPI().setServers(Collections.singletonList(new Server().url(url)));
        }
    }
    return configuration;
}
Also used : URL(java.net.URL) Parameter(io.swagger.v3.oas.models.parameters.Parameter) OpenAPIConfiguration(io.swagger.v3.oas.integration.api.OpenAPIConfiguration) HashMap(java.util.HashMap) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) JAXRSUtils(org.apache.cxf.jaxrs.utils.JAXRSUtils) Operation(io.swagger.v3.oas.models.Operation) StringUtils(org.apache.commons.lang3.StringUtils) JavaDocProvider(org.apache.cxf.jaxrs.model.doc.JavaDocProvider) ArrayList(java.util.ArrayList) Pair(org.apache.commons.lang3.tuple.Pair) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Map(java.util.Map) ResourceUtils(org.apache.cxf.jaxrs.utils.ResourceUtils) Tag(io.swagger.v3.oas.models.tags.Tag) DocumentationProvider(org.apache.cxf.jaxrs.model.doc.DocumentationProvider) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Collection(java.util.Collection) ApplicationPath(javax.ws.rs.ApplicationPath) List(java.util.List) HttpMethod(io.swagger.v3.oas.models.PathItem.HttpMethod) Server(io.swagger.v3.oas.models.servers.Server) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Collections(java.util.Collections) ApplicationInfo(org.apache.cxf.jaxrs.model.ApplicationInfo) Server(io.swagger.v3.oas.models.servers.Server) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext)

Example 2 with OpenAPIConfiguration

use of io.swagger.v3.oas.integration.api.OpenAPIConfiguration in project syncope by apache.

the class SyncopeOpenApiCustomizer method customize.

@Override
public OpenAPIConfiguration customize(final OpenAPIConfiguration configuration) {
    Map<String, Header> headers = new LinkedHashMap<>();
    headers.put(RESTHeaders.ERROR_CODE, new Header().schema(new Schema<>().type("string")).description("Error code"));
    headers.put(RESTHeaders.ERROR_INFO, new Header().schema(new Schema<>().type("string")).description("Error message"));
    Content content = new Content();
    content.addMediaType(javax.ws.rs.core.MediaType.APPLICATION_JSON, new MediaType().schema(new Schema<ErrorTO>()));
    content.addMediaType(javax.ws.rs.core.MediaType.APPLICATION_XML, new MediaType().schema(new Schema<ErrorTO>()));
    configuration.getOpenAPI().getComponents().addResponses("400", new ApiResponse().description("An error occurred; HTTP status code can vary depending on the actual error: " + "400, 403, 404, 409, 412").headers(headers).content(content));
    return super.customize(configuration);
}
Also used : Header(io.swagger.v3.oas.models.headers.Header) Content(io.swagger.v3.oas.models.media.Content) Schema(io.swagger.v3.oas.models.media.Schema) MediaType(io.swagger.v3.oas.models.media.MediaType) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with OpenAPIConfiguration

use of io.swagger.v3.oas.integration.api.OpenAPIConfiguration in project cxf by apache.

the class OpenApiCustomizedResource method getOpenApi.

@GET
@Produces({ MediaType.APPLICATION_JSON, "application/yaml" })
@Operation(hidden = true)
public Response getOpenApi(@Context ServletConfig config, @Context HttpHeaders headers, @Context UriInfo uriInfo, @PathParam("type") String type) throws Exception {
    if (customizer != null) {
        final OpenAPIConfiguration configuration = customizer.customize(getOpenApiConfiguration());
        setOpenApiConfiguration(configuration);
        // By default, the OpenApiContext instance is cached. It means that the configuration
        // changes won't be taken into account (due to the deep copying rather than reference
        // passing). In order to reflect any changes which customization may do, we have to
        // update reader's configuration directly.
        final String ctxId = ServletConfigContextUtils.getContextIdFromServletConfig(config);
        final OpenApiContext ctx = OpenApiContextLocator.getInstance().getOpenApiContext(ctxId);
        if (ctx instanceof GenericOpenApiContext<?>) {
            ((GenericOpenApiContext<?>) ctx).getOpenApiReader().setConfiguration(configuration);
            customizer.customize(ctx.read());
        }
    }
    return super.getOpenApi(headers, uriInfo, type);
}
Also used : OpenAPIConfiguration(io.swagger.v3.oas.integration.api.OpenAPIConfiguration) GenericOpenApiContext(io.swagger.v3.oas.integration.GenericOpenApiContext) GenericOpenApiContext(io.swagger.v3.oas.integration.GenericOpenApiContext) OpenApiContext(io.swagger.v3.oas.integration.api.OpenApiContext) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Operation(io.swagger.v3.oas.annotations.Operation)

Example 4 with OpenAPIConfiguration

use of io.swagger.v3.oas.integration.api.OpenAPIConfiguration in project cxf by apache.

the class OpenApiFeature method initialize.

@Override
public void initialize(Server server, Bus bus) {
    final JAXRSServiceFactoryBean sfb = (JAXRSServiceFactoryBean) server.getEndpoint().get(JAXRSServiceFactoryBean.class.getName());
    final ServerProviderFactory factory = (ServerProviderFactory) server.getEndpoint().get(ServerProviderFactory.class.getName());
    final Set<String> packages = new HashSet<>();
    if (resourcePackages != null) {
        packages.addAll(resourcePackages);
    }
    Properties swaggerProps = null;
    GenericOpenApiContextBuilder<?> openApiConfiguration;
    final Application application = getApplicationOrDefault(server, factory, sfb, bus);
    if (StringUtils.isEmpty(getConfigLocation())) {
        swaggerProps = getSwaggerProperties(propertiesLocation, bus);
        if (isScan()) {
            packages.addAll(scanResourcePackages(sfb));
        }
        final OpenAPI oas = new OpenAPI().info(getInfo(swaggerProps));
        registerComponents(securityDefinitions).ifPresent(oas::setComponents);
        final SwaggerConfiguration config = new SwaggerConfiguration().openAPI(oas).prettyPrint(getOrFallback(isPrettyPrint(), swaggerProps, PRETTY_PRINT_PROPERTY)).readAllResources(isReadAllResources()).ignoredRoutes(getIgnoredRoutes()).filterClass(getOrFallback(getFilterClass(), swaggerProps, FILTER_CLASS_PROPERTY)).resourceClasses(getResourceClasses()).resourcePackages(getOrFallback(packages, swaggerProps, RESOURCE_PACKAGE_PROPERTY));
        openApiConfiguration = new JaxrsOpenApiContextBuilder<>().application(application).openApiConfiguration(config);
    } else {
        openApiConfiguration = new JaxrsOpenApiContextBuilder<>().application(application).configLocation(getConfigLocation());
    }
    try {
        final OpenApiContext context = openApiConfiguration.buildContext(true);
        final Properties userProperties = getUserProperties(context.getOpenApiConfiguration().getUserDefinedOptions());
        registerOpenApiResources(sfb, packages, context.getOpenApiConfiguration());
        registerSwaggerUiResources(sfb, combine(swaggerProps, userProperties), factory, bus);
        if (customizer != null) {
            customizer.setApplicationInfo(factory.getApplicationProvider());
        }
    } catch (OpenApiConfigurationException ex) {
        throw new RuntimeException("Unable to initialize OpenAPI context", ex);
    }
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) OpenApiConfigurationException(io.swagger.v3.oas.integration.OpenApiConfigurationException) Properties(java.util.Properties) SwaggerConfiguration(io.swagger.v3.oas.integration.SwaggerConfiguration) JAXRSServiceFactoryBean(org.apache.cxf.jaxrs.JAXRSServiceFactoryBean) Application(javax.ws.rs.core.Application) OpenAPI(io.swagger.v3.oas.models.OpenAPI) HashSet(java.util.HashSet) OpenApiContext(io.swagger.v3.oas.integration.api.OpenApiContext)

Aggregations

OpenAPIConfiguration (io.swagger.v3.oas.integration.api.OpenAPIConfiguration)2 OpenApiContext (io.swagger.v3.oas.integration.api.OpenApiContext)2 OpenAPI (io.swagger.v3.oas.models.OpenAPI)2 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)2 Operation (io.swagger.v3.oas.annotations.Operation)1 GenericOpenApiContext (io.swagger.v3.oas.integration.GenericOpenApiContext)1 OpenApiConfigurationException (io.swagger.v3.oas.integration.OpenApiConfigurationException)1 SwaggerConfiguration (io.swagger.v3.oas.integration.SwaggerConfiguration)1 Operation (io.swagger.v3.oas.models.Operation)1 HttpMethod (io.swagger.v3.oas.models.PathItem.HttpMethod)1 Header (io.swagger.v3.oas.models.headers.Header)1 Content (io.swagger.v3.oas.models.media.Content)1 MediaType (io.swagger.v3.oas.models.media.MediaType)1 Schema (io.swagger.v3.oas.models.media.Schema)1 Parameter (io.swagger.v3.oas.models.parameters.Parameter)1 Server (io.swagger.v3.oas.models.servers.Server)1 Tag (io.swagger.v3.oas.models.tags.Tag)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1