Search in sources :

Example 1 with OpenAPI

use of io.swagger.v3.oas.models.OpenAPI in project vertx-web by vert-x3.

the class OpenAPI3ParametersUnitTest method loadSwagger.

private OpenAPI loadSwagger(String filename) throws IOException {
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setResolveCombinators(false);
    options.setResolveFully(true);
    return new OpenAPIV3Parser().readContents(String.join("\n", Files.readAllLines(Paths.get(filename), StandardCharsets.UTF_8)), null, options).getOpenAPI();
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser)

Example 2 with OpenAPI

use of io.swagger.v3.oas.models.OpenAPI in project cxf by apache.

the class OpenApiCustomizer method customize.

public void customize(final OpenAPI oas) {
    if (replaceTags || javadocProvider != null) {
        Map<String, ClassResourceInfo> operations = new HashMap<>();
        Map<Pair<String, String>, OperationResourceInfo> methods = new HashMap<>();
        cris.forEach(cri -> {
            cri.getMethodDispatcher().getOperationResourceInfos().forEach(ori -> {
                String normalizedPath = getNormalizedPath(cri.getURITemplate().getValue(), ori.getURITemplate().getValue());
                operations.put(normalizedPath, cri);
                methods.put(Pair.of(ori.getHttpMethod(), normalizedPath), ori);
            });
        });
        List<Tag> tags = new ArrayList<>();
        oas.getPaths().forEach((pathKey, pathItem) -> {
            Tag tag = null;
            if (replaceTags && operations.containsKey(pathKey)) {
                ClassResourceInfo cri = operations.get(pathKey);
                tag = new Tag();
                tag.setName(cri.getURITemplate().getValue().replaceAll("/", "_"));
                if (javadocProvider != null) {
                    tag.setDescription(javadocProvider.getClassDoc(cri));
                }
                if (!tags.contains(tag)) {
                    tags.add(tag);
                }
            }
            for (Map.Entry<HttpMethod, Operation> subentry : pathItem.readOperationsMap().entrySet()) {
                if (replaceTags && tag != null) {
                    subentry.getValue().setTags(Collections.singletonList(tag.getName()));
                }
                Pair<String, String> key = Pair.of(subentry.getKey().name(), pathKey);
                if (methods.containsKey(key) && javadocProvider != null) {
                    OperationResourceInfo ori = methods.get(key);
                    if (StringUtils.isBlank(subentry.getValue().getSummary())) {
                        subentry.getValue().setSummary(javadocProvider.getMethodDoc(ori));
                    }
                    if (subentry.getValue().getParameters() == null) {
                        List<Parameter> parameters = new ArrayList<>();
                        addParameters(parameters);
                        subentry.getValue().setParameters(parameters);
                    } else {
                        for (int i = 0; i < subentry.getValue().getParameters().size(); i++) {
                            if (StringUtils.isBlank(subentry.getValue().getParameters().get(i).getDescription())) {
                                subentry.getValue().getParameters().get(i).setDescription(javadocProvider.getMethodParameterDoc(ori, i));
                            }
                        }
                        addParameters(subentry.getValue().getParameters());
                    }
                    if (subentry.getValue().getResponses() != null && !subentry.getValue().getResponses().isEmpty()) {
                        ApiResponse response = subentry.getValue().getResponses().entrySet().iterator().next().getValue();
                        if (StringUtils.isBlank(response.getDescription())) {
                            response.setDescription(javadocProvider.getMethodResponseDoc(ori));
                        }
                    }
                }
            }
        });
        if (replaceTags && oas.getTags() != null) {
            oas.setTags(tags);
        }
    }
}
Also used : HashMap(java.util.HashMap) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) ArrayList(java.util.ArrayList) Operation(io.swagger.v3.oas.models.Operation) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Parameter(io.swagger.v3.oas.models.parameters.Parameter) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Tag(io.swagger.v3.oas.models.tags.Tag) HashMap(java.util.HashMap) Map(java.util.Map) HttpMethod(io.swagger.v3.oas.models.PathItem.HttpMethod) Pair(org.apache.commons.lang3.tuple.Pair)

Example 3 with OpenAPI

use of io.swagger.v3.oas.models.OpenAPI 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)

Example 4 with OpenAPI

use of io.swagger.v3.oas.models.OpenAPI in project gravitee-management-rest-api by gravitee-io.

the class SwaggerServiceImpl method transformV3.

private OpenAPI transformV3(String content, PageConfigurationEntity config) {
    SwaggerParseResult result = new OpenAPIV3Parser().readContents(content, null, null);
    if (result != null && config != null && config.getTryItURL() != null) {
        URI newURI = URI.create(config.getTryItURL());
        result.getOpenAPI().getServers().forEach(server -> {
            try {
                server.setUrl(new URI(newURI.getScheme(), newURI.getUserInfo(), newURI.getHost(), newURI.getPort(), newURI.getPath(), newURI.getQuery(), newURI.getFragment()).toString());
            } catch (URISyntaxException e) {
                logger.error(e.getMessage(), e);
            }
        });
    }
    if (result != null) {
        return result.getOpenAPI();
    } else {
        return null;
    }
}
Also used : SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) URISyntaxException(java.net.URISyntaxException) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) URI(java.net.URI)

Example 5 with OpenAPI

use of io.swagger.v3.oas.models.OpenAPI in project vertx-web by vert-x3.

the class OpenAPI3RouterFactoryImpl method resolveOperationId.

private String resolveOperationId(HttpMethod method, String path) {
    // I assume the user give path in openapi style
    PathItem pathObject = this.spec.getPaths().get(path);
    if (pathObject == null) {
        throw RouterFactoryException.createPathNotFoundException(path);
    }
    Operation operation;
    switch(method) {
        case GET:
            operation = pathObject.getGet();
            break;
        case PUT:
            operation = pathObject.getPut();
            break;
        case HEAD:
            operation = pathObject.getHead();
            break;
        case DELETE:
            operation = pathObject.getDelete();
            break;
        case PATCH:
            operation = pathObject.getPatch();
            break;
        case POST:
            operation = pathObject.getPost();
            break;
        case OPTIONS:
            operation = pathObject.getOptions();
            break;
        case TRACE:
            operation = pathObject.getTrace();
            break;
        case OTHER:
        case CONNECT:
        default:
            throw RouterFactoryException.createPathNotFoundException(path);
    }
    return operation.getOperationId();
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) Operation(io.swagger.v3.oas.models.Operation)

Aggregations

Operation (io.swagger.v3.oas.models.Operation)2 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 JsonSchema (com.networknt.schema.JsonSchema)1 OpenApiConfigurationException (io.swagger.v3.oas.integration.OpenApiConfigurationException)1 SwaggerConfiguration (io.swagger.v3.oas.integration.SwaggerConfiguration)1 OpenApiContext (io.swagger.v3.oas.integration.api.OpenApiContext)1 OpenAPI (io.swagger.v3.oas.models.OpenAPI)1 PathItem (io.swagger.v3.oas.models.PathItem)1 HttpMethod (io.swagger.v3.oas.models.PathItem.HttpMethod)1 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)1 Schema (io.swagger.v3.oas.models.media.Schema)1 Parameter (io.swagger.v3.oas.models.parameters.Parameter)1 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)1 Tag (io.swagger.v3.oas.models.tags.Tag)1 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)1 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1