|
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 |
|
|
19 | 19 |
|
20 | 20 | import com.google.common.base.Predicates; |
21 | 21 |
|
| 22 | +import org.springframework.context.annotation.DependsOn; |
22 | 23 | import springfox.documentation.builders.ApiInfoBuilder; |
23 | 24 | import springfox.documentation.builders.RequestHandlerSelectors; |
24 | 25 | import springfox.documentation.builders.RequestParameterBuilder; |
25 | 26 | import springfox.documentation.schema.ScalarType; |
26 | 27 | import springfox.documentation.service.ApiInfo; |
27 | 28 | import springfox.documentation.service.Contact; |
| 29 | +import springfox.documentation.service.ParameterType; |
28 | 30 | import springfox.documentation.service.RequestParameter; |
29 | 31 | import springfox.documentation.spi.DocumentationType; |
30 | 32 | import springfox.documentation.spring.web.plugins.Docket; |
@@ -70,8 +72,10 @@ public void createSpringFoxRestApi() { |
70 | 72 |
|
71 | 73 | Docket docket4Group = (Docket)beanFactory.getBean(beanName); |
72 | 74 | ApiInfo apiInfo = apiInfo(swaggerProperties); |
73 | | - docket4Group.host(swaggerProperties.getHost()).apiInfo(apiInfo).select() |
74 | | - .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage())) |
| 75 | + docket4Group.host(swaggerProperties.getHost()).apiInfo(apiInfo) |
| 76 | + .globalRequestParameters( |
| 77 | + assemblyRequestParameters(swaggerProperties.getGlobalOperationParameters(), new ArrayList<>())) |
| 78 | + .select().apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage())) |
75 | 79 | .paths(paths(swaggerProperties.getBasePath(), swaggerProperties.getExcludePath())).build(); |
76 | 80 | return; |
77 | 81 | } |
@@ -124,28 +128,64 @@ public void createSpringFoxRestApi() { |
124 | 128 | beanRegistry.registerBeanDefinition(beanName, beanDefinition4Group); |
125 | 129 |
|
126 | 130 | Docket docket4Group = (Docket)beanFactory.getBean(beanName); |
127 | | - docket4Group.groupName(groupName).host(docketInfo.getBasePackage()).apiInfo(apiInfo).select() |
128 | | - .apis(RequestHandlerSelectors.basePackage(docketInfo.getBasePackage())) |
| 131 | + docket4Group.groupName(groupName).host(docketInfo.getBasePackage()).apiInfo(apiInfo) |
| 132 | + .globalRequestParameters(assemblyRequestParameters(swaggerProperties.getGlobalOperationParameters(), |
| 133 | + docketInfo.getGlobalOperationParameters())) |
| 134 | + .select().apis(RequestHandlerSelectors.basePackage(docketInfo.getBasePackage())) |
129 | 135 | .paths(paths(docketInfo.getBasePath(), docketInfo.getExcludePath())).build(); |
130 | 136 | } |
131 | 137 | } |
132 | 138 |
|
133 | 139 | /** |
134 | 140 | * 全局请求参数 |
135 | 141 | * |
136 | | - * @param swaggerProperties |
| 142 | + * @param properties |
137 | 143 | * {@link SwaggerProperties} |
138 | 144 | * @return RequestParameter {@link RequestParameter} |
139 | 145 | */ |
140 | | - private List<RequestParameter> globalRequestParameters(SwaggerProperties swaggerProperties) { |
141 | | - return swaggerProperties.getGlobalOperationParameters().stream() |
| 146 | + private List<RequestParameter> getRequestParameters(List<SwaggerProperties.GlobalOperationParameter> properties) { |
| 147 | + return properties.stream() |
142 | 148 | .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()) |
| 149 | + .in(ParameterType.from(param.getParameterType())).required(param.getRequired()) |
| 150 | + .query(q -> q.defaultValue(param.getType())) |
| 151 | + .query(q -> q.model(m -> m.scalarModel(!ScalarType.from(param.getType(), param.getFormat()).isPresent() |
| 152 | + ? ScalarType.STRING : ScalarType.from(param.getType(), param.getFormat()).get()))) |
| 153 | + .build()) |
146 | 154 | .collect(Collectors.toList()); |
147 | 155 | } |
148 | 156 |
|
| 157 | + /** |
| 158 | + * 局部参数按照name覆盖局部参数 |
| 159 | + * |
| 160 | + * @param globalRequestParameters |
| 161 | + * @param groupRequestParameters |
| 162 | + * @return |
| 163 | + */ |
| 164 | + private List<RequestParameter> assemblyRequestParameters( |
| 165 | + List<SwaggerProperties.GlobalOperationParameter> globalRequestParameters, |
| 166 | + List<SwaggerProperties.GlobalOperationParameter> groupRequestParameters) { |
| 167 | + |
| 168 | + if (Objects.isNull(groupRequestParameters) || groupRequestParameters.isEmpty()) { |
| 169 | + return getRequestParameters(globalRequestParameters); |
| 170 | + } |
| 171 | + |
| 172 | + Set<String> paramNames = groupRequestParameters.stream() |
| 173 | + .map(SwaggerProperties.GlobalOperationParameter::getName).collect(Collectors.toSet()); |
| 174 | + |
| 175 | + List<SwaggerProperties.GlobalOperationParameter> requestParameters = newArrayList(); |
| 176 | + |
| 177 | + if (Objects.nonNull(globalRequestParameters)) { |
| 178 | + for (SwaggerProperties.GlobalOperationParameter parameter : globalRequestParameters) { |
| 179 | + if (!paramNames.contains(parameter.getName())) { |
| 180 | + requestParameters.add(parameter); |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + requestParameters.addAll(groupRequestParameters); |
| 186 | + return getRequestParameters(requestParameters); |
| 187 | + } |
| 188 | + |
149 | 189 | /** |
150 | 190 | * API接口路径选择 |
151 | 191 | * |
|
0 commit comments