3333import io .swagger .v3 .oas .models .OpenAPI ;
3434import io .swagger .v3 .oas .models .Operation ;
3535import io .swagger .v3 .oas .models .PathItem ;
36+ import io .swagger .v3 .oas .models .PathItem .HttpMethod ;
3637import io .swagger .v3 .oas .models .Paths ;
3738import io .swagger .v3 .oas .models .info .Contact ;
3839import io .swagger .v3 .oas .models .info .Info ;
8485 */
8586public class SpecPropertiesCustomizer implements GlobalOpenApiCustomizer {
8687
88+ /**
89+ * The Open api properties.
90+ */
8791 private final OpenAPI openApiProperties ;
8892
93+ /**
94+ * Instantiates a new Spec properties customizer.
95+ *
96+ * @param springDocConfigProperties the spring doc config properties
97+ */
8998 public SpecPropertiesCustomizer (SpringDocConfigProperties springDocConfigProperties ) {
9099 this .openApiProperties = springDocConfigProperties .getOpenApi ();
91100 }
92101
102+ /**
103+ * Instantiates a new Spec properties customizer.
104+ *
105+ * @param openApiProperties the open api properties
106+ */
93107 public SpecPropertiesCustomizer (OpenAPI openApiProperties ) {
94108 this .openApiProperties = openApiProperties ;
95109 }
@@ -99,6 +113,12 @@ public void customise(OpenAPI openApi) {
99113 customizeOpenApi (openApi , openApiProperties );
100114 }
101115
116+ /**
117+ * Customize open api.
118+ *
119+ * @param openApi the open api
120+ * @param openApiProperties the open api properties
121+ */
102122 private void customizeOpenApi (OpenAPI openApi , OpenAPI openApiProperties ) {
103123 if (openApiProperties != null ) {
104124 Info infoProperties = openApiProperties .getInfo ();
@@ -108,12 +128,19 @@ private void customizeOpenApi(OpenAPI openApi, OpenAPI openApiProperties) {
108128 Components componentsProperties = openApiProperties .getComponents ();
109129 if (componentsProperties != null )
110130 customizeComponents (openApi , componentsProperties );
131+
111132 Paths pathsProperties = openApiProperties .getPaths ();
112133 if (pathsProperties != null )
113134 customizePaths (openApi , pathsProperties );
114135 }
115136 }
116137
138+ /**
139+ * Customize paths.
140+ *
141+ * @param openApi the open api
142+ * @param pathsProperties the paths properties
143+ */
117144 private void customizePaths (OpenAPI openApi , Paths pathsProperties ) {
118145 Paths paths = openApi .getPaths ();
119146 if (paths == null ) {
@@ -126,20 +153,30 @@ private void customizePaths(OpenAPI openApi, Paths pathsProperties) {
126153 }
127154 PathItem pathItemProperties = pathsProperties .get (path );
128155 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+ });
139170 }});
140171 }
141172 }
142-
173+
174+ /**
175+ * Customize components.
176+ *
177+ * @param openApi the open api
178+ * @param componentsProperties the components properties
179+ */
143180 private void customizeComponents (OpenAPI openApi , Components componentsProperties ) {
144181 Components components = openApi .getComponents ();
145182 if (components == null || CollectionUtils .isEmpty (components .getSchemas ())) {
@@ -158,44 +195,52 @@ private void customizeComponents(OpenAPI openApi, Components componentsPropertie
158195 properties .forEach ((propKey , propSchema ) -> {
159196 Schema propSchemaProperties = (Schema ) schemaProperties .getProperties ().get (propKey );
160197 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 );
163201 }
164202 });
165203 }
166204 });
167205 }
168206 }
169207
208+ /**
209+ * Customize info.
210+ *
211+ * @param openApi the open api
212+ * @param infoProperties the info properties
213+ */
170214 private void customizeInfo (OpenAPI openApi , Info infoProperties ) {
171215 Info info = openApi .getInfo ();
172216 if (info != null ) {
173217 resolveString (info ::title , infoProperties ::getTitle );
174218 resolveString (info ::description , infoProperties ::getDescription );
175219 resolveString (info ::version , infoProperties ::getVersion );
176220 resolveString (info ::termsOfService , infoProperties ::getTermsOfService );
177- }
178- else
179- openApi .info (infoProperties );
221+ resolveString (info ::summary , infoProperties ::getSummary );
180222
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 );
196241 }
197242 else
198- info . contact ( contactProperties );
243+ openApi . info ( infoProperties );
199244 }
200245
201246
@@ -212,4 +257,18 @@ private void resolveString(Consumer<String> setter, Supplier<Object> getter) {
212257 }
213258 }
214259
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+
215274}
0 commit comments