Skip to content

Commit 31c4f09

Browse files
authored
feat: Support writing directly into file in JsonRenderer (#388)
Previously, openapi-diff rendered the JSON output into a string and wrote the string representation into the designated output file. This failed with an `OutOfMemoryError` for very large outputs. This change set introduces a method in `JsonRenderer` to write the JSON output directly into a file and removes the verbatim copy of the old and new OpenAPI specs (`oldSpecOpenApi` and `newSpecOpenApi`) from the JSON output.
1 parent 07949f1 commit 31c4f09

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,8 @@ public static void main(String... args) {
196196
}
197197
if (line.hasOption("json")) {
198198
JsonRender jsonRender = new JsonRender();
199-
String output = jsonRender.render(result);
200199
String outputFile = line.getOptionValue("json");
201-
writeOutput(output, outputFile);
200+
jsonRender.renderToFile(result, outputFile);
202201
}
203202
if (line.hasOption("state")) {
204203
System.out.println(result.isChanged().getValue());

core/src/main/java/org/openapitools/openapidiff/core/model/ChangedOpenApi.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.openapitools.openapidiff.core.model;
22

3+
import com.fasterxml.jackson.annotation.JsonIgnore;
34
import io.swagger.v3.oas.models.OpenAPI;
45
import java.util.List;
56
import java.util.Objects;
@@ -8,8 +9,8 @@
89
import org.openapitools.openapidiff.core.utils.EndpointUtils;
910

1011
public class ChangedOpenApi implements ComposedChanged {
11-
private OpenAPI oldSpecOpenApi;
12-
private OpenAPI newSpecOpenApi;
12+
@JsonIgnore private OpenAPI oldSpecOpenApi;
13+
@JsonIgnore private OpenAPI newSpecOpenApi;
1314
private List<Endpoint> newEndpoints;
1415
private List<Endpoint> missingEndpoints;
1516
private List<ChangedOperation> changedOperations;

core/src/main/java/org/openapitools/openapidiff/core/output/JsonRender.java

+12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.fasterxml.jackson.annotation.JsonInclude;
44
import com.fasterxml.jackson.core.JsonProcessingException;
55
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import java.io.IOException;
7+
import java.nio.file.Paths;
68
import org.openapitools.openapidiff.core.model.ChangedOpenApi;
79

810
public class JsonRender implements Render {
@@ -17,4 +19,14 @@ public String render(ChangedOpenApi diff) {
1719
throw new RuntimeException("Could not serialize diff as JSON", e);
1820
}
1921
}
22+
23+
public void renderToFile(ChangedOpenApi diff, String file) {
24+
try {
25+
objectMapper.writeValue(Paths.get(file).toFile(), diff);
26+
} catch (JsonProcessingException e) {
27+
throw new RuntimeException("Could not serialize diff as JSON", e);
28+
} catch (IOException e) {
29+
throw new RuntimeException("Could not write to JSON file", e);
30+
}
31+
}
2032
}

0 commit comments

Comments
 (0)