2727import java .util .Arrays ;
2828import java .util .List ;
2929import java .util .Map ;
30+ import java .util .Objects ;
3031import java .util .Optional ;
3132import java .util .Set ;
3233import java .util .stream .Collectors ;
4950import org .springdoc .core .MethodAttributes ;
5051import org .springdoc .core .OpenAPIBuilder ;
5152import org .springdoc .core .OperationBuilder ;
53+ import org .springdoc .core .SpringDocConfigProperties ;
54+ import org .springdoc .core .SpringDocConfigProperties .GroupConfig ;
5255import org .springdoc .core .customizers .OpenApiCustomiser ;
5356
54- import org .springframework .beans .factory .annotation .Value ;
5557import org .springframework .core .annotation .AnnotationUtils ;
5658import org .springframework .util .AntPathMatcher ;
5759import org .springframework .util .CollectionUtils ;
6062import org .springframework .web .bind .annotation .RequestMethod ;
6163import org .springframework .web .method .HandlerMethod ;
6264
63- import static org .springdoc .core .Constants .SPRINGDOC_CACHE_DISABLED_VALUE ;
64- import static org .springdoc .core .Constants .SPRINGDOC_PACKAGES_TO_SCAN ;
65- import static org .springdoc .core .Constants .SPRINGDOC_PATHS_TO_MATCH ;
66-
6765public abstract class AbstractOpenApiResource {
6866
6967 private static final Logger LOGGER = LoggerFactory .getLogger (AbstractOpenApiResource .class );
@@ -80,45 +78,28 @@ public abstract class AbstractOpenApiResource {
8078
8179 private final AntPathMatcher antPathMatcher = new AntPathMatcher ();
8280
83- private boolean computeDone ;
84-
85- @ Value (SPRINGDOC_PACKAGES_TO_SCAN )
86- private List <String > packagesToScan ;
81+ private final SpringDocConfigProperties springDocConfigProperties ;
8782
88- @ Value (SPRINGDOC_PATHS_TO_MATCH )
89- private List <String > pathsToMatch ;
83+ private boolean computeDone ;
9084
91- @ Value (SPRINGDOC_CACHE_DISABLED_VALUE )
92- private boolean cacheDisabled ;
85+ private final String groupName ;
9386
94- protected AbstractOpenApiResource (OpenAPIBuilder openAPIBuilder , AbstractRequestBuilder requestBuilder ,
87+ protected AbstractOpenApiResource (String groupName , OpenAPIBuilder openAPIBuilder , AbstractRequestBuilder requestBuilder ,
9588 GenericResponseBuilder responseBuilder , OperationBuilder operationParser ,
96- Optional <List <OpenApiCustomiser >> openApiCustomisers ) {
89+ Optional <List <OpenApiCustomiser >> openApiCustomisers , SpringDocConfigProperties springDocConfigProperties ) {
9790 super ();
91+ this .groupName = Objects .requireNonNull (groupName , "groupName" );
9892 this .openAPIBuilder = openAPIBuilder ;
9993 this .requestBuilder = requestBuilder ;
10094 this .responseBuilder = responseBuilder ;
10195 this .operationParser = operationParser ;
10296 this .openApiCustomisers = openApiCustomisers ;
103- }
104-
105- protected AbstractOpenApiResource (OpenAPIBuilder openAPIBuilder , AbstractRequestBuilder requestBuilder ,
106- GenericResponseBuilder responseBuilder , OperationBuilder operationParser ,
107- Optional <List <OpenApiCustomiser >> openApiCustomisers , List <String > pathsToMatch , List <String > packagesToScan , boolean cacheDisabled ) {
108- super ();
109- this .openAPIBuilder = openAPIBuilder ;
110- this .requestBuilder = requestBuilder ;
111- this .responseBuilder = responseBuilder ;
112- this .operationParser = operationParser ;
113- this .openApiCustomisers = openApiCustomisers ;
114- this .pathsToMatch = pathsToMatch ;
115- this .packagesToScan = packagesToScan ;
116- this .cacheDisabled = cacheDisabled ;
97+ this .springDocConfigProperties = springDocConfigProperties ;
11798 }
11899
119100 protected synchronized OpenAPI getOpenApi () {
120101 OpenAPI openApi ;
121- if (!computeDone || cacheDisabled ) {
102+ if (!computeDone || Boolean . TRUE . equals ( springDocConfigProperties . getCache (). getDisabled ()) ) {
122103 Instant start = Instant .now ();
123104 openAPIBuilder .build ();
124105 Map <String , Object > restControllersMap = openAPIBuilder .getRestControllersMap ();
@@ -171,15 +152,15 @@ protected void calculatePath(OpenAPIBuilder openAPIBuilder, HandlerMethod handle
171152 continue ;
172153 }
173154
174- RequestMapping reqMappringClass = ReflectionUtils .getAnnotation (handlerMethod .getBeanType (),
155+ RequestMapping reqMappingClass = ReflectionUtils .getAnnotation (handlerMethod .getBeanType (),
175156 RequestMapping .class );
176157
177158 MethodAttributes methodAttributes = new MethodAttributes ();
178159 methodAttributes .setMethodOverloaded (existingOperation != null );
179160
180- if (reqMappringClass != null ) {
181- methodAttributes .setClassConsumes (reqMappringClass .consumes ());
182- methodAttributes .setClassProduces (reqMappringClass .produces ());
161+ if (reqMappingClass != null ) {
162+ methodAttributes .setClassConsumes (reqMappingClass .consumes ());
163+ methodAttributes .setClassProduces (reqMappingClass .produces ());
183164 }
184165
185166 methodAttributes .calculateConsumesProduces (method );
@@ -336,10 +317,22 @@ private PathItem buildPathItem(RequestMethod requestMethod, Operation operation,
336317 }
337318
338319 protected boolean isPackageToScan (String aPackage ) {
320+ List <String > packagesToScan = springDocConfigProperties .getPackagesToScan ();
321+ if (CollectionUtils .isEmpty (packagesToScan )) {
322+ Optional <GroupConfig > optionalGroupConfig = springDocConfigProperties .getGroupConfigs ().stream ().filter (groupConfig -> this .groupName .equals (groupConfig .getGroup ())).findAny ();
323+ if (optionalGroupConfig .isPresent ())
324+ packagesToScan = optionalGroupConfig .get ().getPackagesToScan ();
325+ }
339326 return CollectionUtils .isEmpty (packagesToScan ) || packagesToScan .stream ().anyMatch (pack -> aPackage .equals (pack ) || aPackage .startsWith (pack + "." ));
340327 }
341328
342329 protected boolean isPathToMatch (String operationPath ) {
330+ List <String > pathsToMatch = springDocConfigProperties .getPathsToMatch ();
331+ if (CollectionUtils .isEmpty (pathsToMatch )) {
332+ Optional <GroupConfig > optionalGroupConfig = springDocConfigProperties .getGroupConfigs ().stream ().filter (groupConfig -> this .groupName .equals (groupConfig .getGroup ())).findAny ();
333+ if (optionalGroupConfig .isPresent ())
334+ pathsToMatch = optionalGroupConfig .get ().getPathsToMatch ();
335+ }
343336 return CollectionUtils .isEmpty (pathsToMatch ) || pathsToMatch .stream ().anyMatch (pattern -> antPathMatcher .match (pattern , operationPath ));
344337 }
345338
0 commit comments