Skip to content

Commit 9150ee4

Browse files
committed
When @get, using @parameter over the method results in duplicate of the same parameter. Fixes #1901
1 parent 30c387f commit 9150ee4

File tree

7 files changed

+274
-165
lines changed

7 files changed

+274
-165
lines changed

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

+91-155
Large diffs are not rendered by default.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public static Parameter mergeParameter(List<Parameter> existingParamDoc, Paramet
191191
* @param paramCalcul the param calcul
192192
* @param paramDoc the param doc
193193
*/
194-
private static void mergeParameter(Parameter paramCalcul, Parameter paramDoc) {
194+
public static void mergeParameter(Parameter paramCalcul, Parameter paramDoc) {
195195
if (StringUtils.isBlank(paramDoc.getDescription()))
196196
paramDoc.setDescription(paramCalcul.getDescription());
197197

springdoc-openapi-common/src/main/java/org/springdoc/core/ParameterId.java

+13-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import java.util.Objects;
2626

27-
import io.swagger.v3.oas.annotations.enums.ParameterIn;
2827
import io.swagger.v3.oas.models.parameters.Parameter;
2928
import org.apache.commons.lang3.StringUtils;
3029

@@ -67,8 +66,19 @@ public ParameterId(Parameter parameter) {
6766
*/
6867
public ParameterId(io.swagger.v3.oas.annotations.Parameter parameter) {
6968
this.pName = parameter.name();
70-
this.paramType = parameter.in().toString();
71-
this.$ref = parameter.ref();
69+
this.paramType = StringUtils.isNotBlank(parameter.in().toString()) ? parameter.in().toString():null;
70+
this.$ref = StringUtils.isNotBlank(parameter.ref()) ? parameter.ref():null;
71+
}
72+
73+
/**
74+
* Instantiates a new Parameter id.
75+
*
76+
* @param pName the p name
77+
* @param paramType the param type
78+
*/
79+
public ParameterId(String pName, String paramType) {
80+
this.pName = pName;
81+
this.paramType =paramType;
7282
}
7383

7484
/**
@@ -136,7 +146,6 @@ public boolean equals(Object o) {
136146
return Objects.equals(pName, that.pName);
137147

138148
return Objects.equals(pName, that.pName) && Objects.equals(paramType, that.paramType);
139-
140149
}
141150

142151
@Override

springdoc-openapi-common/src/main/java/org/springdoc/core/ParameterInfo.java

+35-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
package org.springdoc.core;
2424

25+
import io.swagger.v3.oas.annotations.Parameter;
2526
import io.swagger.v3.oas.annotations.enums.ParameterIn;
2627
import org.apache.commons.lang3.StringUtils;
2728
import org.slf4j.Logger;
@@ -76,6 +77,11 @@ public class ParameterInfo {
7677
*/
7778
private boolean requestPart;
7879

80+
/**
81+
* The Parameter id.
82+
*/
83+
private ParameterId parameterId;
84+
7985
/**
8086
* The constant LOGGER.
8187
*/
@@ -86,8 +92,9 @@ public class ParameterInfo {
8692
* @param pName the parameter name
8793
* @param methodParameter the method parameter
8894
* @param genericParameterService the parameter builder
95+
* @param parameterAnnotation the parameter annotation
8996
*/
90-
public ParameterInfo(String pName, MethodParameter methodParameter, GenericParameterService genericParameterService) {
97+
public ParameterInfo(String pName, MethodParameter methodParameter, GenericParameterService genericParameterService, Parameter parameterAnnotation) {
9198
RequestHeader requestHeader = methodParameter.getParameterAnnotation(RequestHeader.class);
9299
RequestParam requestParam = methodParameter.getParameterAnnotation(RequestParam.class);
93100
PathVariable pathVar = methodParameter.getParameterAnnotation(PathVariable.class);
@@ -123,6 +130,15 @@ else if (cookieValue != null)
123130
}
124131

125132
this.required = this.required && !methodParameter.isOptional();
133+
if (parameterAnnotation != null){
134+
this.parameterId = new ParameterId(parameterAnnotation);
135+
if(StringUtils.isBlank(parameterId.getpName()))
136+
this.parameterId.setpName(this.pName);
137+
if(StringUtils.isBlank(parameterId.getParamType()))
138+
this.parameterId.setParamType(this.paramType);
139+
}
140+
else
141+
this.parameterId = new ParameterId(this.pName, paramType);
126142
}
127143

128144
/**
@@ -294,4 +310,22 @@ public boolean isRequestPart() {
294310
public void setRequestPart(boolean requestPart) {
295311
this.requestPart = requestPart;
296312
}
313+
314+
/**
315+
* Gets parameter id.
316+
*
317+
* @return the parameter id
318+
*/
319+
public ParameterId getParameterId() {
320+
return parameterId;
321+
}
322+
323+
/**
324+
* Sets parameter id.
325+
*
326+
* @param parameterId the parameter id
327+
*/
328+
public void setParameterId(ParameterId parameterId) {
329+
this.parameterId = parameterId;
330+
}
297331
}

springdoc-openapi-data-rest/src/main/java/org/springdoc/data/rest/core/DataRestRequestService.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ public void buildCommonParameters(OpenAPI openAPI, RequestMethod requestMethod,
153153
Class<?> domainType = dataRestRepository.getDomainType();
154154
for (MethodParameter methodParameter : parameters) {
155155
final String pName = methodParameter.getParameterName();
156-
ParameterInfo parameterInfo = new ParameterInfo(pName, methodParameter, parameterBuilder);
156+
io.swagger.v3.oas.annotations.Parameter parameterDoc = AnnotatedElementUtils.findMergedAnnotation(
157+
AnnotatedElementUtils.forAnnotations(methodParameter.getParameterAnnotations()),
158+
io.swagger.v3.oas.annotations.Parameter.class);
159+
ParameterInfo parameterInfo = new ParameterInfo(pName, methodParameter, parameterBuilder,parameterDoc);
157160
if (isParamToIgnore(methodParameter)) {
158161
if (PersistentEntityResource.class.equals(methodParameter.getParameterType())) {
159162
Schema<?> schema = SpringDocAnnotationsUtils.resolveSchemaFromType(domainType, openAPI.getComponents(), null, methodParameter.getParameterAnnotations());
@@ -163,9 +166,6 @@ else if (methodParameter.getParameterAnnotation(BackendId.class) != null) {
163166
parameterInfo.setParameterModel(new Parameter().name("id").in(ParameterIn.PATH.toString()).schema(new StringSchema()));
164167
}
165168
Parameter parameter = null;
166-
io.swagger.v3.oas.annotations.Parameter parameterDoc = AnnotatedElementUtils.findMergedAnnotation(
167-
AnnotatedElementUtils.forAnnotations(methodParameter.getParameterAnnotations()),
168-
io.swagger.v3.oas.annotations.Parameter.class);
169169
if (parameterDoc != null) {
170170
if (parameterDoc.hidden() || parameterDoc.schema().hidden())
171171
continue;

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

+25
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.web.bind.annotation.PathVariable;
3737
import org.springframework.web.bind.annotation.PutMapping;
3838
import org.springframework.web.bind.annotation.RequestHeader;
39+
import org.springframework.web.bind.annotation.RequestParam;
3940
import org.springframework.web.bind.annotation.RestController;
4041

4142

@@ -69,4 +70,28 @@ void list(@RequestHeader(value = "access_token", required = false)
6970
}
7071
public static class Item {
7172
}
73+
74+
@GetMapping("/duplicate_param")
75+
@Operation(summary = "Duplicate param")
76+
@Parameter(name = "sample", required = true, in = ParameterIn.HEADER, description = "sample Header")
77+
@Parameter(name = "sample", required = true, in = ParameterIn.QUERY, description = "sample query")
78+
public String duplicateParam(@RequestParam String sample, @RequestHeader("sample") String sampleHeader) {
79+
return "duplicateParam";
80+
}
81+
82+
@GetMapping("/duplicate_param2")
83+
@Operation(summary = "Duplicate param")
84+
@Parameter(name = "sample", required = true, description = "sample")
85+
public String duplicateParam2(@RequestParam String sample) {
86+
return "duplicateParam";
87+
}
88+
89+
90+
@GetMapping("/duplicate_param3")
91+
@Operation(summary = "Duplicate param")
92+
@Parameter(name = "sample", required = true, description = "sample")
93+
public String duplicateParam3(@RequestHeader String sample) {
94+
return "duplicateParam";
95+
}
96+
7297
}

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

+105
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,111 @@
9191
}
9292
}
9393
}
94+
},
95+
"/duplicate_param": {
96+
"get": {
97+
"tags": [
98+
"hello-controller"
99+
],
100+
"summary": "Duplicate param",
101+
"operationId": "duplicateParam",
102+
"parameters": [
103+
{
104+
"name": "sample",
105+
"in": "query",
106+
"description": "sample query",
107+
"required": true,
108+
"schema": {
109+
"type": "string"
110+
}
111+
},
112+
{
113+
"name": "sample",
114+
"in": "header",
115+
"description": "sample Header",
116+
"required": true,
117+
"schema": {
118+
"type": "string"
119+
}
120+
}
121+
],
122+
"responses": {
123+
"200": {
124+
"description": "OK",
125+
"content": {
126+
"*/*": {
127+
"schema": {
128+
"type": "string"
129+
}
130+
}
131+
}
132+
}
133+
}
134+
}
135+
},
136+
"/duplicate_param3": {
137+
"get": {
138+
"tags": [
139+
"hello-controller"
140+
],
141+
"summary": "Duplicate param",
142+
"operationId": "duplicateParam3",
143+
"parameters": [
144+
{
145+
"name": "sample",
146+
"in": "header",
147+
"description": "sample",
148+
"required": true,
149+
"schema": {
150+
"type": "string"
151+
}
152+
}
153+
],
154+
"responses": {
155+
"200": {
156+
"description": "OK",
157+
"content": {
158+
"*/*": {
159+
"schema": {
160+
"type": "string"
161+
}
162+
}
163+
}
164+
}
165+
}
166+
}
167+
},
168+
"/duplicate_param2": {
169+
"get": {
170+
"tags": [
171+
"hello-controller"
172+
],
173+
"summary": "Duplicate param",
174+
"operationId": "duplicateParam2",
175+
"parameters": [
176+
{
177+
"name": "sample",
178+
"in": "query",
179+
"description": "sample",
180+
"required": true,
181+
"schema": {
182+
"type": "string"
183+
}
184+
}
185+
],
186+
"responses": {
187+
"200": {
188+
"description": "OK",
189+
"content": {
190+
"*/*": {
191+
"schema": {
192+
"type": "string"
193+
}
194+
}
195+
}
196+
}
197+
}
198+
}
94199
}
95200
},
96201
"components": {

0 commit comments

Comments
 (0)