1
1
package com .spring4all .swagger ;
2
2
3
- import java . util . ArrayList ;
4
- import java . util . List ;
5
- import java .util .Map ;
3
+ import static com . google . common . collect . Lists . newArrayList ;
4
+
5
+ import java .util .* ;
6
6
import java .util .function .Predicate ;
7
7
import java .util .stream .Collectors ;
8
8
25
25
import springfox .documentation .schema .ScalarType ;
26
26
import springfox .documentation .service .ApiInfo ;
27
27
import springfox .documentation .service .Contact ;
28
+ import springfox .documentation .service .ParameterType ;
28
29
import springfox .documentation .service .RequestParameter ;
29
30
import springfox .documentation .spi .DocumentationType ;
30
31
import springfox .documentation .spring .web .plugins .Docket ;
@@ -45,6 +46,9 @@ public class DocketConfiguration implements BeanFactoryAware {
45
46
@ Autowired
46
47
private SwaggerProperties swaggerProperties ;
47
48
49
+ @ Autowired
50
+ private SwaggerAuthorizationConfiguration authConfiguration ;
51
+
48
52
private static final String BEAN_NAME = "spring-boot-starter-swagger-" ;
49
53
50
54
@ Override
@@ -70,7 +74,11 @@ public void createSpringFoxRestApi() {
70
74
71
75
Docket docket4Group = (Docket )beanFactory .getBean (beanName );
72
76
ApiInfo apiInfo = apiInfo (swaggerProperties );
73
- docket4Group .host (swaggerProperties .getHost ()).apiInfo (apiInfo ).select ()
77
+ docket4Group .host (swaggerProperties .getHost ()).apiInfo (apiInfo )
78
+ .globalRequestParameters (
79
+ assemblyRequestParameters (swaggerProperties .getGlobalOperationParameters (), new ArrayList <>()))
80
+ .securityContexts (Collections .singletonList (authConfiguration .securityContext ()))
81
+ .securitySchemes (authConfiguration .getSecuritySchemes ()).select ()
74
82
.apis (RequestHandlerSelectors .basePackage (swaggerProperties .getBasePackage ()))
75
83
.paths (paths (swaggerProperties .getBasePath (), swaggerProperties .getExcludePath ())).build ();
76
84
return ;
@@ -124,7 +132,11 @@ public void createSpringFoxRestApi() {
124
132
beanRegistry .registerBeanDefinition (beanName , beanDefinition4Group );
125
133
126
134
Docket docket4Group = (Docket )beanFactory .getBean (beanName );
127
- docket4Group .groupName (groupName ).host (docketInfo .getBasePackage ()).apiInfo (apiInfo ).select ()
135
+ docket4Group .groupName (groupName ).host (docketInfo .getBasePackage ()).apiInfo (apiInfo )
136
+ .globalRequestParameters (assemblyRequestParameters (swaggerProperties .getGlobalOperationParameters (),
137
+ docketInfo .getGlobalOperationParameters ()))
138
+ .securityContexts (Collections .singletonList (authConfiguration .securityContext ()))
139
+ .securitySchemes (authConfiguration .getSecuritySchemes ()).select ()
128
140
.apis (RequestHandlerSelectors .basePackage (docketInfo .getBasePackage ()))
129
141
.paths (paths (docketInfo .getBasePath (), docketInfo .getExcludePath ())).build ();
130
142
}
@@ -133,19 +145,53 @@ public void createSpringFoxRestApi() {
133
145
/**
134
146
* 全局请求参数
135
147
*
136
- * @param swaggerProperties
148
+ * @param properties
137
149
* {@link SwaggerProperties}
138
150
* @return RequestParameter {@link RequestParameter}
139
151
*/
140
- private List <RequestParameter > globalRequestParameters ( SwaggerProperties swaggerProperties ) {
141
- return swaggerProperties . getGlobalOperationParameters () .stream ()
152
+ private List <RequestParameter > getRequestParameters ( List < SwaggerProperties . GlobalOperationParameter > properties ) {
153
+ return properties .stream ()
142
154
.map (param -> new RequestParameterBuilder ().name (param .getName ()).description (param .getDescription ())
143
- .in (param .getParameterType ()).required (param .getRequired ())
144
- .query (q -> q .defaultValue (param .getModelRef ()))
145
- .query (q -> q .model (m -> m .scalarModel (ScalarType .STRING ))).build ())
155
+ .in (ParameterType .from (param .getParameterType ())).required (param .getRequired ())
156
+ .query (q -> q .defaultValue (param .getType ()))
157
+ .query (q -> q .model (m -> m .scalarModel (!ScalarType .from (param .getType (), param .getFormat ()).isPresent ()
158
+ ? ScalarType .STRING : ScalarType .from (param .getType (), param .getFormat ()).get ())))
159
+ .build ())
146
160
.collect (Collectors .toList ());
147
161
}
148
162
163
+ /**
164
+ * 局部参数按照name覆盖局部参数
165
+ *
166
+ * @param globalRequestParameters 全局配置
167
+ * @param groupRequestParameters Group 的配置
168
+ * @return 汇总配置
169
+ */
170
+ private List <RequestParameter > assemblyRequestParameters (
171
+ List <SwaggerProperties .GlobalOperationParameter > globalRequestParameters ,
172
+ List <SwaggerProperties .GlobalOperationParameter > groupRequestParameters ) {
173
+
174
+ if (Objects .isNull (groupRequestParameters ) || groupRequestParameters .isEmpty ()) {
175
+ return getRequestParameters (globalRequestParameters );
176
+ }
177
+
178
+ Set <String > paramNames = groupRequestParameters .stream ()
179
+ .map (SwaggerProperties .GlobalOperationParameter ::getName ).collect (Collectors .toSet ());
180
+
181
+ List <SwaggerProperties .GlobalOperationParameter > requestParameters = newArrayList ();
182
+
183
+ if (Objects .nonNull (globalRequestParameters )) {
184
+ for (SwaggerProperties .GlobalOperationParameter parameter : globalRequestParameters ) {
185
+ if (!paramNames .contains (parameter .getName ())) {
186
+ requestParameters .add (parameter );
187
+ }
188
+ }
189
+ }
190
+
191
+ requestParameters .addAll (groupRequestParameters );
192
+ return getRequestParameters (requestParameters );
193
+ }
194
+
149
195
/**
150
196
* API接口路径选择
151
197
*
0 commit comments