Skip to content

Commit 07c6a89

Browse files
author
Nithin Tatikonda
committed
Option to create html doc with all changes
1 parent e3cedbd commit 07c6a89

File tree

3 files changed

+144
-2
lines changed

3 files changed

+144
-2
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,15 @@ public static void main(String... args) {
9494
.longOpt("html")
9595
.hasArg()
9696
.argName("file")
97-
.desc("export diff as html in given file")
97+
.desc("export diff as html in given file with incompatible changes")
9898
.build());
99+
options.addOption(
100+
Option.builder()
101+
.longOpt("html-detailed")
102+
.hasArg()
103+
.argName("file")
104+
.desc("export diff as html in given file with all changes")
105+
.build());
99106
options.addOption(
100107
Option.builder()
101108
.longOpt("text")
@@ -183,6 +190,12 @@ public static void main(String... args) {
183190
String outputFile = line.getOptionValue("html");
184191
writeOutput(output, outputFile);
185192
}
193+
if (line.hasOption("html-detailed")) {
194+
HtmlRender htmlRender = new HtmlRender(true);
195+
String output = htmlRender.render(result);
196+
String outputFile = line.getOptionValue("html-detailed");
197+
writeOutput(output, outputFile);
198+
}
186199
if (line.hasOption("markdown")) {
187200
MarkdownRender mdRender = new MarkdownRender();
188201
String output = mdRender.render(result);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public DiffResult resultApiResponses() {
6363
public DiffResult resultRequestBody() {
6464
return requestBody == null ? DiffResult.NO_CHANGES : requestBody.isChanged();
6565
}
66+
public DiffResult resultSecurityRequirements() {
67+
return securityRequirements == null ? DiffResult.NO_CHANGES : securityRequirements.isChanged();
68+
}
6669

6770
public Operation getOldOperation() {
6871
return this.oldOperation;

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

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.swagger.v3.oas.models.media.Schema;
2727
import io.swagger.v3.oas.models.parameters.Parameter;
2828
import io.swagger.v3.oas.models.responses.ApiResponse;
29+
import io.swagger.v3.oas.models.security.SecurityRequirement;
2930
import j2html.tags.ContainerTag;
3031
import j2html.tags.specialized.DivTag;
3132
import j2html.tags.specialized.HtmlTag;
@@ -46,6 +47,8 @@
4647
import org.openapitools.openapidiff.core.model.ChangedParameters;
4748
import org.openapitools.openapidiff.core.model.ChangedResponse;
4849
import org.openapitools.openapidiff.core.model.ChangedSchema;
50+
import org.openapitools.openapidiff.core.model.ChangedSecurityRequirement;
51+
import org.openapitools.openapidiff.core.model.ChangedSecurityRequirements;
4952
import org.openapitools.openapidiff.core.model.DiffContext;
5053
import org.openapitools.openapidiff.core.model.DiffResult;
5154
import org.openapitools.openapidiff.core.model.Endpoint;
@@ -60,15 +63,27 @@ public class HtmlRender implements Render {
6063

6164
private final String title;
6265
private final String linkCss;
66+
private final boolean showAllChanges;
6367
protected ChangedOpenApi diff;
6468

6569
public HtmlRender() {
6670
this("Api Change Log", "http://deepoove.com/swagger-diff/stylesheets/demo.css");
6771
}
6872

73+
public HtmlRender(boolean showAllChanges) {
74+
this("Api Change Log", "http://deepoove.com/swagger-diff/stylesheets/demo.css", showAllChanges);
75+
}
76+
6977
public HtmlRender(String title, String linkCss) {
7078
this.title = title;
7179
this.linkCss = linkCss;
80+
this.showAllChanges = false;
81+
}
82+
83+
public HtmlRender(String title, String linkCss, boolean showAllChanges) {
84+
this.title = title;
85+
this.linkCss = linkCss;
86+
this.showAllChanges = showAllChanges;
7287
}
7388

7489
public String render(ChangedOpenApi diff) {
@@ -183,6 +198,11 @@ private OlTag ol_changed(List<ChangedOperation> changedOperations) {
183198
ul_detail.with(
184199
li().with(h3("Response")).with(ul_response(changedOperation.getApiResponses())));
185200
}
201+
if (showAllChanges && changedOperation.resultSecurityRequirements().isDifferent()) {
202+
ul_detail.with(
203+
li().with(h3("Security Requirements"))
204+
.with(ul_securityRequirements(changedOperation.getSecurityRequirements())));
205+
}
186206
ol.with(
187207
li().with(span(method).withClass(method))
188208
.withText(pathUrl + " ")
@@ -192,6 +212,52 @@ private OlTag ol_changed(List<ChangedOperation> changedOperations) {
192212
return ol;
193213
}
194214

215+
private UlTag ul_securityRequirements(ChangedSecurityRequirements changedSecurityRequirements) {
216+
List<SecurityRequirement> addRequirements = changedSecurityRequirements.getIncreased();
217+
List<SecurityRequirement> delRequirements = changedSecurityRequirements.getMissing();
218+
List<ChangedSecurityRequirement> changedRequirements = changedSecurityRequirements.getChanged();
219+
UlTag ul = ul().withClass("change security requirements");
220+
if (addRequirements != null) {
221+
for (SecurityRequirement addRequirement : addRequirements) {
222+
ul.with(li_addSecurityRequirement(addRequirement));
223+
}
224+
}
225+
if (delRequirements != null) {
226+
for (SecurityRequirement delRequirement : delRequirements) {
227+
ul.with(li_missingSecurityRequirement(delRequirement));
228+
}
229+
}
230+
if (changedRequirements != null) {
231+
for (ChangedSecurityRequirement changedRequirement : changedRequirements) {
232+
ul.with(li_changedSecurityRequirement(changedRequirement));
233+
}
234+
}
235+
236+
return ul;
237+
}
238+
239+
private LiTag li_addSecurityRequirement(SecurityRequirement securityRequirement) {
240+
return li().withText("New security requirement : ")
241+
.with(span(null == securityRequirement.toString() ? "" : (securityRequirement.toString())));
242+
}
243+
244+
private LiTag li_missingSecurityRequirement(SecurityRequirement securityRequirement) {
245+
return li().withText("Deleted security requirement : ")
246+
.with(span(null == securityRequirement.toString() ? "" : (securityRequirement.toString())));
247+
}
248+
249+
private LiTag li_changedSecurityRequirement(
250+
ChangedSecurityRequirement changedSecurityRequirement) {
251+
return li().withText(String.format("Changed security requirement : "))
252+
.with(
253+
span(
254+
(null == changedSecurityRequirement.getNewSecurityRequirement()
255+
|| null
256+
== changedSecurityRequirement.getNewSecurityRequirement().toString())
257+
? ""
258+
: (changedSecurityRequirement.getNewSecurityRequirement().toString())));
259+
}
260+
195261
private UlTag ul_response(ChangedApiResponse changedApiResponse) {
196262
Map<String, ApiResponse> addResponses = changedApiResponse.getIncreased();
197263
Map<String, ApiResponse> delResponses = changedApiResponse.getMissing();
@@ -262,9 +328,12 @@ private LiTag li_changedRequest(String name, ChangedMediaType request) {
262328
LiTag li =
263329
li().with(div_changedSchema(request.getSchema()))
264330
.withText(String.format("Changed body: '%s'", name));
265-
if (request.isIncompatible()) {
331+
if (request.isIncompatible() && !showAllChanges) {
266332
incompatibilities(li, request.getSchema());
267333
}
334+
else if (showAllChanges) {
335+
allChanges(li, request.getSchema());
336+
}
268337
return li;
269338
}
270339

@@ -274,6 +343,28 @@ private DivTag div_changedSchema(ChangedSchema schema) {
274343
return div;
275344
}
276345

346+
private void allChanges(final LiTag output, final ChangedSchema schema) {
347+
allChanges(output, "", schema);
348+
}
349+
350+
private void allChanges(
351+
final ContainerTag<?> output, String propName, final ChangedSchema schema) {
352+
String prefix = propName.isEmpty() ? "" : propName + ".";
353+
properties(
354+
output, prefix, "Missing property", schema.getMissingProperties(), schema.getContext());
355+
properties(
356+
output, prefix, "Added property", schema.getIncreasedProperties(), schema.getContext());
357+
358+
propertiesChanged(
359+
output, prefix, "Changed property", schema.getChangedProperties(), schema.getContext());
360+
if (schema.getItems() != null) {
361+
itemsAllChanges(output, propName, schema.getItems());
362+
}
363+
schema
364+
.getChangedProperties()
365+
.forEach((name, property) -> allChanges(output, prefix + name, property));
366+
}
367+
277368
private void incompatibilities(final LiTag output, final ChangedSchema schema) {
278369
incompatibilities(output, "", schema);
279370
}
@@ -299,6 +390,10 @@ private void items(ContainerTag<?> output, String propName, ChangedSchema schema
299390
incompatibilities(output, propName + "[n]", schema);
300391
}
301392

393+
private void itemsAllChanges(ContainerTag<?> output, String propName, ChangedSchema schema) {
394+
allChanges(output, propName + "[n]", schema);
395+
}
396+
302397
private void properties(
303398
ContainerTag<?> output,
304399
String propPrefix,
@@ -310,6 +405,17 @@ private void properties(
310405
}
311406
}
312407

408+
private void propertiesChanged(
409+
ContainerTag<?> output,
410+
String propPrefix,
411+
String title,
412+
Map<String, ChangedSchema> properties,
413+
DiffContext context) {
414+
if (properties != null) {
415+
properties.forEach((key, value) -> resolveProperty(output, propPrefix, key, value, title));
416+
}
417+
}
418+
313419
private void resolveProperty(
314420
ContainerTag<?> output, String propPrefix, String key, Schema<?> value, String title) {
315421
try {
@@ -319,6 +425,15 @@ private void resolveProperty(
319425
}
320426
}
321427

428+
private void resolveProperty(
429+
ContainerTag<?> output, String propPrefix, String key, ChangedSchema value, String title) {
430+
try {
431+
property(output, propPrefix + key, title, resolve(value));
432+
} catch (Exception e) {
433+
property(output, propPrefix + key, title, type(value));
434+
}
435+
}
436+
322437
protected void property(ContainerTag<?> output, String name, String title, Schema<?> schema) {
323438
property(output, name, title, type(schema));
324439
}
@@ -332,6 +447,13 @@ protected Schema<?> resolve(Schema<?> schema) {
332447
diff.getNewSpecOpenApi().getComponents(), schema, schema.get$ref());
333448
}
334449

450+
protected Schema<?> resolve(ChangedSchema schema) {
451+
return refPointer.resolveRef(
452+
diff.getNewSpecOpenApi().getComponents(),
453+
schema.getNewSchema(),
454+
schema.getNewSchema().get$ref());
455+
}
456+
335457
protected String type(Schema<?> schema) {
336458
String result = "object";
337459
if (schema == null) {
@@ -344,6 +466,10 @@ protected String type(Schema<?> schema) {
344466
return result;
345467
}
346468

469+
protected String type(ChangedSchema schema) {
470+
return type(schema.getNewSchema());
471+
}
472+
347473
private UlTag ul_param(ChangedParameters changedParameters) {
348474
List<Parameter> addParameters = changedParameters.getIncreased();
349475
List<Parameter> delParameters = changedParameters.getMissing();

0 commit comments

Comments
 (0)