Search in sources :

Example 1 with OpenApiContext

use of io.swagger.v3.oas.integration.api.OpenApiContext 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 2 with OpenApiContext

use of io.swagger.v3.oas.integration.api.OpenApiContext 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

OpenApiContext (io.swagger.v3.oas.integration.api.OpenApiContext)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 OpenAPIConfiguration (io.swagger.v3.oas.integration.api.OpenAPIConfiguration)1 OpenAPI (io.swagger.v3.oas.models.OpenAPI)1 HashSet (java.util.HashSet)1 Properties (java.util.Properties)1 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1 Application (javax.ws.rs.core.Application)1 JAXRSServiceFactoryBean (org.apache.cxf.jaxrs.JAXRSServiceFactoryBean)1 ServerProviderFactory (org.apache.cxf.jaxrs.provider.ServerProviderFactory)1