6
6
import io .swagger .v3 .oas .models .media .Schema ;
7
7
import io .swagger .v3 .oas .models .parameters .Parameter ;
8
8
import io .swagger .v3 .oas .models .responses .ApiResponse ;
9
+ import java .io .IOException ;
10
+ import java .io .OutputStreamWriter ;
9
11
import java .util .List ;
10
12
import java .util .Map ;
11
13
import java .util .Map .Entry ;
12
14
import java .util .Optional ;
13
15
import org .apache .commons .lang3 .StringUtils ;
16
+ import org .openapitools .openapidiff .core .exception .RendererException ;
14
17
import org .openapitools .openapidiff .core .model .*;
15
18
import org .openapitools .openapidiff .core .utils .RefPointer ;
16
19
import org .openapitools .openapidiff .core .utils .RefType ;
@@ -21,81 +24,78 @@ public class ConsoleRender implements Render {
21
24
protected ChangedOpenApi diff ;
22
25
23
26
@ Override
24
- public String render (ChangedOpenApi diff ) {
27
+ public void render (ChangedOpenApi diff , OutputStreamWriter outputStreamWriter ) {
25
28
this .diff = diff ;
26
- StringBuilder output = new StringBuilder ();
27
29
if (diff .isUnchanged ()) {
28
- output . append ( "No differences. Specifications are equivalents" );
30
+ safelyAppend ( outputStreamWriter , "No differences. Specifications are equivalents" );
29
31
} 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 ());
34
37
35
38
List <Endpoint > newEndpoints = diff .getNewEndpoints ();
36
- String ol_newEndpoint = listEndpoints (newEndpoints , "What's New" );
39
+ listEndpoints (newEndpoints , "What's New" , outputStreamWriter );
37
40
38
41
List <Endpoint > missingEndpoints = diff .getMissingEndpoints ();
39
- String ol_missingEndpoint = listEndpoints (missingEndpoints , "What's Deleted" );
42
+ listEndpoints (missingEndpoints , "What's Deleted" , outputStreamWriter );
40
43
41
44
List <Endpoint > deprecatedEndpoints = diff .getDeprecatedEndpoints ();
42
- String ol_deprecatedEndpoint = listEndpoints (deprecatedEndpoints , "What's Deprecated" );
45
+ listEndpoints (deprecatedEndpoints , "What's Deprecated" , outputStreamWriter );
43
46
44
47
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 );
58
64
}
59
- return output .toString ();
60
65
}
61
66
62
- private String ol_changed (List <ChangedOperation > operations ) {
67
+ private void ol_changed (
68
+ List <ChangedOperation > operations , OutputStreamWriter outputStreamWriter ) {
63
69
if (null == operations || operations .isEmpty ()) {
64
- return "" ;
70
+ return ;
65
71
}
66
- StringBuilder sb = new StringBuilder ();
67
- sb .append (title ("What's Changed" ));
72
+ safelyAppend (outputStreamWriter , title ("What's Changed" ));
68
73
for (ChangedOperation operation : operations ) {
69
74
String pathUrl = operation .getPathUrl ();
70
75
String method = operation .getHttpMethod ().toString ();
71
76
String desc =
72
77
Optional .ofNullable (operation .getSummary ()).map (ChangedMetadata ::getRight ).orElse ("" );
73
78
74
- StringBuilder ul_detail = new StringBuilder ();
75
79
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 ()));
81
84
}
82
85
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 ));
88
90
}
89
91
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 ()));
95
96
}
96
- sb . append ( itemEndpoint (method , pathUrl , desc )). append ( ul_detail );
97
+ safelyAppend ( outputStreamWriter , itemEndpoint (method , pathUrl , desc ));
97
98
}
98
- return sb .toString ();
99
99
}
100
100
101
101
private String ul_response (ChangedApiResponse changedApiResponse ) {
@@ -279,7 +279,8 @@ private String li_changedParam(ChangedParameter changeParam) {
279
279
}
280
280
}
281
281
282
- private String listEndpoints (List <Endpoint > endpoints , String title ) {
282
+ private String listEndpoints (
283
+ List <Endpoint > endpoints , String title , OutputStreamWriter outputStreamWriter ) {
283
284
if (null == endpoints || endpoints .isEmpty ()) {
284
285
return "" ;
285
286
}
@@ -317,8 +318,7 @@ public String title(String title, char ch) {
317
318
separator (ch ), little , StringUtils .center (title , LINE_LENGTH - 4 ), little , separator (ch ));
318
319
}
319
320
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 ();
323
323
}
324
324
}
0 commit comments