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;
}
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);
}
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);
}
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);
}
}
Aggregations