Search in sources :

Example 1 with OpenAPIImpl

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;
}
Also used : Components(org.eclipse.microprofile.openapi.models.Components) Server(org.eclipse.microprofile.openapi.models.servers.Server) OpenAPIImpl(org.wildfly.swarm.microprofile.openapi.api.models.OpenAPIImpl) SecurityScheme(org.eclipse.microprofile.openapi.models.security.SecurityScheme) AnnotationInstance(org.jboss.jandex.AnnotationInstance)

Example 2 with OpenAPIImpl

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);
}
Also used : OASFilter(org.eclipse.microprofile.openapi.OASFilter) OpenAPIImpl(org.wildfly.swarm.microprofile.openapi.api.models.OpenAPIImpl) URL(java.net.URL) Test(org.junit.Test)

Example 3 with OpenAPIImpl

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);
}
Also used : OpenAPIImpl(org.wildfly.swarm.microprofile.openapi.api.models.OpenAPIImpl) URL(java.net.URL)

Example 4 with OpenAPIImpl

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);
    }
}
Also used : OASModelReader(org.eclipse.microprofile.openapi.OASModelReader) OpenAPIImpl(org.wildfly.swarm.microprofile.openapi.api.models.OpenAPIImpl)

Example 5 with OpenAPIImpl

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;
    }
}
Also used : OpenAPIImpl(org.wildfly.swarm.microprofile.openapi.api.models.OpenAPIImpl) URL(java.net.URL)

Aggregations

OpenAPIImpl (org.wildfly.swarm.microprofile.openapi.api.models.OpenAPIImpl)9 URL (java.net.URL)3 PathsImpl (org.wildfly.swarm.microprofile.openapi.api.models.PathsImpl)2 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1 OASFilter (org.eclipse.microprofile.openapi.OASFilter)1 OASModelReader (org.eclipse.microprofile.openapi.OASModelReader)1 Components (org.eclipse.microprofile.openapi.models.Components)1 OpenAPI (org.eclipse.microprofile.openapi.models.OpenAPI)1 PathItem (org.eclipse.microprofile.openapi.models.PathItem)1 Paths (org.eclipse.microprofile.openapi.models.Paths)1 Schema (org.eclipse.microprofile.openapi.models.media.Schema)1 SecurityScheme (org.eclipse.microprofile.openapi.models.security.SecurityScheme)1 Server (org.eclipse.microprofile.openapi.models.servers.Server)1 AnnotationInstance (org.jboss.jandex.AnnotationInstance)1 ClassInfo (org.jboss.jandex.ClassInfo)1 Test (org.junit.Test)1 ComponentsImpl (org.wildfly.swarm.microprofile.openapi.api.models.ComponentsImpl)1 InfoImpl (org.wildfly.swarm.microprofile.openapi.api.models.info.InfoImpl)1