Skip to content

Commit 02dfb0a

Browse files
Merge branch 'master' into all-changes-html-option
2 parents 07c6a89 + d26a063 commit 02dfb0a

File tree

171 files changed

+5975
-1063
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

171 files changed

+5975
-1063
lines changed

README.md

+10-9
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ Add openapi-diff to your POM to show diffs when you test your Maven project. You
145145
<failOnIncompatible>true</failOnIncompatible>
146146
<!-- Fail if API changed (default: false) -->
147147
<failOnChanged>true</failOnChanged>
148+
<!-- Supply file path for console output to file if desired. -->
149+
<consoleOutputFileName>${project.basedir}/../maven/target/diff.txt</consoleOutputFileName>
150+
<!-- Supply file path for json output to file if desired. -->
151+
<jsonOutputFileName>${project.basedir}/../maven/target/diff.json</jsonOutputFileName>
152+
<!-- Supply file path for markdown output to file if desired. -->
153+
<markdownOutputFileName>${project.basedir}/../maven/target/diff.md</markdownOutputFileName>
148154
</configuration>
149155
</execution>
150156
</executions>
@@ -167,6 +173,7 @@ public class Main {
167173
```
168174
169175
### Render difference
176+
170177
---
171178
#### HTML
172179
@@ -176,11 +183,9 @@ String html = new HtmlRender("Changelog",
176183
.render(diff);
177184
178185
try {
179-
FileWriter fw = new FileWriter(
180-
"testNewApi.html");
186+
FileWriter fw = new FileWriter("testNewApi.html");
181187
fw.write(html);
182188
fw.close();
183-
184189
} catch (IOException e) {
185190
e.printStackTrace();
186191
}
@@ -191,11 +196,9 @@ try {
191196
```java
192197
String render = new MarkdownRender().render(diff);
193198
try {
194-
FileWriter fw = new FileWriter(
195-
"testDiff.md");
199+
FileWriter fw = new FileWriter("testDiff.md");
196200
fw.write(render);
197201
fw.close();
198-
199202
} catch (IOException e) {
200203
e.printStackTrace();
201204
}
@@ -207,11 +210,9 @@ try {
207210
```java
208211
String render = new JsonRender().render(diff);
209212
try {
210-
FileWriter fw = new FileWriter(
211-
"testDiff.json");
213+
FileWriter fw = new FileWriter("testDiff.json");
212214
fw.write(render);
213215
fw.close();
214-
215216
} catch (IOException e) {
216217
e.printStackTrace();
217218
}

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

+19-27
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import ch.qos.logback.classic.Level;
44
import io.swagger.v3.parser.core.models.AuthorizationValue;
5-
import java.io.File;
6-
import java.io.IOException;
7-
import java.nio.charset.StandardCharsets;
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.FileOutputStream;
7+
import java.io.OutputStreamWriter;
88
import java.util.Collections;
99
import java.util.List;
1010
import org.apache.commons.cli.CommandLine;
@@ -14,7 +14,6 @@
1414
import org.apache.commons.cli.Option;
1515
import org.apache.commons.cli.Options;
1616
import org.apache.commons.cli.ParseException;
17-
import org.apache.commons.io.FileUtils;
1817
import org.apache.commons.lang3.exception.ExceptionUtils;
1918
import org.openapitools.openapidiff.core.OpenApiCompare;
2019
import org.openapitools.openapidiff.core.model.ChangedOpenApi;
@@ -182,13 +181,16 @@ public static void main(String... args) {
182181
ChangedOpenApi result = OpenApiCompare.fromLocations(oldPath, newPath, auths);
183182
ConsoleRender consoleRender = new ConsoleRender();
184183
if (!logLevel.equals("OFF")) {
185-
System.out.println(consoleRender.render(result));
184+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
185+
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
186+
consoleRender.render(result, outputStreamWriter);
187+
System.out.println(outputStream);
186188
}
187189
if (line.hasOption("html")) {
188190
HtmlRender htmlRender = new HtmlRender();
189-
String output = htmlRender.render(result);
190-
String outputFile = line.getOptionValue("html");
191-
writeOutput(output, outputFile);
191+
FileOutputStream outputStream = new FileOutputStream(line.getOptionValue("html"));
192+
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
193+
htmlRender.render(result, outputStreamWriter);
192194
}
193195
if (line.hasOption("html-detailed")) {
194196
HtmlRender htmlRender = new HtmlRender(true);
@@ -198,19 +200,20 @@ public static void main(String... args) {
198200
}
199201
if (line.hasOption("markdown")) {
200202
MarkdownRender mdRender = new MarkdownRender();
201-
String output = mdRender.render(result);
202-
String outputFile = line.getOptionValue("markdown");
203-
writeOutput(output, outputFile);
203+
FileOutputStream outputStream = new FileOutputStream(line.getOptionValue("markdown"));
204+
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
205+
mdRender.render(result, outputStreamWriter);
204206
}
205207
if (line.hasOption("text")) {
206-
String output = consoleRender.render(result);
207-
String outputFile = line.getOptionValue("text");
208-
writeOutput(output, outputFile);
208+
FileOutputStream outputStream = new FileOutputStream(line.getOptionValue("text"));
209+
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
210+
consoleRender.render(result, outputStreamWriter);
209211
}
210212
if (line.hasOption("json")) {
211213
JsonRender jsonRender = new JsonRender();
212-
String outputFile = line.getOptionValue("json");
213-
jsonRender.renderToFile(result, outputFile);
214+
FileOutputStream outputStream = new FileOutputStream(line.getOptionValue("json"));
215+
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
216+
jsonRender.render(result, outputStreamWriter);
214217
}
215218
if (line.hasOption("state")) {
216219
System.out.println(result.isChanged().getValue());
@@ -235,17 +238,6 @@ public static void main(String... args) {
235238
}
236239
}
237240

238-
private static void writeOutput(String output, String outputFile) {
239-
File file = new File(outputFile);
240-
logger.debug("Output file: {}", file.getAbsolutePath());
241-
try {
242-
FileUtils.writeStringToFile(file, output, StandardCharsets.UTF_8);
243-
} catch (IOException e) {
244-
logger.error("Impossible to write output to file {}", outputFile, e);
245-
System.exit(2);
246-
}
247-
}
248-
249241
public static void printHelp(Options options) {
250242
HelpFormatter formatter = new HelpFormatter();
251243
formatter.printHelp("openapi-diff <old> <new>", options);

core/src/main/java/org/openapitools/openapidiff/core/compare/ParametersDiff.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Optional;
1010
import java.util.regex.Matcher;
1111
import java.util.regex.Pattern;
12+
import org.apache.commons.lang3.StringUtils;
1213
import org.openapitools.openapidiff.core.model.Changed;
1314
import org.openapitools.openapidiff.core.model.ChangedParameters;
1415
import org.openapitools.openapidiff.core.model.DiffContext;
@@ -99,7 +100,7 @@ public boolean pathUnchangedParametersChanged(
99100
// Speedy Check. Use the map already created in changedParameters to check if missing param is
100101
// linked to newParam
101102
String newParameterName = context.getParameters().get(parameter.getName());
102-
if (newParameterName.isEmpty()) return false;
103+
if (StringUtils.isBlank(newParameterName)) return false;
103104

104105
Optional<Parameter> newParameter =
105106
changedParameters.getIncreased().stream()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.openapitools.openapidiff.core.exception;
2+
3+
public class RendererException extends RuntimeException {
4+
5+
public RendererException(Throwable cause) {
6+
super(cause);
7+
}
8+
9+
public RendererException(String message, Throwable cause) {
10+
super(message, cause);
11+
}
12+
}

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

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

3-
import io.swagger.v3.oas.models.PathItem;
43
import io.swagger.v3.oas.models.media.Schema;
54
import java.util.*;
65
import java.util.stream.Collectors;
@@ -157,9 +156,6 @@ private DiffResult calculateCoreChanged() {
157156
}
158157

159158
private boolean compatibleForRequest() {
160-
if (PathItem.HttpMethod.PUT.equals(context.getMethod()) && !increasedProperties.isEmpty()) {
161-
return false;
162-
}
163159
return (oldSchema != null || newSchema == null);
164160
}
165161

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

+7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ public DiffResult isCoreChanged() {
4444
&& !changedBearerFormat
4545
&& !changedOpenIdConnectUrl
4646
&& (changedScopes == null || changedScopes.getIncreased().isEmpty())) {
47+
48+
// TODO: Dead code removal opportunity for changedType and changedIn. It appears that
49+
// SecuritySchemaDiff will never be given the chance to detect differences TYPE and
50+
// IN differences because that case has already been detected and filtered out by
51+
// SecurityRequirementsDiff and recorded as a dropped requirement in
52+
// ChangedSecurityRequirements.
53+
4754
return DiffResult.COMPATIBLE;
4855
}
4956
return DiffResult.INCOMPATIBLE;

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

+50-50
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
import io.swagger.v3.oas.models.media.Schema;
77
import io.swagger.v3.oas.models.parameters.Parameter;
88
import io.swagger.v3.oas.models.responses.ApiResponse;
9+
import java.io.IOException;
10+
import java.io.OutputStreamWriter;
911
import java.util.List;
1012
import java.util.Map;
1113
import java.util.Map.Entry;
1214
import java.util.Optional;
1315
import org.apache.commons.lang3.StringUtils;
16+
import org.openapitools.openapidiff.core.exception.RendererException;
1417
import org.openapitools.openapidiff.core.model.*;
1518
import org.openapitools.openapidiff.core.utils.RefPointer;
1619
import org.openapitools.openapidiff.core.utils.RefType;
@@ -21,81 +24,78 @@ public class ConsoleRender implements Render {
2124
protected ChangedOpenApi diff;
2225

2326
@Override
24-
public String render(ChangedOpenApi diff) {
27+
public void render(ChangedOpenApi diff, OutputStreamWriter outputStreamWriter) {
2528
this.diff = diff;
26-
StringBuilder output = new StringBuilder();
2729
if (diff.isUnchanged()) {
28-
output.append("No differences. Specifications are equivalents");
30+
safelyAppend(outputStreamWriter, "No differences. Specifications are equivalents");
2931
} else {
30-
output
31-
.append(bigTitle("Api Change Log"))
32-
.append(StringUtils.center(diff.getNewSpecOpenApi().getInfo().getTitle(), LINE_LENGTH))
33-
.append(System.lineSeparator());
32+
safelyAppend(outputStreamWriter, bigTitle("Api Change Log"));
33+
safelyAppend(
34+
outputStreamWriter,
35+
StringUtils.center(diff.getNewSpecOpenApi().getInfo().getTitle(), LINE_LENGTH));
36+
safelyAppend(outputStreamWriter, System.lineSeparator());
3437

3538
List<Endpoint> newEndpoints = diff.getNewEndpoints();
36-
String ol_newEndpoint = listEndpoints(newEndpoints, "What's New");
39+
listEndpoints(newEndpoints, "What's New", outputStreamWriter);
3740

3841
List<Endpoint> missingEndpoints = diff.getMissingEndpoints();
39-
String ol_missingEndpoint = listEndpoints(missingEndpoints, "What's Deleted");
42+
listEndpoints(missingEndpoints, "What's Deleted", outputStreamWriter);
4043

4144
List<Endpoint> deprecatedEndpoints = diff.getDeprecatedEndpoints();
42-
String ol_deprecatedEndpoint = listEndpoints(deprecatedEndpoints, "What's Deprecated");
45+
listEndpoints(deprecatedEndpoints, "What's Deprecated", outputStreamWriter);
4346

4447
List<ChangedOperation> changedOperations = diff.getChangedOperations();
45-
String ol_changed = ol_changed(changedOperations);
46-
47-
output
48-
.append(renderBody(ol_newEndpoint, ol_missingEndpoint, ol_deprecatedEndpoint, ol_changed))
49-
.append(title("Result"))
50-
.append(
51-
StringUtils.center(
52-
diff.isCompatible()
53-
? "API changes are backward compatible"
54-
: "API changes broke backward compatibility",
55-
LINE_LENGTH))
56-
.append(System.lineSeparator())
57-
.append(separator('-'));
48+
ol_changed(changedOperations, outputStreamWriter);
49+
50+
safelyAppend(
51+
outputStreamWriter,
52+
StringUtils.center(
53+
diff.isCompatible()
54+
? "API changes are backward compatible"
55+
: "API changes broke backward compatibility",
56+
LINE_LENGTH));
57+
safelyAppend(outputStreamWriter, System.lineSeparator());
58+
safelyAppend(outputStreamWriter, separator('-'));
59+
}
60+
try {
61+
outputStreamWriter.close();
62+
} catch (IOException e) {
63+
throw new RendererException(e);
5864
}
59-
return output.toString();
6065
}
6166

62-
private String ol_changed(List<ChangedOperation> operations) {
67+
private void ol_changed(
68+
List<ChangedOperation> operations, OutputStreamWriter outputStreamWriter) {
6369
if (null == operations || operations.isEmpty()) {
64-
return "";
70+
return;
6571
}
66-
StringBuilder sb = new StringBuilder();
67-
sb.append(title("What's Changed"));
72+
safelyAppend(outputStreamWriter, title("What's Changed"));
6873
for (ChangedOperation operation : operations) {
6974
String pathUrl = operation.getPathUrl();
7075
String method = operation.getHttpMethod().toString();
7176
String desc =
7277
Optional.ofNullable(operation.getSummary()).map(ChangedMetadata::getRight).orElse("");
7378

74-
StringBuilder ul_detail = new StringBuilder();
7579
if (result(operation.getParameters()).isDifferent()) {
76-
ul_detail
77-
.append(StringUtils.repeat(' ', 2))
78-
.append("Parameter:")
79-
.append(System.lineSeparator())
80-
.append(ul_param(operation.getParameters()));
80+
safelyAppend(outputStreamWriter, StringUtils.repeat(' ', 2));
81+
safelyAppend(outputStreamWriter, "Parameter:");
82+
safelyAppend(outputStreamWriter, System.lineSeparator());
83+
safelyAppend(outputStreamWriter, ul_param(operation.getParameters()));
8184
}
8285
if (operation.resultRequestBody().isDifferent()) {
83-
ul_detail
84-
.append(StringUtils.repeat(' ', 2))
85-
.append("Request:")
86-
.append(System.lineSeparator())
87-
.append(ul_content(operation.getRequestBody().getContent(), true));
86+
safelyAppend(outputStreamWriter, StringUtils.repeat(' ', 2));
87+
safelyAppend(outputStreamWriter, "Request:");
88+
safelyAppend(outputStreamWriter, System.lineSeparator());
89+
safelyAppend(outputStreamWriter, ul_content(operation.getRequestBody().getContent(), true));
8890
}
8991
if (operation.resultApiResponses().isDifferent()) {
90-
ul_detail
91-
.append(StringUtils.repeat(' ', 2))
92-
.append("Return Type:")
93-
.append(System.lineSeparator())
94-
.append(ul_response(operation.getApiResponses()));
92+
safelyAppend(outputStreamWriter, StringUtils.repeat(' ', 2));
93+
safelyAppend(outputStreamWriter, "Return Type:");
94+
safelyAppend(outputStreamWriter, System.lineSeparator());
95+
safelyAppend(outputStreamWriter, ul_response(operation.getApiResponses()));
9596
}
96-
sb.append(itemEndpoint(method, pathUrl, desc)).append(ul_detail);
97+
safelyAppend(outputStreamWriter, itemEndpoint(method, pathUrl, desc));
9798
}
98-
return sb.toString();
9999
}
100100

101101
private String ul_response(ChangedApiResponse changedApiResponse) {
@@ -279,7 +279,8 @@ private String li_changedParam(ChangedParameter changeParam) {
279279
}
280280
}
281281

282-
private String listEndpoints(List<Endpoint> endpoints, String title) {
282+
private String listEndpoints(
283+
List<Endpoint> endpoints, String title, OutputStreamWriter outputStreamWriter) {
283284
if (null == endpoints || endpoints.isEmpty()) {
284285
return "";
285286
}
@@ -317,8 +318,7 @@ public String title(String title, char ch) {
317318
separator(ch), little, StringUtils.center(title, LINE_LENGTH - 4), little, separator(ch));
318319
}
319320

320-
public StringBuilder separator(char ch) {
321-
StringBuilder sb = new StringBuilder();
322-
return sb.append(StringUtils.repeat(ch, LINE_LENGTH)).append(System.lineSeparator());
321+
public String separator(char ch) {
322+
return StringUtils.repeat(ch, LINE_LENGTH) + System.lineSeparator();
323323
}
324324
}

0 commit comments

Comments
 (0)