io.smallrye.openapi.runtime.io.OpenApiSerializer Java Examples

The following examples show how to use io.smallrye.openapi.runtime.io.OpenApiSerializer. 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: MergeUtilTest.java    From smallrye-open-api with Apache License 2.0 6 votes vote down vote up
/**
 * 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);

    OpenAPI resource1Model = OpenApiParser.parse(resource1Url);
    OpenAPI resource2Model = OpenApiParser.parse(resource2Url);

    OpenAPI actualModel = MergeUtil.merge(resource1Model, resource2Model);

    String actual = OpenApiSerializer.serialize(actualModel, Format.JSON);

    assertJsonEquals(expectedContent, actual);
}
 
Example #2
Source File: FilterUtilTest.java    From smallrye-open-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test method for
 * {@link 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");

    OpenAPI model = OpenApiParser.parse(beforeUrl);
    OASFilter filter = filter();

    model = FilterUtil.applyFilter(filter, model);

    String actual = OpenApiSerializer.serialize(model, Format.JSON);
    String expected = loadResource(afterUrl);

    assertJsonEquals(expected, actual);
}
 
Example #3
Source File: OpenApiDeploymentProcessorTest.java    From thorntail with Apache License 2.0 6 votes vote down vote up
/**
 * Common test method.
 * @throws Exception
 */
protected void doTest(
        Class modelReaderClass,
        String staticResource,
        boolean disableAnnotationScanning,
        Class filterClass,
        String expectedResource) throws Exception {

    System.setProperty(OASConfig.SCAN_DISABLE, "" + disableAnnotationScanning);
    System.setProperty(OASConfig.MODEL_READER, modelReaderClass != null ? modelReaderClass.getName() : "");
    System.setProperty(OASConfig.FILTER, filterClass != null ? filterClass.getName() : "");

    TestConfig cfg = new TestConfig();
    OpenApiConfig oaiConfig = new OpenApiConfigImpl(cfg);
    Archive archive = archive(staticResource);
    OpenApiDocument.INSTANCE.reset();
    OpenApiDeploymentProcessor processor = new OpenApiDeploymentProcessor(oaiConfig, archive);
    processor.process();
    new OpenApiServletContextListener(cfg).contextInitialized(null);

    String actual = OpenApiSerializer.serialize(OpenApiDocument.INSTANCE.get(), Format.JSON);
    String expected = loadResource(getClass().getResource(expectedResource));

    assertJsonEquals(expected, actual);
}
 
Example #4
Source File: IndexScannerTestBase.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
public static String schemaToString(String entityName, Schema schema) throws IOException {
    Map<String, Schema> map = new HashMap<>();
    map.put(entityName, schema);
    OpenAPIImpl oai = new OpenAPIImpl();
    ComponentsImpl comp = new ComponentsImpl();
    comp.setSchemas(map);
    oai.setComponents(comp);
    return OpenApiSerializer.serialize(oai, Format.JSON);
}
 
Example #5
Source File: SmallRyeOpenApiProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@BuildStep
public void build(ApplicationArchivesBuildItem archivesBuildItem,
        BuildProducer<FeatureBuildItem> feature,
        BuildProducer<GeneratedResourceBuildItem> resourceBuildItemBuildProducer,
        BuildProducer<NativeImageResourceBuildItem> nativeImageResources,
        OpenApiFilteredIndexViewBuildItem openApiFilteredIndexViewBuildItem,
        Capabilities capabilities) throws Exception {
    FilteredIndexView index = openApiFilteredIndexViewBuildItem.getIndex();

    feature.produce(new FeatureBuildItem(Feature.SMALLRYE_OPENAPI));
    OpenAPI staticModel = generateStaticModel(archivesBuildItem);

    OpenAPI annotationModel;

    if (shouldScanAnnotations(capabilities)) {
        annotationModel = generateAnnotationModel(index, capabilities);
    } else {
        annotationModel = null;
    }
    OpenApiDocument finalDocument = loadDocument(staticModel, annotationModel);
    for (Format format : Format.values()) {
        String name = OpenApiHandler.BASE_NAME + format;
        resourceBuildItemBuildProducer.produce(new GeneratedResourceBuildItem(name,
                OpenApiSerializer.serialize(finalDocument.get(), format).getBytes(StandardCharsets.UTF_8)));
        nativeImageResources.produce(new NativeImageResourceBuildItem(name));
    }
}
 
Example #6
Source File: OpenApiHttpHandler.java    From thorntail with Apache License 2.0 5 votes vote down vote up
private String getModel(Format format) {
    try {
        return OpenApiSerializer.serialize(OpenApiDocument.INSTANCE.get(), format);
    } catch (IOException e) {
        throw new RuntimeException("Unable to serialize OpenAPI in " + format, e);
    }
}
 
Example #7
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);
    }
}
 
Example #8
Source File: IndexScannerTestBase.java    From smallrye-open-api with Apache License 2.0 4 votes vote down vote up
public static void printToConsole(OpenAPI oai) throws IOException {
    // Remember to set debug level logging.
    LOG.debug(OpenApiSerializer.serialize(oai, Format.JSON));
    System.out.println(OpenApiSerializer.serialize(oai, Format.JSON));
}
 
Example #9
Source File: IndexScannerTestBase.java    From smallrye-open-api with Apache License 2.0 4 votes vote down vote up
public static void assertJsonEquals(String expectedResource, OpenAPI actual) throws JSONException, IOException {
    URL resourceUrl = IndexScannerTestBase.class.getResource(expectedResource);
    JSONAssert.assertEquals(loadResource(resourceUrl), OpenApiSerializer.serialize(actual, Format.JSON),
            true);
}