Skip to content

Add support for outputting to Asciidoc format #559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -44,6 +44,7 @@ Available on [Docker Hub](https://hub.docker.com/r/openapitools/openapi-diff/) a
```bash
# docker run openapitools/openapi-diff:latest
usage: openapi-diff <old> <new>
--asciidoc <file> export diff as asciidoc in given file
--debug Print debugging information
--error Print error information
--fail-on-changed Fail if API changed but is backward
@@ -101,6 +102,7 @@ openapi-diff can read OpenAPI specs from JSON files or HTTP URLs.
```bash
$ openapi-diff --help
usage: openapi-diff <old> <new>
--asciidoc <file> export diff as asciidoc in given file
--debug Print debugging information
--error Print error information
-h,--help print this message
@@ -151,6 +153,8 @@ Add openapi-diff to your POM to show diffs when you test your Maven project. You
<jsonOutputFileName>${project.basedir}/../maven/target/diff.json</jsonOutputFileName>
<!-- Supply file path for markdown output to file if desired. -->
<markdownOutputFileName>${project.basedir}/../maven/target/diff.md</markdownOutputFileName>
<!-- Supply file path for Asciidoc output to file if desired. -->
<asciidocOutputFileName>${project.basedir}/../maven/target/diff.adoc</asciidocOutputFileName>
</configuration>
</execution>
</executions>
18 changes: 14 additions & 4 deletions cli/src/main/java/org/openapitools/openapidiff/cli/Main.java
Original file line number Diff line number Diff line change
@@ -17,10 +17,7 @@
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.openapitools.openapidiff.core.OpenApiCompare;
import org.openapitools.openapidiff.core.model.ChangedOpenApi;
import org.openapitools.openapidiff.core.output.ConsoleRender;
import org.openapitools.openapidiff.core.output.HtmlRender;
import org.openapitools.openapidiff.core.output.JsonRender;
import org.openapitools.openapidiff.core.output.MarkdownRender;
import org.openapitools.openapidiff.core.output.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@@ -109,6 +106,13 @@ public static void main(String... args) {
.argName("file")
.desc("export diff as json in given file")
.build());
options.addOption(
Option.builder()
.longOpt("asciidoc")
.hasArg()
.argName("file")
.desc("export diff as asciidoc in given file")
.build());

// create the parser
CommandLineParser parser = new DefaultParser();
@@ -191,6 +195,12 @@ public static void main(String... args) {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
mdRender.render(result, outputStreamWriter);
}
if (line.hasOption("asciidoc")) {
AsciidocRender asciidocRender = new AsciidocRender();
FileOutputStream outputStream = new FileOutputStream(line.getOptionValue("asciidoc"));
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
asciidocRender.render(result, outputStreamWriter);
}
if (line.hasOption("text")) {
FileOutputStream outputStream = new FileOutputStream(line.getOptionValue("text"));
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.openapitools.openapidiff.core.output;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AsciidocRender extends MarkdownRenderBase {
private static final Logger LOGGER = LoggerFactory.getLogger(AsciidocRender.class);
private static final String H4 = "==== ";
private static final String H5 = "===== ";
private static final String H6 = "====== ";
private static final String CODE = "`";
private static final String PRE_LI = "*";
private static final String LI = "* ";
private static final String HR = "\n'''\n";

@Override
public String getH4() {
return H4;
}

@Override
public String getH5() {
return H5;
}

@Override
public String getH6() {
return H6;
}

@Override
public String getBlockQuote() {
throw new UnsupportedOperationException(
"Asciidoc does not use blockquote marker;"
+ "instead it uses blocks like this: \n\n----\nQUOTED TEXT\n----\n");
}

@Override
public String getCode() {
return CODE;
}

@Override
public String getPreListItem() {
return PRE_LI;
}

@Override
public String getListItem() {
return LI;
}

@Override
public String getHorizontalRule() {
return HR;
}

@Override
protected String blockquote(String beginning, String text) {
return beginning + "\n----\n" + text.trim() + "\n----\n";
}
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.openapitools.openapidiff.core;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import org.junit.jupiter.api.Test;
import org.openapitools.openapidiff.core.model.ChangedOpenApi;
import org.openapitools.openapidiff.core.output.AsciidocRender;

public class AsciidocRenderTest {
@Test
public void renderDoesNotFailWhenPropertyHasBeenRemoved() {
AsciidocRender render = new AsciidocRender();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
ChangedOpenApi diff =
OpenApiCompare.fromLocations("missing_property_1.yaml", "missing_property_2.yaml");
render.render(diff, outputStreamWriter);
assertThat(outputStream.toString()).isNotBlank();
}

@Test
public void renderDoesNotCauseStackOverflowWithRecursiveDefinitions() {
AsciidocRender render = new AsciidocRender();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
ChangedOpenApi diff = OpenApiCompare.fromLocations("recursive_old.yaml", "recursive_new.yaml");
render.render(diff, outputStreamWriter);
assertThat(outputStream.toString()).isNotBlank();
}

@Test
public void renderDoesNotFailWhenHTTPStatusCodeIsRange() {
AsciidocRender render = new AsciidocRender();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
ChangedOpenApi diff =
OpenApiCompare.fromLocations("range_statuscode_1.yaml", "range_statuscode_2.yaml");
render.render(diff, outputStreamWriter);
assertThat(outputStream.toString()).isNotBlank();
}

@Test
public void rendersChangeCorrectly() {
AsciidocRender render = new AsciidocRender();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
ChangedOpenApi diff =
OpenApiCompare.fromLocations("missing_property_1.yaml", "missing_property_2.yaml");
render.render(diff, outputStreamWriter);
assertThat(outputStream.toString())
.isEqualTo(
"==== What's Changed\n"
+ "\n"
+ "'''\n"
+ "\n"
+ "===== `GET` /\n"
+ "\n"
+ "\n"
+ "====== Return Type:\n"
+ "\n"
+ "Changed response : **default **\n"
+ "\n"
+ "* Changed content type : `application/json`\n"
+ "\n"
+ "** Deleted property `childProperty` (object)\n"
+ "\n");
}
}
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.openapitools.openapidiff.core.OpenApiCompare;
import org.openapitools.openapidiff.core.model.ChangedOpenApi;
import org.openapitools.openapidiff.core.output.AsciidocRender;
import org.openapitools.openapidiff.core.output.ConsoleRender;
import org.openapitools.openapidiff.core.output.JsonRender;
import org.openapitools.openapidiff.core.output.MarkdownRender;
@@ -46,6 +47,9 @@ public class OpenApiDiffMojo extends AbstractMojo {
@Parameter(property = "markdownOutputFileName")
String markdownOutputFileName;

@Parameter(property = "asciidocOutputFileName")
String asciidocOutputFileName;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (Boolean.TRUE.equals(skip)) {
@@ -67,6 +71,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
writeDiffAsTextToFile(diff);
writeDiffAsJsonToFile(diff);
writeDiffAsMarkdownToFile(diff);
writeDiffAsAsciidocToFile(diff);

if (failOnIncompatible && diff.isIncompatible()) {
throw new BackwardIncompatibilityException("The API changes broke backward compatibility");
@@ -91,4 +96,8 @@ private void writeDiffAsJsonToFile(final ChangedOpenApi diff) {
private void writeDiffAsMarkdownToFile(final ChangedOpenApi diff) {
writeToFile(new MarkdownRender(), diff, markdownOutputFileName);
}

private void writeDiffAsAsciidocToFile(final ChangedOpenApi diff) {
writeToFile(new AsciidocRender(), diff, asciidocOutputFileName);
}
}
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@ class OpenApiDiffMojoTest {
private final File consoleOutputfile = new File("target/diff.txt");
private final File markdownOutputfile = new File("target/diff.md");
private final File jsonOutputfile = new File("target/diff.json");
private final File asciidocOutputfile = new File("target/diff.adoc");

@BeforeEach
void setup() {
@@ -158,6 +159,20 @@ void Should_outputToJsonFile_When_SpecIsDifferent() {
assertTrue(Files.exists(jsonOutputfile.toPath()));
}

@Test
void Should_outputToAsciidocFile_When_SpecIsDifferent() {
final OpenApiDiffMojo mojo = new OpenApiDiffMojo();
mojo.oldSpec = oldSpecFile.getAbsolutePath();
mojo.newSpec = newSpecFile.getAbsolutePath();

mojo.asciidocOutputFileName = asciidocOutputfile.getAbsolutePath();
mojo.failOnChanged = true;

assertThrows(ApiChangedException.class, mojo::execute);

assertTrue(Files.exists(asciidocOutputfile.toPath()));
}

private void cleanupGeneratedFiles() {
try {
Files.deleteIfExists(Paths.get(consoleOutputfile.getPath()));