@Test public void testWithEnums() throws IOException, URISyntaxException { //Given Path file = Paths.get(AsciidocConverterTest.class.getResource("/json/swagger_enums.json").toURI()); Path outputDirectory = Paths.get("build/test/asciidoc/enums"); FileUtils.deleteQuietly(outputDirectory.toFile()); Map<String, String> configMap = new HashMap<>(); configMap.put("swagger2markup.generatedExamplesEnabled", "true"); // enable examples generation //When Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder(configMap) .withMarkupLanguage(MarkupLanguage.ASCIIDOC) .build(); Swagger2MarkupConverter.from(file) .withConfig(config) .build() .toFolder(outputDirectory); //Then String[] files = outputDirectory.toFile().list(); assertThat(files).hasSize(4).containsAll(expectedFiles); Path expectedFilesDirectory = Paths.get(AsciidocConverterTest.class.getResource("/expected/asciidoc/enums").toURI()); DiffUtils.assertThatAllFilesAreEqual(expectedFilesDirectory, outputDirectory, "testWithEnums.html"); }
@Test public void testWithMinMaxItems() throws IOException, URISyntaxException { //Given Path file = Paths.get(MarkdownConverterTest.class.getResource("/yaml/swagger_petstore_min_max_items.yaml").toURI()); Path outputDirectory = Paths.get("build/test/markdown/to_folder"); FileUtils.deleteQuietly(outputDirectory.toFile()); //When Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withMarkupLanguage(MarkupLanguage.MARKDOWN) .build(); Swagger2MarkupConverter.from(file) .withConfig(config) .build() .toFolder(outputDirectory); //Then String[] files = outputDirectory.toFile().list(); assertThat(files).hasSize(4).containsAll(expectedFiles); Path expectedFilesDirectory = Paths.get(AsciidocConverterTest.class.getResource("/expected/markdown/min_max_items").toURI()); DiffUtils.assertThatAllFilesAreEqual(expectedFilesDirectory, outputDirectory, "min_max_items.html"); }
@Test public void testToFolder() throws IOException, URISyntaxException { //Given Path file = Paths.get(MarkdownConverterTest.class.getResource("/yaml/swagger_petstore.yaml").toURI()); Path outputDirectory = Paths.get("build/test/markdown/to_folder"); FileUtils.deleteQuietly(outputDirectory.toFile()); //When Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withMarkupLanguage(MarkupLanguage.MARKDOWN) .build(); Swagger2MarkupConverter.from(file) .withConfig(config) .build() .toFolder(outputDirectory); //Then String[] files = outputDirectory.toFile().list(); assertThat(files).hasSize(4).containsAll(expectedFiles); Path expectedFilesDirectory = Paths.get(AsciidocConverterTest.class.getResource("/expected/markdown/to_folder").toURI()); DiffUtils.assertThatAllFilesAreEqual(expectedFilesDirectory, outputDirectory, "testToFolder.html"); }
@Test public void testWithResponseHeaders() throws IOException, URISyntaxException { //Given Path file = Paths.get(AsciidocConverterTest.class.getResource("/yaml/swagger_response_headers.yaml").toURI()); Path outputDirectory = Paths.get("build/test/markdown/response_headers"); FileUtils.deleteQuietly(outputDirectory.toFile()); //When Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withMarkupLanguage(MarkupLanguage.MARKDOWN) .build(); Swagger2MarkupConverter.from(file) .withConfig(config) .build() .toFolder(outputDirectory); //Then String[] files = outputDirectory.toFile().list(); assertThat(files).hasSize(4).containsAll(expectedFiles); Path expectedFilesDirectory = Paths.get(AsciidocConverterTest.class.getResource("/expected/markdown/response_headers").toURI()); DiffUtils.assertThatAllFilesAreEqual(expectedFilesDirectory, outputDirectory, "testWithResponseHeaders.html"); }
@Test public void testWithInterDocumentCrossReferences() throws IOException, URISyntaxException { //Given Path file = Paths.get(AsciidocConverterTest.class.getResource("/yaml/swagger_petstore.yaml").toURI()); Path outputDirectory = Paths.get("build/test/markdown/idxref"); FileUtils.deleteQuietly(outputDirectory.toFile()); //When Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withMarkupLanguage(MarkupLanguage.MARKDOWN) .withInterDocumentCrossReferences() .build(); Swagger2MarkupConverter.from(file).withConfig(config).build() .toFolder(outputDirectory); //Then String[] files = outputDirectory.toFile().list(); assertThat(files).hasSize(4).containsAll(expectedFiles); Path expectedFilesDirectory = Paths.get(AsciidocConverterTest.class.getResource("/expected/markdown/idxref").toURI()); DiffUtils.assertThatAllFilesAreEqual(expectedFilesDirectory, outputDirectory, "testWithInterDocumentCrossReferences.html"); }
@Test public void testHandlesComposition() throws IOException, URISyntaxException { //Given Path file = Paths.get(MarkdownConverterTest.class.getResource("/yaml/swagger_petstore.yaml").toURI()); Path outputDirectory = Paths.get("build/test/markdown/generated"); FileUtils.deleteQuietly(outputDirectory.toFile()); //When Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withSeparatedDefinitions() .withMarkupLanguage(MarkupLanguage.MARKDOWN) .build(); Swagger2MarkupConverter.from(file) .withConfig(config) .build() .toFolder(outputDirectory); // Then String[] files = outputDirectory.toFile().list(); expectedFiles.add("definitions"); assertThat(files).hasSize(5).containsAll(expectedFiles); Path definitionsDirectory = outputDirectory.resolve("definitions"); verifyMarkdownContainsFieldsInTables( definitionsDirectory.resolve("User.md").toFile(), ImmutableMap.<String, Set<String>>builder() .put("User", ImmutableSet.of("id", "username", "firstName", "lastName", "email", "password", "phone", "userStatus")) .build() ); }
@Test public void testWithInterDocumentCrossReferencesAndMarkdown() { Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withInterDocumentCrossReferences() .withMarkupLanguage(MarkupLanguage.MARKDOWN) .build(); Swagger2MarkupConverter.Context context = createContext(config); context.setOutputPath(Paths.get("/tmp")); assertThat(new OperationDocumentResolverDefault(context).apply(operation)) .isEqualTo("paths.md"); } }
@Test public void testWithInterDocumentCrossReferencesAndMarkdown() { Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withInterDocumentCrossReferences() .withMarkupLanguage(MarkupLanguage.MARKDOWN) .build(); Swagger2MarkupConverter.Context context = createContext(config); context.setOutputPath(Paths.get("/tmp")); assertThat(new DefinitionDocumentResolverDefault(context).apply("DefinitionName")) .isEqualTo("definitions.md"); } }
@Test public void testFreeFormWithEmptyCurlyBracket() throws IOException, URISyntaxException { //Given Path file = Paths.get(MarkdownConverterTest.class.getResource("/yaml/swagger_freeform_with_emtpy_curly_brackets.yaml").toURI()); Path outputDirectory = Paths.get("build/test/markdown/freeform"); FileUtils.deleteQuietly(outputDirectory.toFile()); //When Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withSeparatedDefinitions() .withMarkupLanguage(MarkupLanguage.MARKDOWN) .build(); Swagger2MarkupConverter.from(file) .withConfig(config) .build() .toFolder(outputDirectory); // Then String[] files = outputDirectory.toFile().list(); expectedFiles.add("definitions"); assertThat(files).hasSize(5).containsAll(expectedFiles); Path expectedFilesDirectory = Paths.get(MarkdownConverterTest.class.getResource("/expected/markdown/freeform").toURI()); DiffUtils.assertThatAllFilesAreEqual(expectedFilesDirectory, outputDirectory, "freeform.html"); } }
@Test public void testWithInterDocumentCrossReferencesAndMarkdown() { Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withInterDocumentCrossReferences() .withMarkupLanguage(MarkupLanguage.MARKDOWN) .build(); Swagger2MarkupConverter.Context context = createContext(config); context.setOutputPath(Paths.get("/tmp")); assertThat(new DefinitionDocumentNameResolver(context).apply("DefinitionName")) .isEqualTo("definitions.md"); } }
@Test public void testWithInterDocumentCrossReferencesAndMarkdown() { Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withInterDocumentCrossReferences() .withMarkupLanguage(MarkupLanguage.MARKDOWN) .build(); Swagger2MarkupConverter.Context context = createContext(config); context.setOutputPath(Paths.get("/tmp")); assertThat(new DefinitionDocumentResolverFromOperation(context).apply("DefinitionName")) .isEqualTo("definitions.md"); } }
@Test public void testWithInterDocumentCrossReferencesAndMarkdown() { Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withInterDocumentCrossReferences() .withMarkupLanguage(MarkupLanguage.MARKDOWN) .build(); Swagger2MarkupConverter.Context context = createContext(config); context.setOutputPath(Paths.get("/tmp")); assertThat(new DefinitionDocumentResolverFromDefinition(context).apply("DefinitionName")) .isEqualTo("definitions.md"); } }
@Test public void testWithInterDocumentCrossReferencesAndMarkdown() { Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withInterDocumentCrossReferences() .withMarkupLanguage(MarkupLanguage.MARKDOWN) .build(); Swagger2MarkupConverter.Context context = createContext(config); context.setOutputPath(Paths.get("/tmp")); assertThat(new OperationDocumentNameResolver(context).apply(operation)) .isEqualTo("paths.md"); } }
@Test public void testWithInterDocumentCrossReferencesAndMarkdown() { Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withInterDocumentCrossReferences() .withMarkupLanguage(MarkupLanguage.MARKDOWN) .build(); Swagger2MarkupConverter.Context context = createContext(config); context.setOutputPath(Paths.get("/tmp")); assertThat(new SecurityDocumentResolver(context).apply("petstore_auth")) .isEqualTo("security.md"); } }
@Test public void testWithSeparatedDefinitions() throws IOException, URISyntaxException { //Given Path file = Paths.get(MarkdownConverterTest.class.getResource("/yaml/swagger_petstore.yaml").toURI()); Path outputDirectory = Paths.get("build/test/markdown/generated"); FileUtils.deleteQuietly(outputDirectory.toFile()); //When Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withSeparatedDefinitions() .withMarkupLanguage(MarkupLanguage.MARKDOWN) .build(); Swagger2MarkupConverter.from(file) .withConfig(config) .build() .toFolder(outputDirectory); //Then String[] files = outputDirectory.toFile().list(); expectedFiles.add("definitions"); assertThat(files).hasSize(5).containsAll(expectedFiles); Path definitionsDirectory = outputDirectory.resolve("definitions"); String[] definitions = definitionsDirectory.toFile().list(); assertThat(definitions).hasSize(5).containsAll( asList("Category.md", "Order.md", "Pet.md", "Tag.md", "User.md")); }
public void swagger2MarkupConfigBuilder() { Path localSwaggerFile = Paths.get("/path/to/swagger.yaml"); // tag::swagger2MarkupConfigBuilder[] Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() //<1> .withMarkupLanguage(MarkupLanguage.MARKDOWN) //<2> .withOutputLanguage(Language.DE) //<3> .withPathsGroupedBy(GroupBy.TAGS) //<4> .build(); //<5> Swagger2MarkupConverter converter = Swagger2MarkupConverter.from(localSwaggerFile) .withConfig(config) //<6> .build(); // end::swagger2MarkupConfigBuilder[] }
@Override public String convertSwaggerToAsciiDoc(final String swaggerSchemaPath) { final File swaggerSchemaFile; LOG.info("Converting Swagger Schema to Ascii Doc..."); try { swaggerSchemaFile = getSchemaFile(swaggerSchemaPath); } catch (final FileNotFoundException | URISyntaxException e) { throw new SwaggerConfluenceConfigurationException("Error Locating Swagger Schema", e); } final String swaggerAsciiDoc; try { final Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder() .withMarkupLanguage(MarkupLanguage.ASCIIDOC) .withOutputLanguage(Language.EN) .withPathsGroupedBy(GroupBy.AS_IS) .withOperationOrdering(OrderBy.AS_IS) .build(); final String swaggerSchema = FileUtils.readFileToString(swaggerSchemaFile, StandardCharsets.UTF_8); swaggerAsciiDoc = Swagger2MarkupConverter.from(swaggerSchema) .withConfig(config) .build() .toString(); } catch (IOException e) { throw new SwaggerConfluenceInternalSystemException( "Error Converting Swagger Schema to AsciiDoc", e); } LOG.info("AsciiDoc Conversion Complete!"); return swaggerAsciiDoc; }