Search in sources :

Example 16 with OAIDescriptor

use of io.gravitee.rest.api.service.swagger.OAIDescriptor in project gravitee-management-rest-api by gravitee-io.

the class SwaggerService_ParseTest method shouldThrowInfoNameMissingWhenFailIfErrorsTrue.

@Test
public void shouldThrowInfoNameMissingWhenFailIfErrorsTrue() throws IOException {
    PageEntity pageEntity = getPage("io/gravitee/rest/api/management/service/swagger-v2-no-info-name.json", MediaType.APPLICATION_JSON);
    OAIDescriptor descriptor = (OAIDescriptor) swaggerService.parse(pageEntity.getContent());
    assertNotNull(descriptor.getMessages());
    assertEquals("attribute info.title is missing", descriptor.getMessages().get(0));
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) OAIDescriptor(io.gravitee.rest.api.service.swagger.OAIDescriptor) Test(org.junit.Test)

Example 17 with OAIDescriptor

use of io.gravitee.rest.api.service.swagger.OAIDescriptor in project gravitee-management-rest-api by gravitee-io.

the class EntrypointsOAITransformer method transform.

@Override
public void transform(OAIDescriptor descriptor) {
    if (asBoolean(SwaggerProperties.ENTRYPOINTS_AS_SERVERS) && entrypoints != null && !entrypoints.isEmpty()) {
        List<Server> servers = new ArrayList<>();
        // Add server according to entrypoints
        entrypoints.forEach(entrypoint -> {
            Server server = new Server();
            if (asBoolean(SwaggerProperties.ENTRYPOINT_AS_BASEPATH)) {
                server.setUrl(entrypoint.getTarget());
            } else {
                URI target = URI.create(entrypoint.getTarget());
                server.setUrl(entrypoint.getTarget().substring(0, entrypoint.getTarget().indexOf(target.getRawPath())));
            }
            servers.add(server);
        });
        descriptor.getSpecification().setServers(servers);
    } else if (asBoolean(SwaggerProperties.ENTRYPOINT_AS_BASEPATH) && descriptor.getSpecification().getServers() != null) {
        // Replace the server path with the api context-path.
        descriptor.getSpecification().getServers().forEach(server -> {
            final URI newURI = URI.create(server.getUrl());
            try {
                server.setUrl(new URI(newURI.getScheme(), newURI.getUserInfo(), newURI.getHost(), newURI.getPort(), this.contextPath, newURI.getQuery(), newURI.getFragment()).toString());
            } catch (URISyntaxException e) {
                logger.error(e.getMessage(), e);
            }
        });
    }
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) List(java.util.List) Server(io.swagger.v3.oas.models.servers.Server) AbstractPageConfigurationSwaggerTransformer(io.gravitee.rest.api.service.impl.swagger.transformer.page.AbstractPageConfigurationSwaggerTransformer) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) ApiEntrypointEntity(io.gravitee.rest.api.model.api.ApiEntrypointEntity) OAIDescriptor(io.gravitee.rest.api.service.swagger.OAIDescriptor) URI(java.net.URI) SwaggerProperties(io.gravitee.rest.api.service.impl.swagger.SwaggerProperties) ArrayList(java.util.ArrayList) Server(io.swagger.v3.oas.models.servers.Server) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 18 with OAIDescriptor

use of io.gravitee.rest.api.service.swagger.OAIDescriptor in project gravitee-management-rest-api by gravitee-io.

the class SwaggerServiceImpl method parse.

public SwaggerDescriptor parse(String content, boolean wsdl, ParseOptions options) {
    OpenAPI descriptor;
    if (isUrl(content)) {
        UrlSanitizerUtils.checkAllowed(content, importConfiguration.getImportWhitelist(), importConfiguration.isAllowImportFromPrivate());
    }
    if (wsdl) {
        // try to read wsdl
        logger.debug("Trying to load a Wsdl descriptor");
        descriptor = new WsdlParser().parse(content);
        if (descriptor != null) {
            return new OAIDescriptor(descriptor);
        }
    } else {
        OAIDescriptor oaiDescriptor = new OAIParser().parse(content, options);
        if (oaiDescriptor == null || oaiDescriptor.getSpecification() == null) {
            throw new SwaggerDescriptorException();
        }
        return oaiDescriptor;
    }
    throw new SwaggerDescriptorException();
}
Also used : OAIDescriptor(io.gravitee.rest.api.service.swagger.OAIDescriptor) SwaggerDescriptorException(io.gravitee.rest.api.service.exceptions.SwaggerDescriptorException) OAIParser(io.gravitee.rest.api.service.impl.swagger.parser.OAIParser) WsdlParser(io.gravitee.rest.api.service.impl.swagger.parser.WsdlParser) OpenAPI(io.swagger.v3.oas.models.OpenAPI)

Aggregations

OAIDescriptor (io.gravitee.rest.api.service.swagger.OAIDescriptor)18 PageEntity (io.gravitee.rest.api.model.PageEntity)16 Test (org.junit.Test)15 PageConfigurationOAITransformer (io.gravitee.rest.api.service.impl.swagger.transformer.page.PageConfigurationOAITransformer)8 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 EntrypointsOAITransformer (io.gravitee.rest.api.service.impl.swagger.transformer.entrypoints.EntrypointsOAITransformer)6 SwaggerDescriptorException (io.gravitee.rest.api.service.exceptions.SwaggerDescriptorException)2 List (java.util.List)2 ApiEntrypointEntity (io.gravitee.rest.api.model.api.ApiEntrypointEntity)1 SwaggerProperties (io.gravitee.rest.api.service.impl.swagger.SwaggerProperties)1 OAIParser (io.gravitee.rest.api.service.impl.swagger.parser.OAIParser)1 WsdlParser (io.gravitee.rest.api.service.impl.swagger.parser.WsdlParser)1 AbstractPageConfigurationSwaggerTransformer (io.gravitee.rest.api.service.impl.swagger.transformer.page.AbstractPageConfigurationSwaggerTransformer)1 OpenAPIParser (io.swagger.parser.OpenAPIParser)1 OpenAPI (io.swagger.v3.oas.models.OpenAPI)1 Server (io.swagger.v3.oas.models.servers.Server)1 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)1 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)1 BufferedWriter (java.io.BufferedWriter)1