Search in sources :

Example 1 with OAIDescriptor

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

the class OAIParser method parse.

public OAIDescriptor parse(String content, ParseOptions options) {
    OpenAPIParser parser = new OpenAPIParser();
    SwaggerParseResult parseResult;
    String path = content;
    File temp = null;
    if (!isLocationUrl(content)) {
        // Swagger v1 supports only a URL to read swagger: create temporary file for Swagger parser
        temp = createTempFile(content);
        path = temp.getAbsolutePath();
    }
    parseResult = parser.readLocation(path, null, options);
    if (temp != null) {
        temp.delete();
    }
    /* Hack due to swagger v1 converting issue
         * See https://github.com/swagger-api/swagger-parser/issues/1451
         */
    if (parseResult.getMessages() != null) {
        final List<String> filteredMessages = parseResult.getMessages().stream().filter(message -> message != null && !message.matches("^attribute info.contact.*")).collect(Collectors.toList());
        parseResult.setMessages(filteredMessages);
    }
    if (parseResult.getOpenAPI() == null) {
        throw new SwaggerDescriptorException("Malformed descriptor");
    }
    OAIDescriptor descriptor = new OAIDescriptor(parseResult.getOpenAPI());
    descriptor.setMessages(parseResult.getMessages());
    return descriptor;
}
Also used : SwaggerDescriptorException(io.gravitee.rest.api.service.exceptions.SwaggerDescriptorException) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIParser(io.swagger.parser.OpenAPIParser) Logger(org.slf4j.Logger) BufferedWriter(java.io.BufferedWriter) FileWriter(java.io.FileWriter) LoggerFactory(org.slf4j.LoggerFactory) OAIDescriptor(io.gravitee.rest.api.service.swagger.OAIDescriptor) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) List(java.util.List) OAIDescriptor(io.gravitee.rest.api.service.swagger.OAIDescriptor) SwaggerDescriptorException(io.gravitee.rest.api.service.exceptions.SwaggerDescriptorException) OpenAPIParser(io.swagger.parser.OpenAPIParser) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) File(java.io.File)

Example 2 with OAIDescriptor

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

the class SwaggerService_TransformTest method shouldTransformAPIWithServerUrlAndContextPath.

@Test
public void shouldTransformAPIWithServerUrlAndContextPath() throws IOException {
    PageEntity pageEntity = getPage("io/gravitee/rest/api/management/service/openapi.json", MediaType.APPLICATION_JSON);
    Map<String, String> pageConfigurationEntity = new HashMap<>();
    pageConfigurationEntity.put(SwaggerProperties.ENTRYPOINTS_AS_SERVERS, "true");
    pageConfigurationEntity.put(SwaggerProperties.ENTRYPOINT_AS_BASEPATH, "true");
    pageEntity.setConfiguration(pageConfigurationEntity);
    OAIDescriptor descriptor = (OAIDescriptor) swaggerService.parse(pageEntity.getContent(), false);
    final ApiEntity apiEntity = getApiEntity();
    swaggerService.transform(descriptor, Arrays.asList(new PageConfigurationOAITransformer(pageEntity), new EntrypointsOAITransformer(pageEntity, apiEntity)));
    assertNotNull(descriptor.toJson());
    final JsonNode node = Json.mapper().readTree(descriptor.toJson());
    assertEquals("https://apis.gravitee.io/test", node.get("servers").get(0).get("url").asText());
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) OAIDescriptor(io.gravitee.rest.api.service.swagger.OAIDescriptor) EntrypointsOAITransformer(io.gravitee.rest.api.service.impl.swagger.transformer.entrypoints.EntrypointsOAITransformer) PageConfigurationOAITransformer(io.gravitee.rest.api.service.impl.swagger.transformer.page.PageConfigurationOAITransformer) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 3 with OAIDescriptor

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

the class SwaggerService_TransformTest method shouldTransformAPIWithOriginalServer.

@Test
public void shouldTransformAPIWithOriginalServer() throws IOException {
    PageEntity pageEntity = getPage("io/gravitee/rest/api/management/service/openapi.json", MediaType.APPLICATION_JSON);
    Map<String, String> pageConfigurationEntity = new HashMap<>();
    pageConfigurationEntity.put(SwaggerProperties.ENTRYPOINTS_AS_SERVERS, "false");
    pageConfigurationEntity.put(SwaggerProperties.ENTRYPOINT_AS_BASEPATH, "false");
    pageEntity.setConfiguration(pageConfigurationEntity);
    OAIDescriptor descriptor = (OAIDescriptor) swaggerService.parse(pageEntity.getContent(), false);
    final ApiEntity apiEntity = getApiEntity();
    swaggerService.transform(descriptor, Arrays.asList(new PageConfigurationOAITransformer(pageEntity), new EntrypointsOAITransformer(pageEntity, apiEntity)));
    assertNotNull(descriptor.toJson());
    final JsonNode node = Json.mapper().readTree(descriptor.toJson());
    assertEquals("https://demo.gravitee.io/gateway/echo", node.get("servers").get(0).get("url").asText());
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) OAIDescriptor(io.gravitee.rest.api.service.swagger.OAIDescriptor) EntrypointsOAITransformer(io.gravitee.rest.api.service.impl.swagger.transformer.entrypoints.EntrypointsOAITransformer) PageConfigurationOAITransformer(io.gravitee.rest.api.service.impl.swagger.transformer.page.PageConfigurationOAITransformer) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 4 with OAIDescriptor

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

the class SwaggerService_TransformTest method shouldTransformAPIFromSwaggerV3_yaml.

@Test
public void shouldTransformAPIFromSwaggerV3_yaml() throws IOException {
    PageEntity pageEntity = getPage("io/gravitee/rest/api/management/service/openapi.yaml", MediaType.TEXT_PLAIN);
    OAIDescriptor descriptor = (OAIDescriptor) swaggerService.parse(pageEntity.getContent(), false);
    swaggerService.transform(descriptor, Collections.singleton(new PageConfigurationOAITransformer(pageEntity)));
    assertNotNull(descriptor.toYaml());
    validateV3(Yaml.mapper().readTree(descriptor.toYaml()));
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) OAIDescriptor(io.gravitee.rest.api.service.swagger.OAIDescriptor) PageConfigurationOAITransformer(io.gravitee.rest.api.service.impl.swagger.transformer.page.PageConfigurationOAITransformer) Test(org.junit.Test)

Example 5 with OAIDescriptor

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

the class SwaggerService_TransformTest method shouldTransformAPIWithServerUrl.

@Test
public void shouldTransformAPIWithServerUrl() throws IOException {
    PageEntity pageEntity = getPage("io/gravitee/rest/api/management/service/openapi.json", MediaType.APPLICATION_JSON);
    Map<String, String> pageConfigurationEntity = new HashMap<>();
    pageConfigurationEntity.put(SwaggerProperties.ENTRYPOINTS_AS_SERVERS, "true");
    pageEntity.setConfiguration(pageConfigurationEntity);
    OAIDescriptor descriptor = (OAIDescriptor) swaggerService.parse(pageEntity.getContent(), false);
    final ApiEntity apiEntity = getApiEntity();
    swaggerService.transform(descriptor, Arrays.asList(new PageConfigurationOAITransformer(pageEntity), new EntrypointsOAITransformer(pageEntity, apiEntity)));
    assertNotNull(descriptor.toJson());
    final JsonNode node = Json.mapper().readTree(descriptor.toJson());
    assertEquals("https://apis.gravitee.io", node.get("servers").get(0).get("url").asText());
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) OAIDescriptor(io.gravitee.rest.api.service.swagger.OAIDescriptor) EntrypointsOAITransformer(io.gravitee.rest.api.service.impl.swagger.transformer.entrypoints.EntrypointsOAITransformer) PageConfigurationOAITransformer(io.gravitee.rest.api.service.impl.swagger.transformer.page.PageConfigurationOAITransformer) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

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