Search in sources :

Example 1 with SwaggerConfiguration

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

the class JaxrsApplicationScannerTest method shouldScanClassesFromApplicationOnlyIgnoringResourcePackages.

@Test(description = "scan classes from the Application only, ignoring resource packages")
public void shouldScanClassesFromApplicationOnlyIgnoringResourcePackages() throws Exception {
    SwaggerConfiguration openApiConfiguration = new SwaggerConfiguration();
    openApiConfiguration.setResourcePackages(Collections.singleton("com.my.project.resources"));
    scanner.setConfiguration(openApiConfiguration);
    assertEquals(scanner.classes().size(), 1);
    assertTrue(scanner.classes().contains(ResourceInPackageA.class));
}
Also used : SwaggerConfiguration(io.swagger.v3.oas.integration.SwaggerConfiguration) ResourceInPackageA(com.my.project.resources.ResourceInPackageA) Test(org.testng.annotations.Test)

Example 2 with SwaggerConfiguration

use of io.swagger.v3.oas.integration.SwaggerConfiguration 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 3 with SwaggerConfiguration

use of io.swagger.v3.oas.integration.SwaggerConfiguration 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 4 with SwaggerConfiguration

use of io.swagger.v3.oas.integration.SwaggerConfiguration 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 5 with SwaggerConfiguration

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

the class SwaggerMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (skip) {
        getLog().info("Skipping OpenAPI specification resolution");
        return;
    }
    getLog().info("Resolving OpenAPI specification..");
    if (project != null) {
        String pEnc = project.getProperties().getProperty("project.build.sourceEncoding");
        if (StringUtils.isNotBlank(pEnc)) {
            projectEncoding = pEnc;
        }
    }
    if (StringUtils.isBlank(encoding)) {
        encoding = projectEncoding;
    }
    // read swagger configuration if one was provided
    Optional<SwaggerConfiguration> swaggerConfiguration = readStructuredDataFromFile(configurationFilePath, SwaggerConfiguration.class, "configurationFilePath");
    // read openApi config, if one was provided
    Optional<OpenAPI> openAPIInput = readStructuredDataFromFile(openapiFilePath, OpenAPI.class, "openapiFilePath");
    config = mergeConfig(openAPIInput.orElse(null), swaggerConfiguration.orElse(new SwaggerConfiguration()));
    setDefaultsIfMissing(config);
    try {
        GenericOpenApiContextBuilder builder = new JaxrsOpenApiContextBuilder().openApiConfiguration(config);
        if (StringUtils.isNotBlank(contextId)) {
            builder.ctxId(contextId);
        }
        OpenApiContext context = builder.buildContext(true);
        OpenAPI openAPI = context.read();
        if (StringUtils.isNotBlank(config.getFilterClass())) {
            try {
                OpenAPISpecFilter filterImpl = (OpenAPISpecFilter) this.getClass().getClassLoader().loadClass(config.getFilterClass()).newInstance();
                SpecFilter f = new SpecFilter();
                openAPI = f.filter(openAPI, filterImpl, new HashMap<>(), new HashMap<>(), new HashMap<>());
            } catch (Exception e) {
                getLog().error("Error applying filter to API specification", e);
                throw new MojoExecutionException("Error applying filter to API specification: " + e.getMessage(), e);
            }
        }
        String openapiJson = null;
        String openapiYaml = null;
        if (Format.JSON.equals(outputFormat) || Format.JSONANDYAML.equals(outputFormat)) {
            if (config.isPrettyPrint() != null && config.isPrettyPrint()) {
                openapiJson = context.getOutputJsonMapper().writer(new DefaultPrettyPrinter()).writeValueAsString(openAPI);
            } else {
                openapiJson = context.getOutputJsonMapper().writeValueAsString(openAPI);
            }
        }
        if (Format.YAML.equals(outputFormat) || Format.JSONANDYAML.equals(outputFormat)) {
            if (config.isPrettyPrint() != null && config.isPrettyPrint()) {
                openapiYaml = context.getOutputYamlMapper().writer(new DefaultPrettyPrinter()).writeValueAsString(openAPI);
            } else {
                openapiYaml = context.getOutputYamlMapper().writeValueAsString(openAPI);
            }
        }
        Path path = Paths.get(outputPath, "temp");
        final File parentFile = path.toFile().getParentFile();
        if (parentFile != null) {
            parentFile.mkdirs();
        }
        if (openapiJson != null) {
            path = Paths.get(outputPath, outputFileName + ".json");
            Files.write(path, openapiJson.getBytes(Charset.forName(encoding)));
            getLog().info("JSON output: " + path.toFile().getCanonicalPath());
        }
        if (openapiYaml != null) {
            path = Paths.get(outputPath, outputFileName + ".yaml");
            Files.write(path, openapiYaml.getBytes(Charset.forName(encoding)));
            getLog().info("YAML output: " + path.toFile().getCanonicalPath());
        }
    } catch (OpenApiConfigurationException e) {
        getLog().error("Error resolving API specification", e);
        throw new MojoFailureException(e.getMessage(), e);
    } catch (IOException e) {
        getLog().error("Error writing API specification", e);
        throw new MojoExecutionException("Failed to write API definition", e);
    } catch (Exception e) {
        getLog().error("Error resolving API specification", e);
        throw new MojoExecutionException(e.getMessage(), e);
    }
}
Also used : DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) Path(java.nio.file.Path) OpenAPISpecFilter(io.swagger.v3.core.filter.OpenAPISpecFilter) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) GenericOpenApiContextBuilder(io.swagger.v3.oas.integration.GenericOpenApiContextBuilder) OpenApiConfigurationException(io.swagger.v3.oas.integration.OpenApiConfigurationException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) IOException(java.io.IOException) SwaggerConfiguration(io.swagger.v3.oas.integration.SwaggerConfiguration) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) OpenApiConfigurationException(io.swagger.v3.oas.integration.OpenApiConfigurationException) JaxrsOpenApiContextBuilder(io.swagger.v3.jaxrs2.integration.JaxrsOpenApiContextBuilder) OpenAPI(io.swagger.v3.oas.models.OpenAPI) File(java.io.File) SpecFilter(io.swagger.v3.core.filter.SpecFilter) OpenAPISpecFilter(io.swagger.v3.core.filter.OpenAPISpecFilter) OpenApiContext(io.swagger.v3.oas.integration.api.OpenApiContext)

Aggregations

SwaggerConfiguration (io.swagger.v3.oas.integration.SwaggerConfiguration)16 OpenAPI (io.swagger.v3.oas.models.OpenAPI)11 OpenApiContext (io.swagger.v3.oas.integration.api.OpenApiContext)10 Test (org.testng.annotations.Test)9 GenericOpenApiContext (io.swagger.v3.oas.integration.GenericOpenApiContext)6 Info (io.swagger.v3.oas.models.info.Info)6 OpenApiConfigurationException (io.swagger.v3.oas.integration.OpenApiConfigurationException)5 HashSet (java.util.HashSet)5 DefaultPrettyPrinter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter)4 Reader (io.swagger.v3.jaxrs2.Reader)4 IOException (java.io.IOException)3 Collectors (java.util.stream.Collectors)3 ResourceInPackageA (com.my.project.resources.ResourceInPackageA)2 OpenAPISpecFilter (io.swagger.v3.core.filter.OpenAPISpecFilter)2 SpecFilter (io.swagger.v3.core.filter.SpecFilter)2 JaxrsOpenApiContextBuilder (io.swagger.v3.jaxrs2.integration.JaxrsOpenApiContextBuilder)2 OpenAPIDefinition (io.swagger.v3.oas.annotations.OpenAPIDefinition)2 GenericOpenApiContextBuilder (io.swagger.v3.oas.integration.GenericOpenApiContextBuilder)2 IgnoredPackages (io.swagger.v3.oas.integration.IgnoredPackages)2 OpenAPIConfiguration (io.swagger.v3.oas.integration.api.OpenAPIConfiguration)2