@@ -138,12 +138,12 @@ public abstract class AbstractOpenApiResource extends SpecFilter {
138
138
/**
139
139
* The constant ADDITIONAL_REST_CONTROLLERS.
140
140
*/
141
- private static final List <Class <?>> ADDITIONAL_REST_CONTROLLERS = new ArrayList <>();
141
+ private static final List <Class <?>> ADDITIONAL_REST_CONTROLLERS = Collections . synchronizedList ( new ArrayList <>() );
142
142
143
143
/**
144
144
* The constant HIDDEN_REST_CONTROLLERS.
145
145
*/
146
- private static final List <Class <?>> HIDDEN_REST_CONTROLLERS = new ArrayList <>();
146
+ private static final List <Class <?>> HIDDEN_REST_CONTROLLERS = Collections . synchronizedList ( new ArrayList <>() );
147
147
148
148
/**
149
149
* The Open api builder.
@@ -306,11 +306,11 @@ public static void addHiddenRestControllers(String... classes) {
306
306
* @return the open api
307
307
*/
308
308
protected synchronized OpenAPI getOpenApi (Locale locale ) {
309
- OpenAPI openApi ;
309
+ OpenAPI openAPI ;
310
310
final Locale finalLocale = locale == null ? Locale .getDefault () : locale ;
311
311
if (openAPIService .getCachedOpenAPI (finalLocale ) == null || springDocConfigProperties .isCacheDisabled ()) {
312
312
Instant start = Instant .now ();
313
- openAPIService .build (finalLocale );
313
+ openAPI = openAPIService .build (finalLocale );
314
314
Map <String , Object > mappingsMap = openAPIService .getMappingsMap ().entrySet ().stream ()
315
315
.filter (controller -> (AnnotationUtils .findAnnotation (controller .getValue ().getClass (),
316
316
Hidden .class ) == null ))
@@ -319,33 +319,32 @@ protected synchronized OpenAPI getOpenApi(Locale locale) {
319
319
320
320
Map <String , Object > findControllerAdvice = openAPIService .getControllerAdviceMap ();
321
321
// calculate generic responses
322
- openApi = openAPIService .getCalculatedOpenAPI ();
323
322
if (OpenApiVersion .OPENAPI_3_1 == springDocConfigProperties .getApiDocs ().getVersion ())
324
- openApi .openapi (OpenApiVersion .OPENAPI_3_1 .getVersion ());
323
+ openAPI .openapi (OpenApiVersion .OPENAPI_3_1 .getVersion ());
325
324
if (springDocConfigProperties .isDefaultOverrideWithGenericResponse ()) {
326
325
if (!CollectionUtils .isEmpty (mappingsMap ))
327
326
findControllerAdvice .putAll (mappingsMap );
328
- responseBuilder .buildGenericResponse (openApi .getComponents (), findControllerAdvice , finalLocale );
327
+ responseBuilder .buildGenericResponse (openAPI .getComponents (), findControllerAdvice , finalLocale );
329
328
}
330
- getPaths (mappingsMap , finalLocale );
329
+ getPaths (mappingsMap , finalLocale , openAPI );
331
330
332
331
Optional <CloudFunctionProvider > cloudFunctionProviderOptional = springDocProviders .getSpringCloudFunctionProvider ();
333
332
cloudFunctionProviderOptional .ifPresent (cloudFunctionProvider -> {
334
- List <RouterOperation > routerOperationList = cloudFunctionProvider .getRouterOperations (openApi );
333
+ List <RouterOperation > routerOperationList = cloudFunctionProvider .getRouterOperations (openAPI );
335
334
if (!CollectionUtils .isEmpty (routerOperationList ))
336
- this .calculatePath (routerOperationList , locale );
335
+ this .calculatePath (routerOperationList , locale , openAPI );
337
336
}
338
337
);
339
338
340
- if (!CollectionUtils .isEmpty (openApi .getServers ()))
339
+ if (!CollectionUtils .isEmpty (openAPI .getServers ()))
341
340
openAPIService .setServersPresent (true );
342
- openAPIService .updateServers (openApi );
341
+ openAPIService .updateServers (openAPI );
343
342
344
343
if (springDocConfigProperties .isRemoveBrokenReferenceDefinitions ())
345
- this .removeBrokenReferenceDefinitions (openApi );
344
+ this .removeBrokenReferenceDefinitions (openAPI );
346
345
347
346
// run the optional customisers
348
- List <Server > servers = openApi .getServers ();
347
+ List <Server > servers = openAPI .getServers ();
349
348
List <Server > serversCopy = null ;
350
349
try {
351
350
serversCopy = springDocProviders .jsonMapper ()
@@ -355,40 +354,41 @@ protected synchronized OpenAPI getOpenApi(Locale locale) {
355
354
LOGGER .warn ("Json Processing Exception occurred: {}" , e .getMessage ());
356
355
}
357
356
358
- openApiLocaleCustomizers .values ().forEach (openApiLocaleCustomizer -> openApiLocaleCustomizer .customise (openApi , finalLocale ));
359
- openApiCustomizers .ifPresent (apiCustomisers -> apiCustomisers .forEach (openApiCustomizer -> openApiCustomizer .customise (openApi )));
360
- if (!CollectionUtils .isEmpty (openApi .getServers ()) && !openApi .getServers ().equals (serversCopy ))
357
+ openApiLocaleCustomizers .values ().forEach (openApiLocaleCustomizer -> openApiLocaleCustomizer .customise (openAPI , finalLocale ));
358
+ openApiCustomizers .ifPresent (apiCustomisers -> apiCustomisers .forEach (openApiCustomizer -> openApiCustomizer .customise (openAPI )));
359
+ if (!CollectionUtils .isEmpty (openAPI .getServers ()) && !openAPI .getServers ().equals (serversCopy ))
361
360
openAPIService .setServersPresent (true );
362
361
363
- openAPIService .setCachedOpenAPI (openApi , finalLocale );
364
- openAPIService .resetCalculatedOpenAPI ();
362
+ openAPIService .setCachedOpenAPI (openAPI , finalLocale );
365
363
366
364
LOGGER .info ("Init duration for springdoc-openapi is: {} ms" ,
367
365
Duration .between (start , Instant .now ()).toMillis ());
368
366
}
369
367
else {
370
368
LOGGER .debug ("Fetching openApi document from cache" );
371
- openApi = openAPIService .updateServers (openAPIService .getCachedOpenAPI (finalLocale ));
369
+ openAPI = openAPIService .updateServers (openAPIService .getCachedOpenAPI (finalLocale ));
372
370
}
373
- return openApi ;
371
+ return openAPI ;
374
372
}
375
373
376
374
/**
377
375
* Gets paths.
378
376
*
379
377
* @param findRestControllers the find rest controllers
380
378
* @param locale the locale
379
+ * @param openAPI the open api
381
380
*/
382
- protected abstract void getPaths (Map <String , Object > findRestControllers , Locale locale );
381
+ protected abstract void getPaths (Map <String , Object > findRestControllers , Locale locale , OpenAPI openAPI );
383
382
384
383
/**
385
384
* Calculate path.
386
385
*
387
386
* @param handlerMethod the handler method
388
387
* @param routerOperation the router operation
389
388
* @param locale the locale
389
+ * @param openAPI the open api
390
390
*/
391
- protected void calculatePath (HandlerMethod handlerMethod , RouterOperation routerOperation , Locale locale ) {
391
+ protected void calculatePath (HandlerMethod handlerMethod , RouterOperation routerOperation , Locale locale , OpenAPI openAPI ) {
392
392
String operationPath = routerOperation .getPath ();
393
393
Set <RequestMethod > requestMethods = new HashSet <>(Arrays .asList (routerOperation .getMethods ()));
394
394
io .swagger .v3 .oas .annotations .Operation apiOperation = routerOperation .getOperation ();
@@ -397,7 +397,6 @@ protected void calculatePath(HandlerMethod handlerMethod, RouterOperation router
397
397
String [] headers = routerOperation .getHeaders ();
398
398
Map <String , String > queryParams = routerOperation .getQueryParams ();
399
399
400
- OpenAPI openAPI = openAPIService .getCalculatedOpenAPI ();
401
400
Components components = openAPI .getComponents ();
402
401
Paths paths = openAPI .getPaths ();
403
402
@@ -516,8 +515,9 @@ private void buildCallbacks(OpenAPI openAPI, MethodAttributes methodAttributes,
516
515
*
517
516
* @param routerOperationList the router operation list
518
517
* @param locale the locale
518
+ * @param openAPI the open api
519
519
*/
520
- protected void calculatePath (List <RouterOperation > routerOperationList , Locale locale ) {
520
+ protected void calculatePath (List <RouterOperation > routerOperationList , Locale locale , OpenAPI openAPI ) {
521
521
ApplicationContext applicationContext = openAPIService .getContext ();
522
522
if (!CollectionUtils .isEmpty (routerOperationList )) {
523
523
Collections .sort (routerOperationList );
@@ -547,14 +547,14 @@ protected void calculatePath(List<RouterOperation> routerOperationList, Locale l
547
547
LOGGER .error (e .getMessage ());
548
548
}
549
549
if (handlerMethod != null && isFilterCondition (handlerMethod , routerOperation .getPath (), routerOperation .getProduces (), routerOperation .getConsumes (), routerOperation .getHeaders ()))
550
- calculatePath (handlerMethod , routerOperation , locale );
550
+ calculatePath (handlerMethod , routerOperation , locale , openAPI );
551
551
}
552
552
}
553
553
else if (routerOperation .getOperation () != null && StringUtils .isNotBlank (routerOperation .getOperation ().operationId ()) && isFilterCondition (routerOperation .getPath (), routerOperation .getProduces (), routerOperation .getConsumes (), routerOperation .getHeaders ())) {
554
- calculatePath (routerOperation , locale );
554
+ calculatePath (routerOperation , locale , openAPI );
555
555
}
556
556
else if (routerOperation .getOperationModel () != null && StringUtils .isNotBlank (routerOperation .getOperationModel ().getOperationId ()) && isFilterCondition (routerOperation .getPath (), routerOperation .getProduces (), routerOperation .getConsumes (), routerOperation .getHeaders ())) {
557
- calculatePath (routerOperation , locale );
557
+ calculatePath (routerOperation , locale , openAPI );
558
558
}
559
559
}
560
560
}
@@ -566,15 +566,14 @@ else if (routerOperation.getOperationModel() != null && StringUtils.isNotBlank(r
566
566
* @param routerOperation the router operation
567
567
* @param locale the locale
568
568
*/
569
- protected void calculatePath (RouterOperation routerOperation , Locale locale ) {
569
+ protected void calculatePath (RouterOperation routerOperation , Locale locale , OpenAPI openAPI ) {
570
570
String operationPath = routerOperation .getPath ();
571
571
io .swagger .v3 .oas .annotations .Operation apiOperation = routerOperation .getOperation ();
572
572
String [] methodConsumes = routerOperation .getConsumes ();
573
573
String [] methodProduces = routerOperation .getProduces ();
574
574
String [] headers = routerOperation .getHeaders ();
575
575
Map <String , String > queryParams = routerOperation .getQueryParams ();
576
576
577
- OpenAPI openAPI = openAPIService .getCalculatedOpenAPI ();
578
577
Paths paths = openAPI .getPaths ();
579
578
Map <HttpMethod , Operation > operationMap = null ;
580
579
if (paths .containsKey (operationPath )) {
@@ -620,8 +619,8 @@ protected void calculatePath(RouterOperation routerOperation, Locale locale) {
620
619
* @param locale the locale
621
620
*/
622
621
protected void calculatePath (HandlerMethod handlerMethod , String operationPath ,
623
- Set <RequestMethod > requestMethods ,String [] consumes , String [] produces , String [] headers , Locale locale ) {
624
- this .calculatePath (handlerMethod , new RouterOperation (operationPath , requestMethods .toArray (new RequestMethod [requestMethods .size ()]), consumes , produces , headers ), locale );
622
+ Set <RequestMethod > requestMethods ,String [] consumes , String [] produces , String [] headers , Locale locale , OpenAPI openAPI ) {
623
+ this .calculatePath (handlerMethod , new RouterOperation (operationPath , requestMethods .toArray (new RequestMethod [requestMethods .size ()]), consumes , produces , headers ), locale , openAPI );
625
624
}
626
625
627
626
/**
@@ -630,13 +629,15 @@ protected void calculatePath(HandlerMethod handlerMethod, String operationPath,
630
629
* @param beanName the bean name
631
630
* @param routerFunctionVisitor the router function visitor
632
631
* @param locale the locale
632
+ * @param openAPI the open api
633
633
*/
634
- protected void getRouterFunctionPaths (String beanName , AbstractRouterFunctionVisitor routerFunctionVisitor , Locale locale ) {
634
+ protected void getRouterFunctionPaths (String beanName , AbstractRouterFunctionVisitor routerFunctionVisitor ,
635
+ Locale locale , OpenAPI openAPI ) {
635
636
boolean withRouterOperation = routerFunctionVisitor .getRouterFunctionDatas ().stream ()
636
637
.anyMatch (routerFunctionData -> routerFunctionData .getAttributes ().containsKey (OPERATION_ATTRIBUTE ));
637
638
if (withRouterOperation ) {
638
639
List <RouterOperation > operationList = routerFunctionVisitor .getRouterFunctionDatas ().stream ().map (RouterOperation ::new ).collect (Collectors .toList ());
639
- calculatePath (operationList , locale );
640
+ calculatePath (operationList , locale , openAPI );
640
641
}
641
642
else {
642
643
List <org .springdoc .core .annotations .RouterOperation > routerOperationList = new ArrayList <>();
@@ -650,11 +651,11 @@ protected void getRouterFunctionPaths(String beanName, AbstractRouterFunctionVis
650
651
else
651
652
routerOperationList .addAll (Arrays .asList (routerOperations .value ()));
652
653
if (routerOperationList .size () == 1 )
653
- calculatePath (routerOperationList .stream ().map (routerOperation -> new RouterOperation (routerOperation , routerFunctionVisitor .getRouterFunctionDatas ().get (0 ))).collect (Collectors .toList ()), locale );
654
+ calculatePath (routerOperationList .stream ().map (routerOperation -> new RouterOperation (routerOperation , routerFunctionVisitor .getRouterFunctionDatas ().get (0 ))).collect (Collectors .toList ()), locale , openAPI );
654
655
else {
655
656
List <RouterOperation > operationList = routerOperationList .stream ().map (RouterOperation ::new ).collect (Collectors .toList ());
656
657
mergeRouters (routerFunctionVisitor .getRouterFunctionDatas (), operationList );
657
- calculatePath (operationList , locale );
658
+ calculatePath (operationList , locale , openAPI );
658
659
}
659
660
}
660
661
}
0 commit comments