Skip to content

Add JsonRender and --json command line option #255

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

Merged
merged 1 commit into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
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
85 changes: 84 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Compare two OpenAPI specifications (3.x) and render the difference to HTML plain
* Depth comparison of parameters, responses, endpoint, http method (GET,POST,PUT,DELETE...)
* Supports swagger api Authorization
* Render difference of property with Expression Language
* HTML & Markdown render
* HTML, Markdown & JSON render

# Maven

Expand Down Expand Up @@ -52,6 +52,7 @@ usage: openapi-diff <old> <new>
--header <property=value> use given header for authorisation
--html <file> export diff as html in given file
--info Print additional information
--json <file> export diff as json in given file
-l,--log <level> use given level for log (TRACE, DEBUG,
INFO, WARN, ERROR, OFF). Default: ERROR
--markdown <file> export diff as markdown in given file
Expand Down Expand Up @@ -104,6 +105,7 @@ usage: openapi-diff <old> <new>
--header <property=value> use given header for authorisation
--html <file> export diff as html in given file
--info Print additional information
--json <file> export diff as json in given file
-l,--log <level> use given level for log (TRACE, DEBUG,
INFO, WARN, ERROR, OFF). Default: ERROR
--markdown <file> export diff as markdown in given file
Expand Down Expand Up @@ -168,6 +170,22 @@ try {
}
```


#### JSON

```java
String render = new JsonRender().render(diff);
try {
FileWriter fw = new FileWriter(
"testDiff.json");
fw.write(render);
fw.close();

} catch (IOException e) {
e.printStackTrace();
}
```

### Extensions

This project uses Java Service Provider Inteface (SPI) so additional extensions can be added.
Expand Down Expand Up @@ -350,6 +368,71 @@ Then, including your library with the `openapi-diff` module will cause it to be
Changed response : [200] //successful operation
```

### JSON

```json
{
"changedElements": [...],
"changedExtensions": null,
"changedOperations": [...],
"compatible": false,
"deprecatedEndpoints": [...],
"different": true,
"incompatible": true,
"missingEndpoints": [...],
"newEndpoints": [
{
"method": "GET",
"operation": {
"callbacks": null,
"deprecated": null,
"description": "Returns a single pet",
"extensions": null,
"externalDocs": null,
"operationId": "getPetById",
"parameters": [
{
"$ref": null,
"allowEmptyValue": null,
"allowReserved": null,
"content": null,
"deprecated": null,
"description": "ID of pet to return",
"example": null,
"examples": null,
"explode": false,
"extensions": null,
"in": "path",
"name": "petId",
"required": true,
"schema": {...},
"style": "SIMPLE"
}
],
"requestBody": null,
"responses": {...},
"security": [
{
"api_key": []
}
],
"servers": null,
"summary": "Find pet by ID",
"tags": [
"pet"
]
},
"path": null,
"pathUrl": "/pet/{petId}",
"summary": "Find pet by ID"
}
],
"newSpecOpenApi": {...},
"oldSpecOpenApi": {...},
"unchanged": false
}
```

# License

openapi-diff is released under the Apache License 2.0.
Expand Down
14 changes: 14 additions & 0 deletions cli/src/main/java/org/openapitools/openapidiff/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -106,6 +107,13 @@ public static void main(String... args) {
.argName("file")
.desc("export diff as text in given file")
.build());
options.addOption(
Option.builder()
.longOpt("json")
.hasArg()
.argName("file")
.desc("export diff as json in given file")
.build());

// create the parser
CommandLineParser parser = new DefaultParser();
Expand Down Expand Up @@ -188,6 +196,12 @@ public static void main(String... args) {
String outputFile = line.getOptionValue("text");
writeOutput(output, outputFile);
}
if (line.hasOption("json")) {
JsonRender jsonRender = new JsonRender();
String output = jsonRender.render(result);
String outputFile = line.getOptionValue("json");
writeOutput(output, outputFile);
}
if (line.hasOption("state")) {
System.out.println(result.isChanged().getValue());
System.exit(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.openapitools.openapidiff.core.output;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.openapitools.openapidiff.core.model.ChangedOpenApi;

public class JsonRender implements Render {
private final ObjectMapper objectMapper = new ObjectMapper();

@Override
public String render(ChangedOpenApi diff) {
try {
return objectMapper.writeValueAsString(diff);
} catch (JsonProcessingException e) {
throw new RuntimeException("Could not serialize diff as JSON", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.openapitools.openapidiff.core;

import org.junit.jupiter.api.Test;
import org.openapitools.openapidiff.core.model.ChangedOpenApi;
import org.openapitools.openapidiff.core.output.JsonRender;

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

public class JsonRenderTest {
@Test
public void renderDoesNotFailWhenPropertyHasBeenRemoved() {
JsonRender render = new JsonRender();
ChangedOpenApi diff =
OpenApiCompare.fromLocations("missing_property_1.yaml", "missing_property_2.yaml");
assertThat(render.render(diff)).isNotBlank();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.openapitools.openapidiff.core.model.ChangedOperation;
import org.openapitools.openapidiff.core.model.Endpoint;
import org.openapitools.openapidiff.core.output.HtmlRender;
import org.openapitools.openapidiff.core.output.JsonRender;
import org.openapitools.openapidiff.core.output.MarkdownRender;

public class OpenApiDiffTest {
Expand Down Expand Up @@ -102,4 +103,18 @@ public void testDiffAndMarkdown() {
e.printStackTrace();
}
}

@Test
public void testDiffAndJson() {
ChangedOpenApi diff = OpenApiCompare.fromLocations(OPENAPI_DOC1, OPENAPI_DOC2);
String render = new JsonRender().render(diff);
try {
FileWriter fw = new FileWriter("target/testDiff.json");
fw.write(render);
fw.close();

} catch (IOException e) {
e.printStackTrace();
}
}
}