use of org.wildfly.swarm.microprofile.openapi.api.models.OpenAPIImpl in project wildfly-swarm by wildfly-swarm.
the class OpenApiAnnotationScanner method jaxRsApplicationToOpenApi.
/**
* Processes a JAX-RS {@link Application} and creates an {@link OpenAPI} model. Performs
* annotation scanning and other processing. Returns a model unique to that single JAX-RS
* app.
* @param applicationClass
*/
private OpenAPIImpl jaxRsApplicationToOpenApi(ClassInfo applicationClass) {
OpenAPIImpl oai = new OpenAPIImpl();
oai.setOpenapi(OpenApiConstants.OPEN_API_VERSION);
// Get the @ApplicationPath info and save it for later (also support @Path which seems nonstandard but common).
// //////////////////////////////////////
AnnotationInstance appPathAnno = JandexUtil.getClassAnnotation(applicationClass, OpenApiConstants.DOTNAME_APPLICATION_PATH);
if (appPathAnno == null) {
appPathAnno = JandexUtil.getClassAnnotation(applicationClass, OpenApiConstants.DOTNAME_PATH);
}
if (appPathAnno != null) {
this.currentAppPath = appPathAnno.value().asString();
} else {
this.currentAppPath = "/";
}
// Get the @OpenAPIDefinition annotation and process it.
// //////////////////////////////////////
AnnotationInstance openApiDefAnno = JandexUtil.getClassAnnotation(applicationClass, OpenApiConstants.DOTNAME_OPEN_API_DEFINITION);
if (openApiDefAnno != null) {
processDefinition(oai, openApiDefAnno);
}
// Process @SecurityScheme annotations
// //////////////////////////////////////
List<AnnotationInstance> securitySchemeAnnotations = JandexUtil.getRepeatableAnnotation(applicationClass, OpenApiConstants.DOTNAME_SECURITY_SCHEME, OpenApiConstants.DOTNAME_SECURITY_SCHEMES);
for (AnnotationInstance annotation : securitySchemeAnnotations) {
String name = JandexUtil.stringValue(annotation, OpenApiConstants.PROP_SECURITY_SCHEME_NAME);
if (name == null && JandexUtil.isRef(annotation)) {
name = JandexUtil.nameFromRef(annotation);
}
if (name != null) {
SecurityScheme securityScheme = readSecurityScheme(annotation);
Components components = ModelUtil.components(oai);
components.addSecurityScheme(name, securityScheme);
}
}
// Process @Server annotations
// /////////////////////////////////
List<AnnotationInstance> serverAnnotations = JandexUtil.getRepeatableAnnotation(applicationClass, OpenApiConstants.DOTNAME_SERVER, OpenApiConstants.DOTNAME_SERVERS);
for (AnnotationInstance annotation : serverAnnotations) {
Server server = readServer(annotation);
oai.addServer(server);
}
return oai;
}
use of org.wildfly.swarm.microprofile.openapi.api.models.OpenAPIImpl in project wildfly-swarm by wildfly-swarm.
the class FilterUtilTest method testApplyFilter.
/**
* Test method for {@link org.wildfly.swarm.microprofile.openapi.api.util.FilterUtil#applyFilter(org.eclipse.microprofile.openapi.OASFilter, org.eclipse.microprofile.openapi.models.OpenAPI)}.
* @throws Exception
*/
@Test
public void testApplyFilter() throws Exception {
URL beforeUrl = FilterUtilTest.class.getResource("filter-before.json");
URL afterUrl = FilterUtilTest.class.getResource("filter-after.json");
OpenAPIImpl model = OpenApiParser.parse(beforeUrl);
OASFilter filter = filter();
model = (OpenAPIImpl) FilterUtil.applyFilter(filter, model);
String actual = OpenApiSerializer.serialize(model, Format.JSON);
String expected = loadResource(afterUrl);
assertJsonEquals(expected, actual);
}
use of org.wildfly.swarm.microprofile.openapi.api.models.OpenAPIImpl in project wildfly-swarm by wildfly-swarm.
the class MergeUtilTest method doTest.
/**
* Performs a single full merge test. Two documents are loaded (as resources) and then
* merged. The expected merge result is then loaded and compared with the actual result.
* @param resource1
* @param resource2
* @param expected
* @throws IOException
* @throws ParseException
* @throws JSONException
*/
private static void doTest(String resource1, String resource2, String expected) throws IOException, ParseException, JSONException {
URL resource1Url = MergeUtilTest.class.getResource(resource1);
URL resource2Url = MergeUtilTest.class.getResource(resource2);
URL expectedUrl = MergeUtilTest.class.getResource(expected);
String expectedContent = loadResource(expectedUrl);
OpenAPIImpl resource1Model = OpenApiParser.parse(resource1Url);
OpenAPIImpl resource2Model = OpenApiParser.parse(resource2Url);
OpenAPIImpl actualModel = MergeUtil.merge(resource1Model, resource2Model);
String actual = OpenApiSerializer.serialize(actualModel, Format.JSON);
assertJsonEquals(expectedContent, actual);
}
use of org.wildfly.swarm.microprofile.openapi.api.models.OpenAPIImpl in project wildfly-swarm by wildfly-swarm.
the class OpenApiServletContextListener method modelFromReader.
/**
* Instantiate the configured {@link OASModelReader} and invoke it. If no reader is configured, then return null. If a class is configured but there is an
* error either instantiating or invokig it, a {@link RuntimeException} is thrown.
*/
private OpenAPIImpl modelFromReader() {
String readerClassName = config.getOptionalValue(OASConfig.MODEL_READER, String.class).orElse(null);
if (readerClassName == null) {
return null;
}
try {
ClassLoader cl = getContextClassLoader();
if (cl == null) {
cl = OpenApiServletContextListener.class.getClassLoader();
}
Class<?> c = cl.loadClass(readerClassName);
OASModelReader reader = (OASModelReader) c.newInstance();
return (OpenAPIImpl) reader.buildModel();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
use of org.wildfly.swarm.microprofile.openapi.api.models.OpenAPIImpl in project wildfly-swarm by wildfly-swarm.
the class OpenApiParserAndSerializerTest method doTest.
/**
* Performs a full round-trip parse+serialize test on a single resource.
* @param resource
* @param format
* @throws IOException
* @throws ParseException
* @throws JSONException
*/
private static void doTest(String resource, Format format) throws IOException, ParseException, JSONException {
URL testResource = OpenApiParserAndSerializerTest.class.getResource(resource);
String original = loadResource(testResource);
OpenAPIImpl impl = OpenApiParser.parse(testResource);
String roundTrip = OpenApiSerializer.serialize(impl, format);
try {
if (format == Format.JSON) {
assertJsonEquals(original, roundTrip);
} else {
assertYamlEquals(original, roundTrip);
}
} catch (AssertionError e) {
System.out.println("================");
System.out.println(roundTrip);
System.out.println("================");
throw e;
}
}
Aggregations