Skip to content

Commit fcfc66e

Browse files
committed
Path variables parameters are not assigned correctly to endpoints. Fixes #2031
1 parent 039fb60 commit fcfc66e

File tree

37 files changed

+161
-141
lines changed

37 files changed

+161
-141
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/api/AbstractOpenApiResource.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.Collection;
3636
import java.util.Collections;
3737
import java.util.HashSet;
38+
import java.util.Iterator;
3839
import java.util.LinkedHashMap;
3940
import java.util.List;
4041
import java.util.Locale;
@@ -1103,6 +1104,17 @@ private void fillRouterOperation(RouterFunctionData routerFunctionData, RouterOp
11031104
private PathItem buildPathItem(RequestMethod requestMethod, Operation operation, String operationPath,
11041105
Paths paths) {
11051106
PathItem pathItemObject;
1107+
if(operation!=null && !CollectionUtils.isEmpty(operation.getParameters())){
1108+
Iterator<Parameter> paramIt = operation.getParameters().iterator();
1109+
while (paramIt.hasNext()){
1110+
Parameter parameter = paramIt.next();
1111+
if(ParameterIn.PATH.toString().equals(parameter.getIn())){
1112+
// check it's present in the path
1113+
if(!operationPath.contains("{" + parameter.getName() + "}"))
1114+
paramIt.remove();
1115+
}
1116+
}
1117+
}
11061118
if (paths.containsKey(operationPath))
11071119
pathItemObject = paths.get(operationPath);
11081120
else

springdoc-openapi-common/src/test/java/org/springdoc/api/AbstractOpenApiResourceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,11 @@ void calculatePathFromRouterOperation() {
163163
final RouterOperation routerOperation = new RouterOperation();
164164
routerOperation.setMethods(new RequestMethod[] { GET });
165165
routerOperation.setOperationModel(operation);
166-
routerOperation.setPath(PATH);
166+
routerOperation.setPath(PATH+"/{"+PARAMETER_WITH_NUMBER_SCHEMA_NAME+"}");
167167

168168
resource.calculatePath(routerOperation, Locale.getDefault(), this.openAPI);
169169

170-
final List<Parameter> parameters = resource.getOpenApi(Locale.getDefault()).getPaths().get(PATH).getGet().getParameters();
170+
final List<Parameter> parameters = resource.getOpenApi(Locale.getDefault()).getPaths().get(PATH+"/{"+PARAMETER_WITH_NUMBER_SCHEMA_NAME+"}").getGet().getParameters();
171171
assertThat(parameters.size(), is(3));
172172
assertThat(parameters, containsInAnyOrder(refParameter, numberParameterInPath, parameterWithoutSchema));
173173

springdoc-openapi-javadoc/src/test/java/test/org/springdoc/api/app120/MetaAnnotationController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ String simpleTest(@PathVariable @AccountId String accountId) {
6464
* @param accountId the account id
6565
* @return the string
6666
*/
67-
@GetMapping(value = "/testTopLevelParamAnnotationOverrides/{accountId}")
67+
@GetMapping(value = "/testTopLevelParamAnnotationOverrides/{id}")
6868
String testTopLevelParamAnnotationOverrides(@PathVariable @AccountId @Parameter(name = "id") String accountId) {
6969
return accountId;
7070
}

springdoc-openapi-javadoc/src/test/java/test/org/springdoc/api/app25/HelloController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void check() {
5454
* @param startDate the start date
5555
* @param endDate the end date
5656
*/
57-
@GetMapping(value = "/list")
57+
@GetMapping(value = "/list/{trackerId}")
5858
void list(
5959

6060
@Parameter(name = "trackerId", in = ParameterIn.PATH, required = true, schema = @Schema(type = "string", example = "the-tracker-id")) @PathVariable String trackerId,
@@ -69,7 +69,7 @@ void list(
6969
* @param startDate the start date
7070
* @param endDate the end date
7171
*/
72-
@GetMapping(value = "/secondlist")
72+
@GetMapping(value = "/secondlist/{trackerId}")
7373
void secondlist(
7474
@Parameter(name = "trackerId", in = ParameterIn.PATH, required = true, schema = @Schema(type = "string", example = "the-tracker-id")) @PathVariable String trackerId,
7575
@Parameter(name = "start", in = ParameterIn.QUERY, required = false, schema = @Schema(type = "string", format = "date-time", required = false, example = "1970-01-01T00:00:00.000Z")) @RequestParam(value = "start", required = false) Instant startDate,

springdoc-openapi-javadoc/src/test/java/test/org/springdoc/api/app42/HelloController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class HelloController {
3434
*
3535
* @param id the id
3636
*/
37-
@GetMapping(value = "/tweets")
37+
@GetMapping(value = "/tweets/{id}")
3838
public void tweets(@PathVariable TweetId id) {
3939

4040
}

springdoc-openapi-javadoc/src/test/java/test/org/springdoc/api/app51/HelloController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public String test3(
8787
* @param params the params
8888
* @return the string
8989
*/
90-
@GetMapping("/test")
90+
@GetMapping("/test/{path}")
9191
public String get(
9292
@PathVariable String path,
9393
@RequestParam(required = false) Map<String, String> params) {

springdoc-openapi-javadoc/src/test/java/test/org/springdoc/api/app73/HelloController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class HelloController {
4545
*/
4646
@DeleteMapping("/{id}")
4747
@ResponseStatus(HttpStatus.NO_CONTENT)
48-
public void delete(@Parameter(name = "country_code", in = ParameterIn.PATH) String countryCode, @PathVariable("id") String id) {
48+
public void delete(@Parameter(name = "country_code", in = ParameterIn.QUERY) String countryCode, @PathVariable("id") String id) {
4949

5050
}
5151

@@ -57,7 +57,7 @@ public void delete(@Parameter(name = "country_code", in = ParameterIn.PATH) Stri
5757
* @return the string
5858
*/
5959
@GetMapping("/{id}")
60-
public String get(@Parameter(name = "country_code", in = ParameterIn.PATH) String countryCode, @PathVariable("id") String id) {
60+
public String get(@Parameter(name = "country_code", in = ParameterIn.QUERY) String countryCode, @PathVariable("id") String id) {
6161
return null;
6262
}
6363
}

springdoc-openapi-javadoc/src/test/java/test/org/springdoc/api/app75/HelloController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class HelloController {
4040
*
4141
* @return the string
4242
*/
43-
@PostMapping("/test1")
43+
@PostMapping("/test1/{uuid}")
4444
@Operation(summary = "Example api that realize an ECHO operation",
4545
description = "The result of the echo is the input value of the api",
4646
parameters = { @Parameter(in = ParameterIn.PATH,
@@ -69,7 +69,7 @@ public String postMyRequestBody1() {
6969
*
7070
* @return the string
7171
*/
72-
@PostMapping("/test2")
72+
@PostMapping("/test2/{uuid}")
7373
@Operation(summary = "Example api that realize an ECHO operation",
7474
description = "The result of the echo is the input value of the api",
7575
responses = {
@@ -98,7 +98,7 @@ public String postMyRequestBody2() {
9898
*
9999
* @return the string
100100
*/
101-
@PostMapping("/test3")
101+
@PostMapping("/test3/{uuid}")
102102
@Operation(summary = "Example api that realize an ECHO operation",
103103
description = "The result of the echo is the input value of the api",
104104
parameters = { @Parameter(in = ParameterIn.PATH,

springdoc-openapi-javadoc/src/test/java/test/org/springdoc/api/app89/HelloController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class HelloController {
3939
* @return the address
4040
*/
4141
@Operation(summary = "Get Status")
42-
@GetMapping(value = "/status", produces = MediaType.TEXT_HTML_VALUE)
42+
@GetMapping(value = "/status/{id}", produces = MediaType.TEXT_HTML_VALUE)
4343
public ModelAndView getAddress(@PathVariable String id) {
4444
return null;
4545
}

springdoc-openapi-javadoc/src/test/resources/results/app120.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
}
1818
],
1919
"paths": {
20-
"/testTopLevelParamAnnotationOverrides/{accountId}": {
20+
"/testTopLevelParamAnnotationOverrides/{id}": {
2121
"get": {
2222
"tags": [
2323
"meta-annotation-controller"

0 commit comments

Comments
 (0)