33
33
import io .swagger .v3 .oas .models .OpenAPI ;
34
34
import io .swagger .v3 .oas .models .Operation ;
35
35
import io .swagger .v3 .oas .models .PathItem ;
36
+ import io .swagger .v3 .oas .models .PathItem .HttpMethod ;
36
37
import io .swagger .v3 .oas .models .Paths ;
37
38
import io .swagger .v3 .oas .models .info .Contact ;
38
39
import io .swagger .v3 .oas .models .info .Info ;
84
85
*/
85
86
public class SpecPropertiesCustomizer implements GlobalOpenApiCustomizer {
86
87
88
+ /**
89
+ * The Open api properties.
90
+ */
87
91
private final OpenAPI openApiProperties ;
88
92
93
+ /**
94
+ * Instantiates a new Spec properties customizer.
95
+ *
96
+ * @param springDocConfigProperties the spring doc config properties
97
+ */
89
98
public SpecPropertiesCustomizer (SpringDocConfigProperties springDocConfigProperties ) {
90
99
this .openApiProperties = springDocConfigProperties .getOpenApi ();
91
100
}
92
101
102
+ /**
103
+ * Instantiates a new Spec properties customizer.
104
+ *
105
+ * @param openApiProperties the open api properties
106
+ */
93
107
public SpecPropertiesCustomizer (OpenAPI openApiProperties ) {
94
108
this .openApiProperties = openApiProperties ;
95
109
}
@@ -99,6 +113,12 @@ public void customise(OpenAPI openApi) {
99
113
customizeOpenApi (openApi , openApiProperties );
100
114
}
101
115
116
+ /**
117
+ * Customize open api.
118
+ *
119
+ * @param openApi the open api
120
+ * @param openApiProperties the open api properties
121
+ */
102
122
private void customizeOpenApi (OpenAPI openApi , OpenAPI openApiProperties ) {
103
123
if (openApiProperties != null ) {
104
124
Info infoProperties = openApiProperties .getInfo ();
@@ -108,12 +128,19 @@ private void customizeOpenApi(OpenAPI openApi, OpenAPI openApiProperties) {
108
128
Components componentsProperties = openApiProperties .getComponents ();
109
129
if (componentsProperties != null )
110
130
customizeComponents (openApi , componentsProperties );
131
+
111
132
Paths pathsProperties = openApiProperties .getPaths ();
112
133
if (pathsProperties != null )
113
134
customizePaths (openApi , pathsProperties );
114
135
}
115
136
}
116
137
138
+ /**
139
+ * Customize paths.
140
+ *
141
+ * @param openApi the open api
142
+ * @param pathsProperties the paths properties
143
+ */
117
144
private void customizePaths (OpenAPI openApi , Paths pathsProperties ) {
118
145
Paths paths = openApi .getPaths ();
119
146
if (paths == null ) {
@@ -126,20 +153,30 @@ private void customizePaths(OpenAPI openApi, Paths pathsProperties) {
126
153
}
127
154
PathItem pathItemProperties = pathsProperties .get (path );
128
155
if (pathItemProperties != null ) {
129
- resolveString (pathItem ::setDescription , pathItemProperties ::getDescription );
130
- resolveString (pathItem ::setSummary , pathItemProperties ::getSummary );
131
- List <Operation > operations = pathItem .readOperations ();
132
- List <Operation > operationsProperties = pathItemProperties .readOperations ();
133
- for (int i = 0 ; i < operations .size (); i ++) {
134
- Operation operation = operations .get (i );
135
- Operation operationProperties = operationsProperties .get (i );
136
- resolveString (operation ::setDescription , operationProperties ::getDescription );
137
- resolveString (operation ::setSummary , operationProperties ::getSummary );
138
- }
156
+ resolveString (pathItem ::description , pathItemProperties ::getDescription );
157
+ resolveString (pathItem ::summary , pathItemProperties ::getSummary );
158
+
159
+ Map <HttpMethod , Operation > operationMap = pathItem .readOperationsMap ();
160
+ Map <HttpMethod , Operation > operationMapProperties = pathItemProperties .readOperationsMap ();
161
+
162
+ operationMapProperties .forEach ((httpMethod , operationProperties ) -> {
163
+ Operation operationToCustomize = operationMap .get (httpMethod );
164
+ if (operationToCustomize != null ) {
165
+ resolveString (operationToCustomize ::description , operationProperties ::getDescription );
166
+ resolveString (operationToCustomize ::summary , operationProperties ::getSummary );
167
+ resolveSet (operationToCustomize ::tags , operationProperties ::getTags );
168
+ }
169
+ });
139
170
}});
140
171
}
141
172
}
142
-
173
+
174
+ /**
175
+ * Customize components.
176
+ *
177
+ * @param openApi the open api
178
+ * @param componentsProperties the components properties
179
+ */
143
180
private void customizeComponents (OpenAPI openApi , Components componentsProperties ) {
144
181
Components components = openApi .getComponents ();
145
182
if (components == null || CollectionUtils .isEmpty (components .getSchemas ())) {
@@ -158,44 +195,52 @@ private void customizeComponents(OpenAPI openApi, Components componentsPropertie
158
195
properties .forEach ((propKey , propSchema ) -> {
159
196
Schema propSchemaProperties = (Schema ) schemaProperties .getProperties ().get (propKey );
160
197
if (propSchemaProperties != null ) {
161
- resolveString (propSchema ::setDescription , propSchemaProperties ::getDescription );
162
- resolveString (propSchema ::setExample , propSchemaProperties ::getExample );
198
+ resolveString (propSchema ::description , propSchemaProperties ::getDescription );
199
+ resolveString (propSchema ::title , propSchemaProperties ::getTitle );
200
+ resolveString (propSchema ::example , propSchemaProperties ::getExample );
163
201
}
164
202
});
165
203
}
166
204
});
167
205
}
168
206
}
169
207
208
+ /**
209
+ * Customize info.
210
+ *
211
+ * @param openApi the open api
212
+ * @param infoProperties the info properties
213
+ */
170
214
private void customizeInfo (OpenAPI openApi , Info infoProperties ) {
171
215
Info info = openApi .getInfo ();
172
216
if (info != null ) {
173
217
resolveString (info ::title , infoProperties ::getTitle );
174
218
resolveString (info ::description , infoProperties ::getDescription );
175
219
resolveString (info ::version , infoProperties ::getVersion );
176
220
resolveString (info ::termsOfService , infoProperties ::getTermsOfService );
177
- }
178
- else
179
- openApi .info (infoProperties );
221
+ resolveString (info ::summary , infoProperties ::getSummary );
180
222
181
- License license = info .getLicense ();
182
- License licenseProperties = infoProperties .getLicense ();
183
- if (license != null ) {
184
- resolveString (license ::name , licenseProperties ::getName );
185
- resolveString (license ::url , licenseProperties ::getUrl );
186
- }
187
- else
188
- info .license (licenseProperties );
189
-
190
- Contact contact = info .getContact ();
191
- Contact contactProperties = infoProperties .getContact ();
192
- if (contact != null ) {
193
- resolveString (contact ::name , contactProperties ::getName );
194
- resolveString (contact ::email , contactProperties ::getEmail );
195
- resolveString (contact ::url , contactProperties ::getUrl );
223
+ License license = info .getLicense ();
224
+ License licenseProperties = infoProperties .getLicense ();
225
+ if (license != null ) {
226
+ resolveString (license ::name , licenseProperties ::getName );
227
+ resolveString (license ::url , licenseProperties ::getUrl );
228
+ }
229
+ else
230
+ info .license (licenseProperties );
231
+
232
+ Contact contact = info .getContact ();
233
+ Contact contactProperties = infoProperties .getContact ();
234
+ if (contact != null ) {
235
+ resolveString (contact ::name , contactProperties ::getName );
236
+ resolveString (contact ::email , contactProperties ::getEmail );
237
+ resolveString (contact ::url , contactProperties ::getUrl );
238
+ }
239
+ else
240
+ info .contact (contactProperties );
196
241
}
197
242
else
198
- info . contact ( contactProperties );
243
+ openApi . info ( infoProperties );
199
244
}
200
245
201
246
@@ -212,4 +257,18 @@ private void resolveString(Consumer<String> setter, Supplier<Object> getter) {
212
257
}
213
258
}
214
259
260
+ /**
261
+ * Resolve set.
262
+ *
263
+ * @param setter the setter
264
+ * @param getter the getter
265
+ */
266
+ private void resolveSet (Consumer <List > setter , Supplier <List > getter ) {
267
+ List value = getter .get ();
268
+ if (!CollectionUtils .isEmpty (value )) {
269
+ setter .accept (value );
270
+ }
271
+ }
272
+
273
+
215
274
}
0 commit comments