22
33import com .google .common .base .Predicate ;
44import com .google .common .base .Predicates ;
5+ import com .google .common .collect .Lists ;
56import org .springframework .beans .BeansException ;
67import org .springframework .beans .factory .BeanFactory ;
78import org .springframework .beans .factory .BeanFactoryAware ;
1011import org .springframework .context .annotation .Bean ;
1112import org .springframework .context .annotation .Configuration ;
1213import springfox .documentation .builders .ApiInfoBuilder ;
14+ import springfox .documentation .builders .ParameterBuilder ;
1315import springfox .documentation .builders .PathSelectors ;
1416import springfox .documentation .builders .RequestHandlerSelectors ;
17+ import springfox .documentation .schema .ModelRef ;
1518import springfox .documentation .service .ApiInfo ;
1619import springfox .documentation .service .Contact ;
20+ import springfox .documentation .service .Parameter ;
1721import springfox .documentation .spi .DocumentationType ;
1822import springfox .documentation .spring .web .plugins .Docket ;
1923
2024import java .util .ArrayList ;
2125import java .util .LinkedList ;
2226import java .util .List ;
27+ import java .util .Set ;
28+ import java .util .stream .Collectors ;
2329
2430/**
2531 * @author 翟永超
@@ -43,7 +49,7 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
4349 ConfigurableBeanFactory configurableBeanFactory = (ConfigurableBeanFactory ) beanFactory ;
4450
4551 // 没有分组
46- if (swaggerProperties .getDocket ().size () == 0 ) {
52+ if (swaggerProperties .getDocket ().size () == 0 ) {
4753 ApiInfo apiInfo = new ApiInfoBuilder ()
4854 .title (swaggerProperties .getTitle ())
4955 .description (swaggerProperties .getDescription ())
@@ -58,23 +64,26 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
5864
5965 // base-path处理
6066 // 当没有配置任何path的时候,解析/**
61- if (swaggerProperties .getBasePath ().isEmpty ()) {
67+ if (swaggerProperties .getBasePath ().isEmpty ()) {
6268 swaggerProperties .getBasePath ().add ("/**" );
6369 }
6470 List <Predicate <String >> basePath = new ArrayList ();
65- for (String path : swaggerProperties .getBasePath ()) {
71+ for (String path : swaggerProperties .getBasePath ()) {
6672 basePath .add (PathSelectors .ant (path ));
6773 }
6874
6975 // exclude-path处理
7076 List <Predicate <String >> excludePath = new ArrayList ();
71- for (String path : swaggerProperties .getExcludePath ()) {
77+ for (String path : swaggerProperties .getExcludePath ()) {
7278 excludePath .add (PathSelectors .ant (path ));
7379 }
7480
81+
7582 Docket docket = new Docket (DocumentationType .SWAGGER_2 )
7683 .host (swaggerProperties .getHost ())
7784 .apiInfo (apiInfo )
85+ .globalOperationParameters (buildGlobalOperationParametersFromSwaggerProperties (
86+ swaggerProperties .getGlobalOperationParameters ()))
7887 .select ()
7988 .apis (RequestHandlerSelectors .basePackage (swaggerProperties .getBasePackage ()))
8089 .paths (
@@ -91,7 +100,7 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
91100
92101 // 分组创建
93102 List <Docket > docketList = new LinkedList <>();
94- for (String groupName : swaggerProperties .getDocket ().keySet ()) {
103+ for (String groupName : swaggerProperties .getDocket ().keySet ()) {
95104 SwaggerProperties .DocketInfo docketInfo = swaggerProperties .getDocket ().get (groupName );
96105
97106 ApiInfo apiInfo = new ApiInfoBuilder ()
@@ -102,33 +111,35 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
102111 .licenseUrl (docketInfo .getLicenseUrl ().isEmpty () ? swaggerProperties .getLicenseUrl () : docketInfo .getLicenseUrl ())
103112 .contact (
104113 new Contact (
105- docketInfo .getContact ().getName ().isEmpty () ? swaggerProperties .getContact ().getName () : docketInfo .getContact ().getName (),
106- docketInfo .getContact ().getUrl ().isEmpty () ? swaggerProperties .getContact ().getUrl () : docketInfo .getContact ().getUrl (),
107- docketInfo .getContact ().getEmail ().isEmpty () ? swaggerProperties .getContact ().getEmail () : docketInfo .getContact ().getEmail ()
114+ docketInfo .getContact ().getName ().isEmpty () ? swaggerProperties .getContact ().getName () : docketInfo .getContact ().getName (),
115+ docketInfo .getContact ().getUrl ().isEmpty () ? swaggerProperties .getContact ().getUrl () : docketInfo .getContact ().getUrl (),
116+ docketInfo .getContact ().getEmail ().isEmpty () ? swaggerProperties .getContact ().getEmail () : docketInfo .getContact ().getEmail ()
108117 )
109118 )
110119 .termsOfServiceUrl (docketInfo .getTermsOfServiceUrl ().isEmpty () ? swaggerProperties .getTermsOfServiceUrl () : docketInfo .getTermsOfServiceUrl ())
111120 .build ();
112121
113122 // base-path处理
114123 // 当没有配置任何path的时候,解析/**
115- if (docketInfo .getBasePath ().isEmpty ()) {
124+ if (docketInfo .getBasePath ().isEmpty ()) {
116125 docketInfo .getBasePath ().add ("/**" );
117126 }
118127 List <Predicate <String >> basePath = new ArrayList ();
119- for (String path : docketInfo .getBasePath ()) {
128+ for (String path : docketInfo .getBasePath ()) {
120129 basePath .add (PathSelectors .ant (path ));
121130 }
122131
123132 // exclude-path处理
124133 List <Predicate <String >> excludePath = new ArrayList ();
125- for (String path : docketInfo .getExcludePath ()) {
134+ for (String path : docketInfo .getExcludePath ()) {
126135 excludePath .add (PathSelectors .ant (path ));
127136 }
128137
129138 Docket docket = new Docket (DocumentationType .SWAGGER_2 )
130139 .host (swaggerProperties .getHost ())
131140 .apiInfo (apiInfo )
141+ .globalOperationParameters (assemblyGlobalOperationParameters (swaggerProperties .getGlobalOperationParameters (),
142+ docketInfo .getGlobalOperationParameters ()))
132143 .groupName (groupName )
133144 .select ()
134145 .apis (RequestHandlerSelectors .basePackage (docketInfo .getBasePackage ()))
@@ -150,4 +161,49 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
150161 public void setBeanFactory (BeanFactory beanFactory ) throws BeansException {
151162 this .beanFactory = beanFactory ;
152163 }
164+
165+ private List <Parameter > buildGlobalOperationParametersFromSwaggerProperties (
166+ List <SwaggerProperties .GlobalOperationParameter > globalOperationParameters ) {
167+ List <Parameter > parameters = Lists .newArrayList ();
168+ for (SwaggerProperties .GlobalOperationParameter globalOperationParameter : globalOperationParameters ) {
169+ parameters .add (new ParameterBuilder ()
170+ .name (globalOperationParameter .getName ())
171+ .description (globalOperationParameter .getDescription ())
172+ .modelRef (new ModelRef (globalOperationParameter .getModelRef ()))
173+ .parameterType (globalOperationParameter .getParameterType ())
174+ .required (Boolean .parseBoolean (globalOperationParameter .getRequired ()))
175+ .build ());
176+ }
177+ return parameters ;
178+ }
179+
180+ /**
181+ * 局部参数按照name覆盖局部参数
182+ *
183+ * @param globalOperationParameters
184+ * @param docketOperationParameters
185+ * @return
186+ */
187+ private List <Parameter > assemblyGlobalOperationParameters (
188+ List <SwaggerProperties .GlobalOperationParameter > globalOperationParameters ,
189+ List <SwaggerProperties .GlobalOperationParameter > docketOperationParameters ) {
190+
191+ if (docketOperationParameters == null || docketOperationParameters .isEmpty ()) {
192+ return buildGlobalOperationParametersFromSwaggerProperties (globalOperationParameters );
193+ }
194+
195+ Set <String > docketNames = docketOperationParameters .stream ()
196+ .map (SwaggerProperties .GlobalOperationParameter ::getName )
197+ .collect (Collectors .toSet ());
198+
199+ List <SwaggerProperties .GlobalOperationParameter > resultOperationParameters = Lists .newArrayList ();
200+
201+ for (SwaggerProperties .GlobalOperationParameter parameter : globalOperationParameters ) {
202+ if (!docketNames .contains (parameter .getName ())) {
203+ resultOperationParameters .add (parameter );
204+ }
205+ }
206+ resultOperationParameters .addAll (docketOperationParameters );
207+ return buildGlobalOperationParametersFromSwaggerProperties (resultOperationParameters );
208+ }
153209}
0 commit comments