26
26
import io .swagger .v3 .oas .models .media .Schema ;
27
27
import io .swagger .v3 .oas .models .parameters .Parameter ;
28
28
import io .swagger .v3 .oas .models .responses .ApiResponse ;
29
+ import io .swagger .v3 .oas .models .security .SecurityRequirement ;
29
30
import j2html .tags .ContainerTag ;
30
31
import j2html .tags .specialized .DivTag ;
31
32
import j2html .tags .specialized .HtmlTag ;
46
47
import org .openapitools .openapidiff .core .model .ChangedParameters ;
47
48
import org .openapitools .openapidiff .core .model .ChangedResponse ;
48
49
import org .openapitools .openapidiff .core .model .ChangedSchema ;
50
+ import org .openapitools .openapidiff .core .model .ChangedSecurityRequirement ;
51
+ import org .openapitools .openapidiff .core .model .ChangedSecurityRequirements ;
49
52
import org .openapitools .openapidiff .core .model .DiffContext ;
50
53
import org .openapitools .openapidiff .core .model .DiffResult ;
51
54
import org .openapitools .openapidiff .core .model .Endpoint ;
@@ -60,15 +63,27 @@ public class HtmlRender implements Render {
60
63
61
64
private final String title ;
62
65
private final String linkCss ;
66
+ private final boolean showAllChanges ;
63
67
protected ChangedOpenApi diff ;
64
68
65
69
public HtmlRender () {
66
70
this ("Api Change Log" , "http://deepoove.com/swagger-diff/stylesheets/demo.css" );
67
71
}
68
72
73
+ public HtmlRender (boolean showAllChanges ) {
74
+ this ("Api Change Log" , "http://deepoove.com/swagger-diff/stylesheets/demo.css" , showAllChanges );
75
+ }
76
+
69
77
public HtmlRender (String title , String linkCss ) {
70
78
this .title = title ;
71
79
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 ;
72
87
}
73
88
74
89
public String render (ChangedOpenApi diff ) {
@@ -183,6 +198,11 @@ private OlTag ol_changed(List<ChangedOperation> changedOperations) {
183
198
ul_detail .with (
184
199
li ().with (h3 ("Response" )).with (ul_response (changedOperation .getApiResponses ())));
185
200
}
201
+ if (showAllChanges && changedOperation .resultSecurityRequirements ().isDifferent ()) {
202
+ ul_detail .with (
203
+ li ().with (h3 ("Security Requirements" ))
204
+ .with (ul_securityRequirements (changedOperation .getSecurityRequirements ())));
205
+ }
186
206
ol .with (
187
207
li ().with (span (method ).withClass (method ))
188
208
.withText (pathUrl + " " )
@@ -192,6 +212,52 @@ private OlTag ol_changed(List<ChangedOperation> changedOperations) {
192
212
return ol ;
193
213
}
194
214
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
+
195
261
private UlTag ul_response (ChangedApiResponse changedApiResponse ) {
196
262
Map <String , ApiResponse > addResponses = changedApiResponse .getIncreased ();
197
263
Map <String , ApiResponse > delResponses = changedApiResponse .getMissing ();
@@ -262,9 +328,12 @@ private LiTag li_changedRequest(String name, ChangedMediaType request) {
262
328
LiTag li =
263
329
li ().with (div_changedSchema (request .getSchema ()))
264
330
.withText (String .format ("Changed body: '%s'" , name ));
265
- if (request .isIncompatible ()) {
331
+ if (request .isIncompatible () && ! showAllChanges ) {
266
332
incompatibilities (li , request .getSchema ());
267
333
}
334
+ else if (showAllChanges ) {
335
+ allChanges (li , request .getSchema ());
336
+ }
268
337
return li ;
269
338
}
270
339
@@ -274,6 +343,28 @@ private DivTag div_changedSchema(ChangedSchema schema) {
274
343
return div ;
275
344
}
276
345
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
+
277
368
private void incompatibilities (final LiTag output , final ChangedSchema schema ) {
278
369
incompatibilities (output , "" , schema );
279
370
}
@@ -299,6 +390,10 @@ private void items(ContainerTag<?> output, String propName, ChangedSchema schema
299
390
incompatibilities (output , propName + "[n]" , schema );
300
391
}
301
392
393
+ private void itemsAllChanges (ContainerTag <?> output , String propName , ChangedSchema schema ) {
394
+ allChanges (output , propName + "[n]" , schema );
395
+ }
396
+
302
397
private void properties (
303
398
ContainerTag <?> output ,
304
399
String propPrefix ,
@@ -310,6 +405,17 @@ private void properties(
310
405
}
311
406
}
312
407
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
+
313
419
private void resolveProperty (
314
420
ContainerTag <?> output , String propPrefix , String key , Schema <?> value , String title ) {
315
421
try {
@@ -319,6 +425,15 @@ private void resolveProperty(
319
425
}
320
426
}
321
427
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
+
322
437
protected void property (ContainerTag <?> output , String name , String title , Schema <?> schema ) {
323
438
property (output , name , title , type (schema ));
324
439
}
@@ -332,6 +447,13 @@ protected Schema<?> resolve(Schema<?> schema) {
332
447
diff .getNewSpecOpenApi ().getComponents (), schema , schema .get$ref ());
333
448
}
334
449
450
+ protected Schema <?> resolve (ChangedSchema schema ) {
451
+ return refPointer .resolveRef (
452
+ diff .getNewSpecOpenApi ().getComponents (),
453
+ schema .getNewSchema (),
454
+ schema .getNewSchema ().get$ref ());
455
+ }
456
+
335
457
protected String type (Schema <?> schema ) {
336
458
String result = "object" ;
337
459
if (schema == null ) {
@@ -344,6 +466,10 @@ protected String type(Schema<?> schema) {
344
466
return result ;
345
467
}
346
468
469
+ protected String type (ChangedSchema schema ) {
470
+ return type (schema .getNewSchema ());
471
+ }
472
+
347
473
private UlTag ul_param (ChangedParameters changedParameters ) {
348
474
List <Parameter > addParameters = changedParameters .getIncreased ();
349
475
List <Parameter > delParameters = changedParameters .getMissing ();
0 commit comments