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));
}
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());
}
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"));
}
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"));
}
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);
}
}
Aggregations