27
27
import java .util .Arrays ;
28
28
import java .util .List ;
29
29
import java .util .Map ;
30
+ import java .util .Objects ;
30
31
import java .util .Optional ;
31
32
import java .util .Set ;
32
33
import java .util .stream .Collectors ;
49
50
import org .springdoc .core .MethodAttributes ;
50
51
import org .springdoc .core .OpenAPIBuilder ;
51
52
import org .springdoc .core .OperationBuilder ;
53
+ import org .springdoc .core .SpringDocConfigProperties ;
54
+ import org .springdoc .core .SpringDocConfigProperties .GroupConfig ;
52
55
import org .springdoc .core .customizers .OpenApiCustomiser ;
53
56
54
- import org .springframework .beans .factory .annotation .Value ;
55
57
import org .springframework .core .annotation .AnnotationUtils ;
56
58
import org .springframework .util .AntPathMatcher ;
57
59
import org .springframework .util .CollectionUtils ;
60
62
import org .springframework .web .bind .annotation .RequestMethod ;
61
63
import org .springframework .web .method .HandlerMethod ;
62
64
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
-
67
65
public abstract class AbstractOpenApiResource {
68
66
69
67
private static final Logger LOGGER = LoggerFactory .getLogger (AbstractOpenApiResource .class );
@@ -80,45 +78,28 @@ public abstract class AbstractOpenApiResource {
80
78
81
79
private final AntPathMatcher antPathMatcher = new AntPathMatcher ();
82
80
83
- private boolean computeDone ;
84
-
85
- @ Value (SPRINGDOC_PACKAGES_TO_SCAN )
86
- private List <String > packagesToScan ;
81
+ private final SpringDocConfigProperties springDocConfigProperties ;
87
82
88
- @ Value (SPRINGDOC_PATHS_TO_MATCH )
89
- private List <String > pathsToMatch ;
83
+ private boolean computeDone ;
90
84
91
- @ Value (SPRINGDOC_CACHE_DISABLED_VALUE )
92
- private boolean cacheDisabled ;
85
+ private final String groupName ;
93
86
94
- protected AbstractOpenApiResource (OpenAPIBuilder openAPIBuilder , AbstractRequestBuilder requestBuilder ,
87
+ protected AbstractOpenApiResource (String groupName , OpenAPIBuilder openAPIBuilder , AbstractRequestBuilder requestBuilder ,
95
88
GenericResponseBuilder responseBuilder , OperationBuilder operationParser ,
96
- Optional <List <OpenApiCustomiser >> openApiCustomisers ) {
89
+ Optional <List <OpenApiCustomiser >> openApiCustomisers , SpringDocConfigProperties springDocConfigProperties ) {
97
90
super ();
91
+ this .groupName = Objects .requireNonNull (groupName , "groupName" );
98
92
this .openAPIBuilder = openAPIBuilder ;
99
93
this .requestBuilder = requestBuilder ;
100
94
this .responseBuilder = responseBuilder ;
101
95
this .operationParser = operationParser ;
102
96
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 ;
117
98
}
118
99
119
100
protected synchronized OpenAPI getOpenApi () {
120
101
OpenAPI openApi ;
121
- if (!computeDone || cacheDisabled ) {
102
+ if (!computeDone || Boolean . TRUE . equals ( springDocConfigProperties . getCache (). getDisabled ()) ) {
122
103
Instant start = Instant .now ();
123
104
openAPIBuilder .build ();
124
105
Map <String , Object > restControllersMap = openAPIBuilder .getRestControllersMap ();
@@ -171,15 +152,15 @@ protected void calculatePath(OpenAPIBuilder openAPIBuilder, HandlerMethod handle
171
152
continue ;
172
153
}
173
154
174
- RequestMapping reqMappringClass = ReflectionUtils .getAnnotation (handlerMethod .getBeanType (),
155
+ RequestMapping reqMappingClass = ReflectionUtils .getAnnotation (handlerMethod .getBeanType (),
175
156
RequestMapping .class );
176
157
177
158
MethodAttributes methodAttributes = new MethodAttributes ();
178
159
methodAttributes .setMethodOverloaded (existingOperation != null );
179
160
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 ());
183
164
}
184
165
185
166
methodAttributes .calculateConsumesProduces (method );
@@ -336,10 +317,22 @@ private PathItem buildPathItem(RequestMethod requestMethod, Operation operation,
336
317
}
337
318
338
319
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
+ }
339
326
return CollectionUtils .isEmpty (packagesToScan ) || packagesToScan .stream ().anyMatch (pack -> aPackage .equals (pack ) || aPackage .startsWith (pack + "." ));
340
327
}
341
328
342
329
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
+ }
343
336
return CollectionUtils .isEmpty (pathsToMatch ) || pathsToMatch .stream ().anyMatch (pattern -> antPathMatcher .match (pattern , operationPath ));
344
337
}
345
338
0 commit comments