Search in sources :

Example 1 with OpenApiReader

use of io.swagger.v3.oas.integration.api.OpenApiReader in project swagger-core by swagger-api.

the class JaxrsApplicationAndResourcePackagesAnnotationScannerTest method shouldScanForClassesWhenApplicationIsNotSet.

@Test(description = "scan classes from Application when it is not set")
public void shouldScanForClassesWhenApplicationIsNotSet() throws Exception {
    SwaggerConfiguration config = new SwaggerConfiguration().openAPI(new OpenAPI().info(new Info().description("TEST INFO DESC")));
    OpenApiContext ctx = new GenericOpenApiContext<>().openApiConfiguration(config).openApiReader(new Reader(config)).openApiScanner(scanner.openApiConfiguration(config)).init();
    OpenAPI openApi = ctx.read();
    assertNotNull(openApi);
    assertNull(openApi.getPaths());
}
Also used : Reader(io.swagger.v3.jaxrs2.Reader) GenericOpenApiContext(io.swagger.v3.oas.integration.GenericOpenApiContext) Info(io.swagger.v3.oas.models.info.Info) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SwaggerConfiguration(io.swagger.v3.oas.integration.SwaggerConfiguration) GenericOpenApiContext(io.swagger.v3.oas.integration.GenericOpenApiContext) OpenApiContext(io.swagger.v3.oas.integration.api.OpenApiContext) Test(org.testng.annotations.Test)

Example 2 with OpenApiReader

use of io.swagger.v3.oas.integration.api.OpenApiReader in project swagger-core by swagger-api.

the class JaxrsApplicationAndResourcePackagesAnnotationScannerTest method shouldScanOnlyApplicationClasses.

@Test(description = "scan classes from Application")
public void shouldScanOnlyApplicationClasses() throws Exception {
    SwaggerConfiguration config = new SwaggerConfiguration().openAPI(new OpenAPI().info(new Info().description("TEST INFO DESC")));
    Application application = new Application() {

        @Override
        public Set<Class<?>> getClasses() {
            return Collections.singleton(ResourceInPackageA.class);
        }
    };
    OpenApiContext ctx = new GenericOpenApiContext<>().openApiConfiguration(config).openApiReader(new Reader(config)).openApiScanner(scanner.application(application).openApiConfiguration(config)).init();
    OpenAPI openApi = ctx.read();
    assertNotNull(openApi);
    assertEquals(openApi.getPaths().keySet(), Arrays.asList("/packageA"));
}
Also used : Reader(io.swagger.v3.jaxrs2.Reader) GenericOpenApiContext(io.swagger.v3.oas.integration.GenericOpenApiContext) Info(io.swagger.v3.oas.models.info.Info) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Application(javax.ws.rs.core.Application) SwaggerConfiguration(io.swagger.v3.oas.integration.SwaggerConfiguration) GenericOpenApiContext(io.swagger.v3.oas.integration.GenericOpenApiContext) OpenApiContext(io.swagger.v3.oas.integration.api.OpenApiContext) Test(org.testng.annotations.Test)

Example 3 with OpenApiReader

use of io.swagger.v3.oas.integration.api.OpenApiReader in project swagger-core by swagger-api.

the class JaxrsApplicationAndResourcePackagesAnnotationScannerTest method shouldScanOnlyResourcePackagesClasses.

@Test(description = "scan classes from resource packages only")
public void shouldScanOnlyResourcePackagesClasses() throws Exception {
    SwaggerConfiguration config = new SwaggerConfiguration().resourcePackages(Stream.of("com.my.project.resources", "org.my.project.resources").collect(Collectors.toSet())).openAPI(new OpenAPI().info(new Info().description("TEST INFO DESC")));
    OpenApiContext ctx = new GenericOpenApiContext<>().openApiConfiguration(config).openApiReader(new Reader(config)).openApiScanner(new JaxrsApplicationAndResourcePackagesAnnotationScanner().openApiConfiguration(config)).init();
    OpenAPI openApi = ctx.read();
    assertNotNull(openApi);
    assertEquals(openApi.getPaths().keySet(), Arrays.asList("/packageA", "/packageB"));
}
Also used : Reader(io.swagger.v3.jaxrs2.Reader) GenericOpenApiContext(io.swagger.v3.oas.integration.GenericOpenApiContext) Info(io.swagger.v3.oas.models.info.Info) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SwaggerConfiguration(io.swagger.v3.oas.integration.SwaggerConfiguration) GenericOpenApiContext(io.swagger.v3.oas.integration.GenericOpenApiContext) OpenApiContext(io.swagger.v3.oas.integration.api.OpenApiContext) Test(org.testng.annotations.Test)

Example 4 with OpenApiReader

use of io.swagger.v3.oas.integration.api.OpenApiReader in project swagger-core by swagger-api.

the class GenericOpenApiContext method init.

@Override
public T init() throws OpenApiConfigurationException {
    if (openApiConfiguration == null) {
        openApiConfiguration = loadConfiguration();
    }
    if (openApiConfiguration == null) {
        openApiConfiguration = new SwaggerConfiguration().resourcePackages(resourcePackages).resourceClasses(resourceClasses);
        ((SwaggerConfiguration) openApiConfiguration).setId(id);
        ((SwaggerConfiguration) openApiConfiguration).setOpenAPI31(openAPI31);
    }
    openApiConfiguration = mergeParentConfiguration(openApiConfiguration, parent);
    try {
        if (openApiReader == null) {
            openApiReader = buildReader(ContextUtils.deepCopy(openApiConfiguration));
        }
        if (openApiScanner == null) {
            openApiScanner = buildScanner(ContextUtils.deepCopy(openApiConfiguration));
        }
        if (objectMapperProcessor == null) {
            objectMapperProcessor = buildObjectMapperProcessor(ContextUtils.deepCopy(openApiConfiguration));
        }
        if (modelConverters == null || modelConverters.isEmpty()) {
            modelConverters = buildModelConverters(ContextUtils.deepCopy(openApiConfiguration));
        }
        if (outputJsonMapper == null) {
            if (Boolean.TRUE.equals(openApiConfiguration.isOpenAPI31())) {
                outputJsonMapper = Json31.mapper().copy();
            } else {
                outputJsonMapper = Json.mapper().copy();
            }
        }
        if (outputYamlMapper == null) {
            if (Boolean.TRUE.equals(openApiConfiguration.isOpenAPI31())) {
                outputYamlMapper = Yaml31.mapper().copy();
            } else {
                outputYamlMapper = Yaml.mapper().copy();
            }
        }
        if (openApiConfiguration.isSortOutput() != null && openApiConfiguration.isSortOutput()) {
            outputJsonMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
            outputJsonMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
            outputYamlMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
            outputYamlMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
            if (Boolean.TRUE.equals(openApiConfiguration.isOpenAPI31())) {
                outputJsonMapper.addMixIn(OpenAPI.class, SortedOpenAPIMixin31.class);
                outputJsonMapper.addMixIn(Schema.class, SortedSchemaMixin31.class);
                outputYamlMapper.addMixIn(OpenAPI.class, SortedOpenAPIMixin31.class);
                outputYamlMapper.addMixIn(Schema.class, SortedSchemaMixin31.class);
            } else {
                outputJsonMapper.addMixIn(OpenAPI.class, SortedOpenAPIMixin.class);
                outputJsonMapper.addMixIn(Schema.class, SortedSchemaMixin.class);
                outputYamlMapper.addMixIn(OpenAPI.class, SortedOpenAPIMixin.class);
                outputYamlMapper.addMixIn(Schema.class, SortedSchemaMixin.class);
            }
        }
    } catch (Exception e) {
        LOGGER.error("error initializing context: " + e.getMessage(), e);
        throw new OpenApiConfigurationException("error initializing context: " + e.getMessage(), e);
    }
    try {
        if (objectMapperProcessor != null) {
            ObjectMapper mapper = IntegrationObjectMapperFactory.createJson();
            objectMapperProcessor.processJsonObjectMapper(mapper);
            ModelConverters.getInstance().addConverter(new ModelResolver(mapper));
            objectMapperProcessor.processOutputJsonObjectMapper(outputJsonMapper);
            objectMapperProcessor.processOutputYamlObjectMapper(outputYamlMapper);
        }
    } catch (Exception e) {
        LOGGER.error("error configuring objectMapper: " + e.getMessage(), e);
        throw new OpenApiConfigurationException("error configuring objectMapper: " + e.getMessage(), e);
    }
    try {
        if (modelConverters != null && !modelConverters.isEmpty()) {
            for (ModelConverter converter : modelConverters) {
                ModelConverters.getInstance().addConverter(converter);
            }
        }
    } catch (Exception e) {
        LOGGER.error("error configuring model converters: " + e.getMessage(), e);
        throw new OpenApiConfigurationException("error configuring model converters: " + e.getMessage(), e);
    }
    // set cache TTL if present in configuration
    if (openApiConfiguration.getCacheTTL() != null) {
        this.cacheTTL = openApiConfiguration.getCacheTTL();
    }
    register();
    return (T) this;
}
Also used : IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ModelConverter(io.swagger.v3.core.converter.ModelConverter) ModelResolver(io.swagger.v3.core.jackson.ModelResolver)

Example 5 with OpenApiReader

use of io.swagger.v3.oas.integration.api.OpenApiReader in project swagger-core by swagger-api.

the class GenericOpenApiContext method buildReader.

protected OpenApiReader buildReader(final OpenAPIConfiguration openApiConfiguration) throws Exception {
    OpenApiReader reader;
    if (StringUtils.isNotBlank(openApiConfiguration.getReaderClass())) {
        Class cls = getClass().getClassLoader().loadClass(openApiConfiguration.getReaderClass());
        reader = (OpenApiReader) cls.newInstance();
    } else {
        reader = new OpenApiReader() {

            OpenAPIConfiguration openApiConfiguration;

            @Override
            public void setConfiguration(OpenAPIConfiguration openApiConfiguration) {
                this.openApiConfiguration = openApiConfiguration;
            }

            @Override
            public OpenAPI read(Set<Class<?>> classes, Map<String, Object> resources) {
                return openApiConfiguration.getOpenAPI();
            }
        };
    }
    reader.setConfiguration(openApiConfiguration);
    return reader;
}
Also used : OpenAPIConfiguration(io.swagger.v3.oas.integration.api.OpenAPIConfiguration) OpenAPI(io.swagger.v3.oas.models.OpenAPI) OpenApiReader(io.swagger.v3.oas.integration.api.OpenApiReader)

Aggregations

Reader (io.swagger.v3.jaxrs2.Reader)5 OpenAPI (io.swagger.v3.oas.models.OpenAPI)5 GenericOpenApiContext (io.swagger.v3.oas.integration.GenericOpenApiContext)4 SwaggerConfiguration (io.swagger.v3.oas.integration.SwaggerConfiguration)4 OpenApiContext (io.swagger.v3.oas.integration.api.OpenApiContext)4 Info (io.swagger.v3.oas.models.info.Info)4 Test (org.testng.annotations.Test)4 OpenApiReader (io.swagger.v3.oas.integration.api.OpenApiReader)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ModelConverter (io.swagger.v3.core.converter.ModelConverter)1 ModelResolver (io.swagger.v3.core.jackson.ModelResolver)1 OpenAPIConfiguration (io.swagger.v3.oas.integration.api.OpenAPIConfiguration)1 IOException (java.io.IOException)1 Application (javax.ws.rs.core.Application)1