Skip to content

Commit bda81cc

Browse files
authored
Add JsonRender and --json CLI option (#255)
1 parent 38e096a commit bda81cc

File tree

5 files changed

+148
-1
lines changed

5 files changed

+148
-1
lines changed

README.md

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Compare two OpenAPI specifications (3.x) and render the difference to HTML plain
2121
* Depth comparison of parameters, responses, endpoint, http method (GET,POST,PUT,DELETE...)
2222
* Supports swagger api Authorization
2323
* Render difference of property with Expression Language
24-
* HTML & Markdown render
24+
* HTML, Markdown & JSON render
2525

2626
# Maven
2727

@@ -52,6 +52,7 @@ usage: openapi-diff <old> <new>
5252
--header <property=value> use given header for authorisation
5353
--html <file> export diff as html in given file
5454
--info Print additional information
55+
--json <file> export diff as json in given file
5556
-l,--log <level> use given level for log (TRACE, DEBUG,
5657
INFO, WARN, ERROR, OFF). Default: ERROR
5758
--markdown <file> export diff as markdown in given file
@@ -104,6 +105,7 @@ usage: openapi-diff <old> <new>
104105
--header <property=value> use given header for authorisation
105106
--html <file> export diff as html in given file
106107
--info Print additional information
108+
--json <file> export diff as json in given file
107109
-l,--log <level> use given level for log (TRACE, DEBUG,
108110
INFO, WARN, ERROR, OFF). Default: ERROR
109111
--markdown <file> export diff as markdown in given file
@@ -168,6 +170,22 @@ try {
168170
}
169171
```
170172
173+
174+
#### JSON
175+
176+
```java
177+
String render = new JsonRender().render(diff);
178+
try {
179+
FileWriter fw = new FileWriter(
180+
"testDiff.json");
181+
fw.write(render);
182+
fw.close();
183+
184+
} catch (IOException e) {
185+
e.printStackTrace();
186+
}
187+
```
188+
171189
### Extensions
172190
173191
This project uses Java Service Provider Inteface (SPI) so additional extensions can be added.
@@ -350,6 +368,71 @@ Then, including your library with the `openapi-diff` module will cause it to be
350368
Changed response : [200] //successful operation
351369
```
352370
371+
### JSON
372+
373+
```json
374+
{
375+
"changedElements": [...],
376+
"changedExtensions": null,
377+
"changedOperations": [...],
378+
"compatible": false,
379+
"deprecatedEndpoints": [...],
380+
"different": true,
381+
"incompatible": true,
382+
"missingEndpoints": [...],
383+
"newEndpoints": [
384+
{
385+
"method": "GET",
386+
"operation": {
387+
"callbacks": null,
388+
"deprecated": null,
389+
"description": "Returns a single pet",
390+
"extensions": null,
391+
"externalDocs": null,
392+
"operationId": "getPetById",
393+
"parameters": [
394+
{
395+
"$ref": null,
396+
"allowEmptyValue": null,
397+
"allowReserved": null,
398+
"content": null,
399+
"deprecated": null,
400+
"description": "ID of pet to return",
401+
"example": null,
402+
"examples": null,
403+
"explode": false,
404+
"extensions": null,
405+
"in": "path",
406+
"name": "petId",
407+
"required": true,
408+
"schema": {...},
409+
"style": "SIMPLE"
410+
}
411+
],
412+
"requestBody": null,
413+
"responses": {...},
414+
"security": [
415+
{
416+
"api_key": []
417+
}
418+
],
419+
"servers": null,
420+
"summary": "Find pet by ID",
421+
"tags": [
422+
"pet"
423+
]
424+
},
425+
"path": null,
426+
"pathUrl": "/pet/{petId}",
427+
"summary": "Find pet by ID"
428+
}
429+
],
430+
"newSpecOpenApi": {...},
431+
"oldSpecOpenApi": {...},
432+
"unchanged": false
433+
}
434+
```
435+
353436
# License
354437
355438
openapi-diff is released under the Apache License 2.0.

cli/src/main/java/org/openapitools/openapidiff/cli/Main.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.openapitools.openapidiff.core.model.ChangedOpenApi;
2525
import org.openapitools.openapidiff.core.output.ConsoleRender;
2626
import org.openapitools.openapidiff.core.output.HtmlRender;
27+
import org.openapitools.openapidiff.core.output.JsonRender;
2728
import org.openapitools.openapidiff.core.output.MarkdownRender;
2829
import org.slf4j.Logger;
2930
import org.slf4j.LoggerFactory;
@@ -106,6 +107,13 @@ public static void main(String... args) {
106107
.argName("file")
107108
.desc("export diff as text in given file")
108109
.build());
110+
options.addOption(
111+
Option.builder()
112+
.longOpt("json")
113+
.hasArg()
114+
.argName("file")
115+
.desc("export diff as json in given file")
116+
.build());
109117

110118
// create the parser
111119
CommandLineParser parser = new DefaultParser();
@@ -188,6 +196,12 @@ public static void main(String... args) {
188196
String outputFile = line.getOptionValue("text");
189197
writeOutput(output, outputFile);
190198
}
199+
if (line.hasOption("json")) {
200+
JsonRender jsonRender = new JsonRender();
201+
String output = jsonRender.render(result);
202+
String outputFile = line.getOptionValue("json");
203+
writeOutput(output, outputFile);
204+
}
191205
if (line.hasOption("state")) {
192206
System.out.println(result.isChanged().getValue());
193207
System.exit(0);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.openapitools.openapidiff.core.output;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import org.openapitools.openapidiff.core.model.ChangedOpenApi;
6+
7+
public class JsonRender implements Render {
8+
private final ObjectMapper objectMapper = new ObjectMapper();
9+
10+
@Override
11+
public String render(ChangedOpenApi diff) {
12+
try {
13+
return objectMapper.writeValueAsString(diff);
14+
} catch (JsonProcessingException e) {
15+
throw new RuntimeException("Could not serialize diff as JSON", e);
16+
}
17+
}
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.openapitools.openapidiff.core;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.openapitools.openapidiff.core.model.ChangedOpenApi;
5+
import org.openapitools.openapidiff.core.output.JsonRender;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
public class JsonRenderTest {
10+
@Test
11+
public void renderDoesNotFailWhenPropertyHasBeenRemoved() {
12+
JsonRender render = new JsonRender();
13+
ChangedOpenApi diff =
14+
OpenApiCompare.fromLocations("missing_property_1.yaml", "missing_property_2.yaml");
15+
assertThat(render.render(diff)).isNotBlank();
16+
}
17+
}

core/src/test/java/org/openapitools/openapidiff/core/OpenApiDiffTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.openapitools.openapidiff.core.model.ChangedOperation;
1212
import org.openapitools.openapidiff.core.model.Endpoint;
1313
import org.openapitools.openapidiff.core.output.HtmlRender;
14+
import org.openapitools.openapidiff.core.output.JsonRender;
1415
import org.openapitools.openapidiff.core.output.MarkdownRender;
1516

1617
public class OpenApiDiffTest {
@@ -102,4 +103,18 @@ public void testDiffAndMarkdown() {
102103
e.printStackTrace();
103104
}
104105
}
106+
107+
@Test
108+
public void testDiffAndJson() {
109+
ChangedOpenApi diff = OpenApiCompare.fromLocations(OPENAPI_DOC1, OPENAPI_DOC2);
110+
String render = new JsonRender().render(diff);
111+
try {
112+
FileWriter fw = new FileWriter("target/testDiff.json");
113+
fw.write(render);
114+
fw.close();
115+
116+
} catch (IOException e) {
117+
e.printStackTrace();
118+
}
119+
}
105120
}

0 commit comments

Comments
 (0)