1
+ package org .springdoc .api ;
2
+
3
+ import com .fasterxml .jackson .core .JsonProcessingException ;
4
+ import io .swagger .v3 .oas .annotations .Operation ;
5
+ import org .springdoc .core .*;
6
+ import org .springframework .beans .factory .ObjectFactory ;
7
+ import org .springframework .beans .factory .annotation .Value ;
8
+ import org .springframework .boot .autoconfigure .condition .ConditionalOnExpression ;
9
+ import org .springframework .http .MediaType ;
10
+ import org .springframework .web .bind .annotation .GetMapping ;
11
+ import org .springframework .web .bind .annotation .PathVariable ;
12
+ import org .springframework .web .bind .annotation .RestController ;
13
+ import org .springframework .web .servlet .mvc .method .RequestMappingInfoHandlerMapping ;
14
+
15
+ import javax .servlet .http .HttpServletRequest ;
16
+ import java .util .List ;
17
+ import java .util .Map ;
18
+ import java .util .Optional ;
19
+ import java .util .stream .Collectors ;
20
+
21
+ import static org .springdoc .core .Constants .*;
22
+
23
+ @ RestController
24
+ @ ConditionalOnExpression (SPRINGDOC_GROUPS_CONDITION )
25
+ public class MultipleOpenApiResource {
26
+
27
+ private final RequestMappingInfoHandlerMapping requestMappingHandlerMapping ;
28
+
29
+ private final Optional <ActuatorProvider > servletContextProvider ;
30
+
31
+ private final Map <String , OpenApiResource > groupedOpenApiResources ;
32
+
33
+ @ Value (SPRINGDOC_SHOW_ACTUATOR_VALUE )
34
+ private boolean showActuator ;
35
+
36
+ public MultipleOpenApiResource (List <GroupedOpenApi > groupedOpenApis ,
37
+ ObjectFactory <OpenAPIBuilder > defaultOpenAPIBuilder , OpenAPIBuilder openAPIBuilder , AbstractRequestBuilder requestBuilder ,
38
+ AbstractResponseBuilder responseBuilder , OperationBuilder operationParser ,
39
+ RequestMappingInfoHandlerMapping requestMappingHandlerMapping , Optional <ActuatorProvider > servletContextProvider ,
40
+ Optional <List <OpenApiCustomiser >> openApiCustomisers ) {
41
+ this .requestMappingHandlerMapping = requestMappingHandlerMapping ;
42
+ this .servletContextProvider = servletContextProvider ;
43
+ this .groupedOpenApiResources = groupedOpenApis .stream ()
44
+ .collect (Collectors .toMap (GroupedOpenApi ::getGroup , item ->
45
+ new OpenApiResource (
46
+ defaultOpenAPIBuilder .getObject (),
47
+ requestBuilder ,
48
+ responseBuilder ,
49
+ operationParser ,
50
+ requestMappingHandlerMapping ,
51
+ servletContextProvider ,
52
+ Optional .of (item .getOpenApiCustomisers ()), item .getPathsToMatch ()
53
+ )
54
+ ));
55
+ }
56
+
57
+ @ Operation (hidden = true )
58
+ @ GetMapping (value = API_DOCS_URL + "/{group}" , produces = MediaType .APPLICATION_JSON_VALUE )
59
+ public String openapiJson (HttpServletRequest request , @ Value (API_DOCS_URL ) String apiDocsUrl ,
60
+ @ PathVariable String group )
61
+ throws JsonProcessingException {
62
+ return getOpenApiResourceOrThrow (group ).openapiJson (request , apiDocsUrl +"/" +group );
63
+ }
64
+
65
+ @ Operation (hidden = true )
66
+ @ GetMapping (value = DEFAULT_API_DOCS_URL_YAML + "/{group}" , produces = APPLICATION_OPENAPI_YAML )
67
+ public String openapiYaml (HttpServletRequest request , @ Value (DEFAULT_API_DOCS_URL_YAML ) String apiDocsUrl ,
68
+ @ PathVariable String group )
69
+ throws JsonProcessingException {
70
+ return getOpenApiResourceOrThrow (group ).openapiYaml (request , apiDocsUrl +"/" +group );
71
+ }
72
+
73
+
74
+ private OpenApiResource getOpenApiResourceOrThrow (String group ) {
75
+ OpenApiResource openApiResource = groupedOpenApiResources .get (group );
76
+ if (openApiResource == null ) {
77
+ throw new IllegalStateException ("No OpenAPI resource found for group " + group );
78
+ }
79
+ return openApiResource ;
80
+ }
81
+ }
0 commit comments