io.smallrye.openapi.runtime.OpenApiStaticFile Java Examples

The following examples show how to use io.smallrye.openapi.runtime.OpenApiStaticFile. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OpenApiDocumentProducer.java    From quarkus with Apache License 2.0 6 votes vote down vote up
/**
 * We load the document from the generated JSON file, which should have all the annotations
 *
 * Most apps will likely just want to serve the OpenAPI doc, rather than inject it, which is why we generated the
 * static file and parse it if required. The more Quarkus-like approach of serializing the doc to bytecode
 * can result in a lot of bytecode, which will likely just be turned straight into a static file anyway.
 */
@PostConstruct
void create() throws IOException {
    try (InputStream is = getClass().getClassLoader()
            .getResourceAsStream(OpenApiHandler.BASE_NAME + Format.JSON)) {
        if (is != null) {
            try (OpenApiStaticFile staticFile = new OpenApiStaticFile(is, Format.JSON)) {
                Config config = ConfigProvider.getConfig();
                OpenApiConfig openApiConfig = new OpenApiConfigImpl(config);

                OpenAPI readerModel = OpenApiProcessor.modelFromReader(openApiConfig,
                        Thread.currentThread().getContextClassLoader());
                document = OpenApiDocument.INSTANCE;
                document.reset();
                document.config(openApiConfig);

                document.modelFromReader(readerModel);
                document.modelFromStaticFile(io.smallrye.openapi.runtime.OpenApiProcessor.modelFromStaticFile(staticFile));
                document.filter(OpenApiProcessor.getFilter(openApiConfig, Thread.currentThread().getContextClassLoader()));
                document.initialize();
            }
        }
    }

}
 
Example #2
Source File: OpenApiDeploymentProcessor.java    From thorntail with Apache License 2.0 6 votes vote down vote up
/**
 * Process the deployment in order to produce an OpenAPI document.
 *
 * @see org.wildfly.swarm.spi.api.DeploymentProcessor#process()
 */
@Override
public void process() throws Exception {
    // if the deployment is Implicit, we don't want to process it
    if (deploymentContext != null && deploymentContext.isImplicit()) {
        return;
    }
    try {
        // First register OpenApiServletContextListener which triggers the final init
        WARArchive warArchive = archive.as(WARArchive.class);
        warArchive.findWebXmlAsset().addListener(LISTENER_CLASS);
    } catch (Exception e) {
        throw new RuntimeException("Failed to register OpenAPI listener", e);
    }

    OpenApiStaticFile staticFile = ArchiveUtil.archiveToStaticFile(archive);

    // Set models from annotations and static file
    OpenApiDocument openApiDocument = OpenApiDocument.INSTANCE;
    openApiDocument.config(config);
    openApiDocument.modelFromStaticFile(OpenApiProcessor.modelFromStaticFile(staticFile));
    openApiDocument.modelFromAnnotations(OpenApiProcessor.modelFromAnnotations(config, index));
}
 
Example #3
Source File: ArchiveUtil.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
/**
 * Finds the static OpenAPI file located in the deployment and, if it exists, returns
 * it as an {@link OpenApiStaticFile}. If not found, returns null. The static file
 * (when not null) contains an {@link InputStream} to the contents of the static file.
 * The caller is responsible for closing this stream.
 * 
 * @param archive Shrinkwrap Archive instance
 * @return OpenApiStaticFile
 */
public static OpenApiStaticFile archiveToStaticFile(Archive<?> archive) {
    // Instruct SonarCloud to ignore this as an unclosed resource (since this method produces it)
    @SuppressWarnings("squid:S2095")
    OpenApiStaticFile rval = new OpenApiStaticFile();
    rval.setFormat(Format.YAML);

    // Check for the file in both META-INF and WEB-INF/classes/META-INF
    Node node = archive.get("/META-INF/openapi.yaml");
    if (node == null) {
        node = archive.get("/WEB-INF/classes/META-INF/openapi.yaml");
    }
    if (node == null) {
        node = archive.get("/META-INF/openapi.yml");
    }
    if (node == null) {
        node = archive.get("/WEB-INF/classes/META-INF/openapi.yml");
    }
    if (node == null) {
        node = archive.get("/META-INF/openapi.json");
        rval.setFormat(Format.JSON);
    }
    if (node == null) {
        node = archive.get("/WEB-INF/classes/META-INF/openapi.json");
        rval.setFormat(Format.JSON);
    }

    if (node == null) {
        return null;
    }

    rval.setContent(node.getAsset().openStream());

    return rval;
}
 
Example #4
Source File: SmallRyeOpenApiProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private OpenAPI generateStaticModel(ApplicationArchivesBuildItem archivesBuildItem) throws IOException {
    Result result = findStaticModel(archivesBuildItem);
    if (result != null) {
        try (InputStream is = Files.newInputStream(result.path);
                OpenApiStaticFile staticFile = new OpenApiStaticFile(is, result.format)) {
            return io.smallrye.openapi.runtime.OpenApiProcessor.modelFromStaticFile(staticFile);
        }
    }
    return null;
}
 
Example #5
Source File: TckTestRunner.java    From smallrye-open-api with Apache License 2.0 4 votes vote down vote up
/**
 * Constructor.
 * 
 * @param testClass
 * @throws InitializationError
 */
public TckTestRunner(Class<?> testClass) throws InitializationError {
    super(testClass);
    this.testClass = testClass;
    this.tckTestClass = determineTckTestClass(testClass);

    // The Archive (shrinkwrap deployment)
    Archive archive = archive();
    // MPConfig
    OpenApiConfig config = ArchiveUtil.archiveToConfig(archive);

    try {
        IndexView index = ArchiveUtil.archiveToIndex(config, archive);
        OpenApiStaticFile staticFile = ArchiveUtil.archiveToStaticFile(archive);

        // Reset and then initialize the OpenApiDocument for this test.
        OpenApiDocument.INSTANCE.reset();
        OpenApiDocument.INSTANCE.config(config);
        OpenApiDocument.INSTANCE.modelFromStaticFile(OpenApiProcessor.modelFromStaticFile(staticFile));
        OpenApiDocument.INSTANCE.modelFromAnnotations(OpenApiProcessor.modelFromAnnotations(config, index));
        OpenApiDocument.INSTANCE.modelFromReader(OpenApiProcessor.modelFromReader(config, getContextClassLoader()));
        OpenApiDocument.INSTANCE.filter(OpenApiProcessor.getFilter(config, getContextClassLoader()));
        OpenApiDocument.INSTANCE.initialize();

        Assert.assertNotNull("Generated OAI document must not be null.", OpenApiDocument.INSTANCE.get());

        OPEN_API_DOCS.put(testClass, OpenApiDocument.INSTANCE.get());

        // Output the /openapi content to a file for debugging purposes
        File parent = new File("target", "TckTestRunner");
        if (!parent.exists()) {
            parent.mkdir();
        }
        File file = new File(parent, testClass.getName() + ".json");
        String content = OpenApiSerializer.serialize(OpenApiDocument.INSTANCE.get(), Format.JSON);
        try (FileWriter writer = new FileWriter(file)) {
            IOUtils.write(content, writer);
        }
    } catch (Exception e) {
        throw new InitializationError(e);
    }
}