75
75
import org .slf4j .Logger ;
76
76
import org .slf4j .LoggerFactory ;
77
77
import org .springdoc .core .annotations .RouterOperations ;
78
- import org .springdoc .core .customizers .OpenApiCustomizer ;
79
- import org .springdoc .core .customizers .OpenApiLocaleCustomizer ;
78
+ import org .springdoc .core .customizers .DataRestRouterOperationCustomizer ;
80
79
import org .springdoc .core .customizers .OperationCustomizer ;
81
80
import org .springdoc .core .customizers .RouterOperationCustomizer ;
82
- import org .springdoc .core .filters . OpenApiMethodFilter ;
81
+ import org .springdoc .core .customizers . SpringDocCustomizers ;
83
82
import org .springdoc .core .fn .AbstractRouterFunctionVisitor ;
84
83
import org .springdoc .core .fn .RouterFunctionData ;
85
84
import org .springdoc .core .fn .RouterOperation ;
115
114
import static org .springdoc .core .converters .SchemaPropertyDeprecatingConverter .isDeprecated ;
116
115
import static org .springdoc .core .utils .Constants .ACTUATOR_DEFAULT_GROUP ;
117
116
import static org .springdoc .core .utils .Constants .DOT ;
118
- import static org .springdoc .core .utils .Constants .LINKS_SCHEMA_CUSTOMISER ;
119
117
import static org .springdoc .core .utils .Constants .OPERATION_ATTRIBUTE ;
120
118
import static org .springdoc .core .utils .Constants .SPRING_MVC_SERVLET_PATH ;
121
119
import static org .springframework .util .AntPathMatcher .DEFAULT_PATH_SEPARATOR ;
122
120
123
121
/**
124
122
* The type Abstract open api resource.
123
+ *
125
124
* @author bnasslahsen
126
125
* @author kevinraddatz
127
126
* @author hyeonisism
@@ -184,84 +183,46 @@ public abstract class AbstractOpenApiResource extends SpecFilter {
184
183
*/
185
184
private final OperationService operationParser ;
186
185
187
- /**
188
- * The Open api customisers.
189
- */
190
- private final Optional <List <OpenApiCustomizer >> openApiCustomizers ;
191
-
192
- /**
193
- * The Operation customizers.
194
- */
195
- private final Optional <List <OperationCustomizer >> operationCustomizers ;
196
-
197
- /**
198
- * The RouterOperation customizers.
199
- */
200
- private final Optional <List <RouterOperationCustomizer >> routerOperationCustomizers ;
201
-
202
- /**
203
- * The method filters to use.
204
- */
205
- private final Optional <List <OpenApiMethodFilter >> methodFilters ;
206
-
207
186
/**
208
187
* The Ant path matcher.
209
188
*/
210
189
private final AntPathMatcher antPathMatcher = new AntPathMatcher ();
211
190
212
- /**
213
- * The OpenApi with locale customizers.
214
- */
215
- private final Map <String , OpenApiLocaleCustomizer > openApiLocaleCustomizers ;
216
-
217
191
/**
218
192
* The Open api builder.
219
193
*/
220
194
protected OpenAPIService openAPIService ;
221
195
196
+ protected final SpringDocCustomizers springDocCustomizers ;
197
+
222
198
/**
223
199
* Instantiates a new Abstract open api resource.
200
+ *
224
201
* @param groupName the group name
225
202
* @param openAPIBuilderObjectFactory the open api builder object factory
226
203
* @param requestBuilder the request builder
227
204
* @param responseBuilder the response builder
228
205
* @param operationParser the operation parser
229
- * @param operationCustomizers the operation customizers
230
- * @param openApiCustomizers the open api customisers
231
- * @param routerOperationCustomizers the router operation customisers
232
- * @param methodFilters the method filters
233
206
* @param springDocConfigProperties the spring doc config properties
234
207
* @param springDocProviders the spring doc providers
208
+ * @param springDocCustomizers the spring doc customizers
235
209
*/
236
210
protected AbstractOpenApiResource (String groupName , ObjectFactory <OpenAPIService > openAPIBuilderObjectFactory ,
237
211
AbstractRequestService requestBuilder ,
238
212
GenericResponseService responseBuilder , OperationService operationParser ,
239
- Optional <List <OperationCustomizer >> operationCustomizers ,
240
- Optional <List <OpenApiCustomizer >> openApiCustomizers ,
241
- Optional <List <RouterOperationCustomizer >> routerOperationCustomizers ,
242
- Optional <List <OpenApiMethodFilter >> methodFilters ,
243
- SpringDocConfigProperties springDocConfigProperties , SpringDocProviders springDocProviders ) {
213
+ SpringDocConfigProperties springDocConfigProperties , SpringDocProviders springDocProviders , SpringDocCustomizers springDocCustomizers ) {
244
214
super ();
245
215
this .groupName = Objects .requireNonNull (groupName , "groupName" );
246
216
this .openAPIBuilderObjectFactory = openAPIBuilderObjectFactory ;
247
217
this .openAPIService = openAPIBuilderObjectFactory .getObject ();
248
218
this .requestBuilder = requestBuilder ;
249
219
this .responseBuilder = responseBuilder ;
250
220
this .operationParser = operationParser ;
251
- this .openApiCustomizers = openApiCustomizers ;
252
- this .routerOperationCustomizers = routerOperationCustomizers ;
253
- this .methodFilters = methodFilters ;
254
221
this .springDocProviders = springDocProviders ;
255
- //add the default customizers
256
- Map <String , OpenApiCustomizer > existingOpenApiCustomizers = openAPIService .getContext ().getBeansOfType (OpenApiCustomizer .class );
257
- if (!CollectionUtils .isEmpty (existingOpenApiCustomizers ) && existingOpenApiCustomizers .containsKey (LINKS_SCHEMA_CUSTOMISER ))
258
- openApiCustomizers .ifPresent (openApiCustomizersList -> openApiCustomizersList .add (existingOpenApiCustomizers .get (LINKS_SCHEMA_CUSTOMISER )));
222
+ this .springDocCustomizers = springDocCustomizers ;
259
223
this .springDocConfigProperties = springDocConfigProperties ;
260
- operationCustomizers .ifPresent (customizers -> customizers .removeIf (Objects ::isNull ));
261
- this .operationCustomizers = operationCustomizers ;
262
224
if (springDocConfigProperties .isPreLoadingEnabled ())
263
225
Executors .newSingleThreadExecutor ().execute (this ::getOpenApi );
264
- this .openApiLocaleCustomizers = openAPIService .getContext ().getBeansOfType (OpenApiLocaleCustomizer .class );
265
226
}
266
227
267
228
/**
@@ -341,6 +302,7 @@ private void getOpenApi() {
341
302
342
303
/**
343
304
* Gets open api.
305
+ *
344
306
* @param locale the locale
345
307
* @return the open api
346
308
*/
@@ -391,8 +353,8 @@ protected synchronized OpenAPI getOpenApi(Locale locale) {
391
353
LOGGER .warn ("Json Processing Exception occurred: {}" , e .getMessage ());
392
354
}
393
355
394
- openApiLocaleCustomizers .values ().forEach (openApiLocaleCustomizer -> openApiLocaleCustomizer .customise (openAPI , finalLocale ));
395
- openApiCustomizers .ifPresent (apiCustomizers -> apiCustomizers .forEach (openApiCustomizer -> openApiCustomizer .customise (openAPI )));
356
+ springDocCustomizers . getOpenApiLocaleCustomizers () .values ().forEach (openApiLocaleCustomizer -> openApiLocaleCustomizer .customise (openAPI , finalLocale ));
357
+ springDocCustomizers . getOpenApiCustomizers () .ifPresent (apiCustomizers -> apiCustomizers .forEach (openApiCustomizer -> openApiCustomizer .customise (openAPI )));
396
358
if (!CollectionUtils .isEmpty (openAPI .getServers ()) && !openAPI .getServers ().equals (serversCopy ))
397
359
openAPIService .setServersPresent (true );
398
360
@@ -609,6 +571,8 @@ else if (routerOperation.getOperationModel() != null && StringUtils.isNotBlank(r
609
571
* @param locale the locale
610
572
*/
611
573
protected void calculatePath (RouterOperation routerOperation , Locale locale , OpenAPI openAPI ) {
574
+ routerOperation = customizeDataRestRouterOperation (routerOperation );
575
+
612
576
String operationPath = routerOperation .getPath ();
613
577
io .swagger .v3 .oas .annotations .Operation apiOperation = routerOperation .getOperation ();
614
578
String [] methodConsumes = routerOperation .getConsumes ();
@@ -644,11 +608,28 @@ protected void calculatePath(RouterOperation routerOperation, Locale locale, Ope
644
608
parameter .setIn (ParameterIn .QUERY .toString ());
645
609
}
646
610
);
611
+
647
612
PathItem pathItemObject = buildPathItem (requestMethod , operation , operationPath , paths );
648
613
paths .addPathItem (operationPath , pathItemObject );
649
614
}
650
615
}
651
616
617
+ /**
618
+ * Customize data rest router operation router operation.
619
+ *
620
+ * @param routerOperation the router operation
621
+ * @return the router operation
622
+ */
623
+ private RouterOperation customizeDataRestRouterOperation (RouterOperation routerOperation ) {
624
+ if (springDocCustomizers .getDataRestRouterOperationCustomizers ().isPresent ()) {
625
+ List <DataRestRouterOperationCustomizer > dataRestRouterOperationCustomizerList = springDocCustomizers .getDataRestRouterOperationCustomizers ().get ();
626
+ for (DataRestRouterOperationCustomizer dataRestRouterOperationCustomizer : dataRestRouterOperationCustomizerList ) {
627
+ routerOperation = dataRestRouterOperationCustomizer .customize (routerOperation );
628
+ }
629
+ }
630
+ return routerOperation ;
631
+ }
632
+
652
633
/**
653
634
* Calculate path.
654
635
*
@@ -725,7 +706,7 @@ && isPackageToScan(handlerMethod.getBeanType().getPackage())
725
706
* @return whether the method should be included in the current OpenAPI definition
726
707
*/
727
708
protected boolean isMethodToFilter (HandlerMethod handlerMethod ) {
728
- return this .methodFilters
709
+ return this .springDocCustomizers . getMethodFilters ()
729
710
.map (Collection ::stream )
730
711
.map (stream -> stream .allMatch (m -> m .isMethodToInclude (handlerMethod .getMethod ())))
731
712
.orElse (true );
@@ -865,8 +846,8 @@ protected Set<RequestMethod> getDefaultAllowedHttpMethods() {
865
846
* @return the operation
866
847
*/
867
848
protected Operation customiseOperation (Operation operation , HandlerMethod handlerMethod ) {
868
- if (operationCustomizers .isPresent ()) {
869
- List <OperationCustomizer > operationCustomizerList = operationCustomizers .get ();
849
+ if (springDocCustomizers . getOperationCustomizers () .isPresent ()) {
850
+ List <OperationCustomizer > operationCustomizerList = springDocCustomizers . getOperationCustomizers () .get ();
870
851
for (OperationCustomizer operationCustomizer : operationCustomizerList )
871
852
operation = operationCustomizer .customize (operation , handlerMethod );
872
853
}
@@ -875,13 +856,14 @@ protected Operation customiseOperation(Operation operation, HandlerMethod handle
875
856
876
857
/**
877
858
* Customise router operation
859
+ *
878
860
* @param routerOperation
879
861
* @param handlerMethod
880
862
* @return the router operation
881
863
*/
882
864
protected RouterOperation customizeRouterOperation (RouterOperation routerOperation , HandlerMethod handlerMethod ) {
883
- if (routerOperationCustomizers .isPresent ()) {
884
- List <RouterOperationCustomizer > routerOperationCustomizerList = routerOperationCustomizers .get ();
865
+ if (springDocCustomizers . getRouterOperationCustomizers () .isPresent ()) {
866
+ List <RouterOperationCustomizer > routerOperationCustomizerList = springDocCustomizers . getRouterOperationCustomizers () .get ();
885
867
for (RouterOperationCustomizer routerOperationCustomizer : routerOperationCustomizerList ) {
886
868
routerOperation = routerOperationCustomizer .customize (routerOperation , handlerMethod );
887
869
}
@@ -1050,7 +1032,7 @@ private void fillParametersList(Operation operation, Map<String, String> queryPa
1050
1032
Collection <Parameter > headersMap = AbstractRequestService .getHeaders (methodAttributes , new LinkedHashMap <>());
1051
1033
headersMap .forEach (parameter -> {
1052
1034
Optional <Parameter > existingParam ;
1053
- if (!CollectionUtils .isEmpty (operation .getParameters ())){
1035
+ if (!CollectionUtils .isEmpty (operation .getParameters ())) {
1054
1036
existingParam = operation .getParameters ().stream ().filter (p -> parameter .getName ().equals (p .getName ())).findAny ();
1055
1037
if (existingParam .isEmpty ())
1056
1038
operation .addParametersItem (parameter );
@@ -1100,13 +1082,13 @@ private void fillRouterOperation(RouterFunctionData routerFunctionData, RouterOp
1100
1082
private PathItem buildPathItem (RequestMethod requestMethod , Operation operation , String operationPath ,
1101
1083
Paths paths ) {
1102
1084
PathItem pathItemObject ;
1103
- if (operation != null && !CollectionUtils .isEmpty (operation .getParameters ())){
1085
+ if (operation != null && !CollectionUtils .isEmpty (operation .getParameters ())) {
1104
1086
Iterator <Parameter > paramIt = operation .getParameters ().iterator ();
1105
- while (paramIt .hasNext ()){
1087
+ while (paramIt .hasNext ()) {
1106
1088
Parameter parameter = paramIt .next ();
1107
- if (ParameterIn .PATH .toString ().equals (parameter .getIn ())){
1089
+ if (ParameterIn .PATH .toString ().equals (parameter .getIn ())) {
1108
1090
// check it's present in the path
1109
- if (!operationPath .contains ("{" + parameter .getName () + "}" ))
1091
+ if (!operationPath .contains ("{" + parameter .getName () + "}" ))
1110
1092
paramIt .remove ();
1111
1093
}
1112
1094
}
@@ -1216,6 +1198,7 @@ else if (existingOperation != null) {
1216
1198
1217
1199
/**
1218
1200
* Init open api builder.
1201
+ *
1219
1202
* @param locale the locale
1220
1203
*/
1221
1204
protected void initOpenAPIBuilder (Locale locale ) {
@@ -1364,15 +1347,15 @@ && isConditionToMatch(consumes, ConditionType.CONSUMES)
1364
1347
*/
1365
1348
enum ConditionType {
1366
1349
/**
1367
- *Produces condition type.
1350
+ * Produces condition type.
1368
1351
*/
1369
1352
PRODUCES ,
1370
1353
/**
1371
- *Consumes condition type.
1354
+ * Consumes condition type.
1372
1355
*/
1373
1356
CONSUMES ,
1374
1357
/**
1375
- *Headers condition type.
1358
+ * Headers condition type.
1376
1359
*/
1377
1360
HEADERS
1378
1361
}
0 commit comments