Skip to content

Commit 7de3935

Browse files
committed
changes report #1816
1 parent c0947aa commit 7de3935

File tree

5 files changed

+238
-32
lines changed

5 files changed

+238
-32
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2022 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*/
22+
23+
package org.springdoc.core.models;
24+
25+
import java.util.Objects;
26+
27+
import io.swagger.v3.oas.models.parameters.Parameter;
28+
import org.apache.commons.lang3.StringUtils;
29+
30+
/**
31+
* The type Parameter Id.
32+
* @author bnasslahsen
33+
*/
34+
public class ParameterId {
35+
36+
/**
37+
* The P name.
38+
*/
39+
private String pName;
40+
41+
/**
42+
* The Param type.
43+
*/
44+
private String paramType;
45+
46+
/**
47+
* The Ref.
48+
*/
49+
private String $ref;
50+
51+
/**
52+
* Instantiates a new Parameter id.
53+
*
54+
* @param parameter the parameter
55+
*/
56+
public ParameterId(Parameter parameter) {
57+
this.pName = parameter.getName();
58+
this.paramType = parameter.getIn();
59+
this.$ref = parameter.get$ref();
60+
}
61+
62+
/**
63+
* Instantiates a new Parameter id.
64+
*
65+
* @param parameter the parameter
66+
*/
67+
public ParameterId(io.swagger.v3.oas.annotations.Parameter parameter) {
68+
this.pName = parameter.name();
69+
this.paramType = parameter.in().toString();
70+
this.$ref = parameter.ref();
71+
}
72+
73+
/**
74+
* Gets name.
75+
*
76+
* @return the name
77+
*/
78+
public String getpName() {
79+
return pName;
80+
}
81+
82+
/**
83+
* Sets name.
84+
*
85+
* @param pName the p name
86+
*/
87+
public void setpName(String pName) {
88+
this.pName = pName;
89+
}
90+
91+
/**
92+
* Gets param type.
93+
*
94+
* @return the param type
95+
*/
96+
public String getParamType() {
97+
return paramType;
98+
}
99+
100+
/**
101+
* Sets param type.
102+
*
103+
* @param paramType the param type
104+
*/
105+
public void setParamType(String paramType) {
106+
this.paramType = paramType;
107+
}
108+
109+
/**
110+
* Get ref string.
111+
*
112+
* @return the string
113+
*/
114+
public String get$ref() {
115+
return $ref;
116+
}
117+
118+
/**
119+
* Set ref.
120+
*
121+
* @param $ref the ref
122+
*/
123+
public void set$ref(String $ref) {
124+
this.$ref = $ref;
125+
}
126+
127+
@Override
128+
public boolean equals(Object o) {
129+
if (this == o) return true;
130+
if (o == null || getClass() != o.getClass()) return false;
131+
ParameterId that = (ParameterId) o;
132+
if (this.pName == null && StringUtils.isBlank(this.paramType))
133+
return Objects.equals($ref, that.$ref);
134+
if (this.pName != null && StringUtils.isBlank(this.paramType))
135+
return Objects.equals(pName, that.pName);
136+
137+
return Objects.equals(pName, that.pName) && Objects.equals(paramType, that.paramType);
138+
139+
}
140+
141+
@Override
142+
public int hashCode() {
143+
return Objects.hash(pName, paramType, $ref);
144+
}
145+
}

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/service/AbstractRequestService.java

+25-18
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import org.springdoc.core.customizers.ParameterCustomizer;
6969
import org.springdoc.core.extractor.DelegatingMethodParameter;
7070
import org.springdoc.core.models.MethodAttributes;
71+
import org.springdoc.core.models.ParameterId;
7172
import org.springdoc.core.models.ParameterInfo;
7273
import org.springdoc.core.models.RequestBodyInfo;
7374
import org.springdoc.core.providers.JavadocProvider;
@@ -328,24 +329,24 @@ else if (!RequestMethod.GET.equals(requestMethod)) {
328329
}
329330
}
330331

331-
LinkedHashMap<String, Parameter> map = getParameterLinkedHashMap(components, methodAttributes, operationParameters, parametersDocMap);
332+
LinkedHashMap<ParameterId, Parameter> map = getParameterLinkedHashMap(components, methodAttributes, operationParameters, parametersDocMap);
332333
RequestBody requestBody = requestBodyInfo.getRequestBody();
333334
// support form-data
334335
if (defaultSupportFormData && requestBody != null
335336
&& requestBody.getContent() != null
336337
&& requestBody.getContent().containsKey(org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE)) {
337-
Iterator<Entry<String, Parameter>> it = map.entrySet().iterator();
338+
Iterator<Entry<ParameterId, Parameter>> it = map.entrySet().iterator();
338339
while (it.hasNext()) {
339-
Entry<String, Parameter> entry = it.next();
340+
Entry<ParameterId, Parameter> entry = it.next();
340341
Parameter parameter = entry.getValue();
341342
if (!ParameterIn.PATH.toString().equals(parameter.getIn())) {
342-
io.swagger.v3.oas.models.media.Schema<?> itemSchema = new io.swagger.v3.oas.models.media.Schema() ;
343-
itemSchema.setName(entry.getKey());
343+
io.swagger.v3.oas.models.media.Schema<?> itemSchema = new io.swagger.v3.oas.models.media.Schema<>();
344+
itemSchema.setName(entry.getKey().getpName());
344345
itemSchema.setDescription(parameter.getDescription());
345346
itemSchema.setDeprecated(parameter.getDeprecated());
346347
if (parameter.getExample() != null)
347348
itemSchema.setExample(parameter.getExample());
348-
requestBodyInfo.addProperties(entry.getKey(), itemSchema);
349+
requestBodyInfo.addProperties(entry.getKey().getpName(), itemSchema);
349350
it.remove();
350351
}
351352
}
@@ -363,27 +364,32 @@ else if (!RequestMethod.GET.equals(requestMethod)) {
363364
* @param parametersDocMap the parameters doc map
364365
* @return the parameter linked hash map
365366
*/
366-
private LinkedHashMap<String, Parameter> getParameterLinkedHashMap(Components components, MethodAttributes methodAttributes, List<Parameter> operationParameters, Map<String, io.swagger.v3.oas.annotations.Parameter> parametersDocMap) {
367-
LinkedHashMap<String, Parameter> map = operationParameters.stream()
367+
private LinkedHashMap<ParameterId, Parameter> getParameterLinkedHashMap(Components components, MethodAttributes methodAttributes, List<Parameter> operationParameters, Map<String, io.swagger.v3.oas.annotations.Parameter> parametersDocMap) {
368+
LinkedHashMap<ParameterId, Parameter> map = operationParameters.stream()
368369
.collect(Collectors.toMap(
369-
parameter -> parameter.getName() != null ? parameter.getName() : Integer.toString(parameter.hashCode()),
370-
parameter -> parameter,
370+
ParameterId::new,
371+
parameter -> parameter,
371372
(u, v) -> {
372373
throw new IllegalStateException(String.format("Duplicate key %s", u));
373374
},
374375
LinkedHashMap::new
375376
));
376377

377378
for (Map.Entry<String, io.swagger.v3.oas.annotations.Parameter> entry : parametersDocMap.entrySet()) {
378-
if (entry.getKey() != null && !map.containsKey(entry.getKey()) && !entry.getValue().hidden()) {
379+
ParameterId parameterId = new ParameterId(entry.getValue());
380+
if (entry.getKey() != null && !map.containsKey(parameterId) && !entry.getValue().hidden()) {
379381
//Convert
380382
Parameter parameter = parameterBuilder.buildParameterFromDoc(entry.getValue(), components,
381383
methodAttributes.getJsonViewAnnotation(), methodAttributes.getLocale());
382-
map.put(entry.getKey(), parameter);
384+
map.put(parameterId, parameter);
383385
}
384386
}
385387

386388
getHeaders(methodAttributes, map);
389+
map.forEach((parameterId, parameter) -> {
390+
if(StringUtils.isBlank(parameter.getIn()) && StringUtils.isBlank(parameter.get$ref()))
391+
parameter.setIn(ParameterIn.QUERY.toString());
392+
});
387393
return map;
388394
}
389395

@@ -395,22 +401,23 @@ private LinkedHashMap<String, Parameter> getParameterLinkedHashMap(Components co
395401
* @return the headers
396402
*/
397403
@SuppressWarnings("unchecked")
398-
public static Collection<Parameter> getHeaders(MethodAttributes methodAttributes, Map<String, Parameter> map) {
404+
public static Collection<Parameter> getHeaders(MethodAttributes methodAttributes, Map<ParameterId, Parameter> map) {
399405
for (Map.Entry<String, String> entry : methodAttributes.getHeaders().entrySet()) {
400406
StringSchema schema = new StringSchema();
401407
if (StringUtils.isNotEmpty(entry.getValue()))
402408
schema.addEnumItem(entry.getValue());
403409
Parameter parameter = new Parameter().in(ParameterIn.HEADER.toString()).name(entry.getKey()).schema(schema);
404-
if (map.containsKey(entry.getKey())) {
405-
parameter = map.get(entry.getKey());
410+
ParameterId parameterId = new ParameterId(parameter);
411+
if (map.containsKey(parameterId)) {
412+
parameter = map.get(parameterId);
406413
List existingEnum = null;
407414
if (parameter.getSchema() != null && !CollectionUtils.isEmpty(parameter.getSchema().getEnum()))
408415
existingEnum = parameter.getSchema().getEnum();
409-
if (StringUtils.isNotEmpty(entry.getValue()) && (existingEnum==null || !existingEnum.contains(entry.getValue())))
416+
if (StringUtils.isNotEmpty(entry.getValue()) && (existingEnum == null || !existingEnum.contains(entry.getValue())))
410417
parameter.getSchema().addEnumItemObject(entry.getValue());
411418
parameter.setSchema(parameter.getSchema());
412419
}
413-
map.put(entry.getKey(), parameter);
420+
map.put(parameterId, parameter);
414421
}
415422
return map.values();
416423
}
@@ -508,7 +515,7 @@ public Parameter buildParams(ParameterInfo parameterInfo, Components components,
508515
// By default
509516
if (!isRequestBodyParam(requestMethod, parameterInfo)) {
510517
parameterInfo.setRequired(!((DelegatingMethodParameter) methodParameter).isNotRequired() && !methodParameter.isOptional());
511-
parameterInfo.setParamType(QUERY_PARAM);
518+
//parameterInfo.setParamType(QUERY_PARAM);
512519
parameterInfo.setDefaultValue(null);
513520
return this.buildParam(parameterInfo, components, jsonView);
514521
}

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/service/GenericParameterService.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,12 @@ public static Parameter mergeParameter(List<Parameter> existingParamDoc, Paramet
178178
Parameter result = paramCalcul;
179179
if (paramCalcul != null && paramCalcul.getName() != null) {
180180
final String name = paramCalcul.getName();
181-
Parameter paramDoc = existingParamDoc.stream().filter(p -> name.equals(p.getName())).findAny().orElse(null);
181+
final String in = paramCalcul.getIn();
182+
Parameter paramDoc = existingParamDoc.stream().filter(p ->
183+
name.equals(p.getName())
184+
&& (StringUtils.isEmpty(in) || StringUtils.isEmpty(p.getIn()) || in.equals(p.getIn()))
185+
).findAny()
186+
.orElse(null);
182187
if (paramDoc != null) {
183188
mergeParameter(paramCalcul, paramDoc);
184189
result = paramDoc;

springdoc-openapi-starter-webmvc-api/src/test/java/test/org/springdoc/api/v30/app87/HelloController.java

+29-13
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@
22
*
33
* *
44
* * *
5+
* * * * Copyright 2019-2022 the original author or authors.
56
* * * *
6-
* * * * * Copyright 2019-2022 the original author or authors.
7-
* * * * *
8-
* * * * * Licensed under the Apache License, Version 2.0 (the "License");
9-
* * * * * you may not use this file except in compliance with the License.
10-
* * * * * You may obtain a copy of the License at
11-
* * * * *
12-
* * * * * https://www.apache.org/licenses/LICENSE-2.0
13-
* * * * *
14-
* * * * * Unless required by applicable law or agreed to in writing, software
15-
* * * * * distributed under the License is distributed on an "AS IS" BASIS,
16-
* * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17-
* * * * * See the License for the specific language governing permissions and
18-
* * * * * limitations under the License.
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
1910
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
2018
* * *
2119
* *
2220
*
@@ -27,12 +25,17 @@
2725
import java.util.UUID;
2826

2927
import io.swagger.v3.oas.annotations.Operation;
28+
import io.swagger.v3.oas.annotations.Parameter;
29+
import io.swagger.v3.oas.annotations.enums.ParameterIn;
30+
import io.swagger.v3.oas.annotations.media.Schema;
3031
import io.swagger.v3.oas.annotations.parameters.RequestBody;
3132

3233
import org.springframework.http.ResponseEntity;
3334
import org.springframework.web.bind.annotation.CookieValue;
35+
import org.springframework.web.bind.annotation.GetMapping;
3436
import org.springframework.web.bind.annotation.PathVariable;
3537
import org.springframework.web.bind.annotation.PutMapping;
38+
import org.springframework.web.bind.annotation.RequestHeader;
3639
import org.springframework.web.bind.annotation.RestController;
3740

3841

@@ -51,6 +54,19 @@ public ResponseEntity<Item> putItem(
5154
return ResponseEntity.ok(item);
5255
}
5356

57+
/**
58+
* List tracker data.
59+
*
60+
* @return the tracker data
61+
*/
62+
@GetMapping(value = "/values/data")
63+
void list(@RequestHeader(value = "access_token", required = false)
64+
@Parameter(name = "access_token", in = ParameterIn.HEADER, description = "token in header", schema = @Schema(implementation = String.class))
65+
String tokenInHeader,@CookieValue(value = "access_token", required = false)
66+
@Parameter(name = "access_token", in = ParameterIn.COOKIE, description = "token in cookie", schema = @Schema(implementation = String.class))
67+
String tokenInCookie) {
68+
69+
}
5470
public static class Item {
5571
}
5672
}

springdoc-openapi-starter-webmvc-api/src/test/resources/results/3.0.1/app87.json

+33
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,39 @@
5858
}
5959
}
6060
}
61+
},
62+
"/values/data": {
63+
"get": {
64+
"tags": [
65+
"hello-controller"
66+
],
67+
"operationId": "list",
68+
"parameters": [
69+
{
70+
"name": "access_token",
71+
"in": "header",
72+
"description": "token in header",
73+
"required": false,
74+
"schema": {
75+
"type": "string"
76+
}
77+
},
78+
{
79+
"name": "access_token",
80+
"in": "cookie",
81+
"description": "token in cookie",
82+
"required": false,
83+
"schema": {
84+
"type": "string"
85+
}
86+
}
87+
],
88+
"responses": {
89+
"200": {
90+
"description": "OK"
91+
}
92+
}
93+
}
6194
}
6295
},
6396
"components": {

0 commit comments

Comments
 (0)