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

+12
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

+2-2
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

+1-1
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

+2-2
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

+1-1
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

+1-1
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

+2-2
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

+3-3
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

+1-1
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

+1-1
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"

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
}
7676
}
7777
},
78-
"/secondlist": {
78+
"/secondlist/{trackerId}": {
7979
"get": {
8080
"tags": [
8181
"hello-controller"
@@ -124,7 +124,7 @@
124124
}
125125
}
126126
},
127-
"/list": {
127+
"/list/{trackerId}": {
128128
"get": {
129129
"tags": [
130130
"hello-controller"

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
}
1818
],
1919
"paths": {
20-
"/tweets": {
20+
"/tweets/{id}": {
2121
"get": {
2222
"tags": [
2323
"hello-controller"

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
}
5757
}
5858
},
59-
"/test": {
59+
"/test/{path}": {
6060
"get": {
6161
"tags": [
6262
"hello-controller"

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"parameters": [
2929
{
3030
"name": "country_code",
31-
"in": "path",
31+
"in": "query",
3232
"description": "the country code",
3333
"required": true,
3434
"schema": {
@@ -68,7 +68,7 @@
6868
"parameters": [
6969
{
7070
"name": "country_code",
71-
"in": "path",
71+
"in": "query",
7272
"description": "the country code",
7373
"required": true,
7474
"schema": {
@@ -103,7 +103,7 @@
103103
"parameters": [
104104
{
105105
"name": "country_code",
106-
"in": "path",
106+
"in": "query",
107107
"description": "the country code",
108108
"required": true,
109109
"schema": {
@@ -143,7 +143,7 @@
143143
"parameters": [
144144
{
145145
"name": "country_code",
146-
"in": "path",
146+
"in": "query",
147147
"description": "the country code",
148148
"required": true,
149149
"schema": {

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
}
1818
],
1919
"paths": {
20-
"/test3": {
20+
"/test3/{uuid}": {
2121
"post": {
2222
"tags": [
2323
"hello-controller"
@@ -64,7 +64,7 @@
6464
}
6565
}
6666
},
67-
"/test2": {
67+
"/test2/{uuid}": {
6868
"post": {
6969
"tags": [
7070
"hello-controller"
@@ -108,7 +108,7 @@
108108
}
109109
}
110110
},
111-
"/test1": {
111+
"/test1/{uuid}": {
112112
"post": {
113113
"tags": [
114114
"hello-controller"

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
}
1818
],
1919
"paths": {
20-
"/status": {
20+
"/status/{id}": {
2121
"get": {
2222
"tags": [
2323
"hello-controller"

springdoc-openapi-kotlin/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
<version>${project.version}</version>
3737
<scope>test</scope>
3838
</dependency>
39+
<dependency>
40+
<groupId>io.netty</groupId>
41+
<artifactId>netty-resolver-dns-native-macos</artifactId>
42+
<classifier>osx-aarch_64</classifier>
43+
<scope>test</scope>
44+
</dependency>
3945
</dependencies>
4046

4147
<build>

springdoc-openapi-webflux-core/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
<artifactId>spring-cloud-starter-function-webflux</artifactId>
3434
<scope>test</scope>
3535
</dependency>
36+
<dependency>
37+
<groupId>io.netty</groupId>
38+
<artifactId>netty-resolver-dns-native-macos</artifactId>
39+
<classifier>osx-aarch_64</classifier>
40+
<scope>test</scope>
41+
</dependency>
3642
</dependencies>
3743
<build>
3844
<plugins>

springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app70/CoffeeService.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ Mono<Coffee> getCoffeeById(@Parameter(in = ParameterIn.PATH) String id) {
2626
return repo.findById(id);
2727
}
2828

29-
Flux<CoffeeOrder> getOrdersForCoffeeById(@Parameter(in = ParameterIn.PATH) String coffeeId) {
29+
Flux<CoffeeOrder> getOrdersForCoffeeById(@Parameter(in = ParameterIn.PATH) String id) {
3030
return Flux.interval(Duration.ofSeconds(1))
3131
.onBackpressureDrop()
32-
.map(i -> new CoffeeOrder(coffeeId, Instant.now()));
32+
.map(i -> new CoffeeOrder(id, Instant.now()));
3333
}
3434
}

springdoc-openapi-webflux-core/src/test/java/test/org/springdoc/api/app83/CoffeeService.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ Mono<Coffee> getCoffeeById(@Parameter(in = ParameterIn.PATH) String id) {
2626
return repo.findById(id);
2727
}
2828

29-
Flux<CoffeeOrder> getOrdersForCoffeeById(@Parameter(in = ParameterIn.PATH) String coffeeId) {
29+
Flux<CoffeeOrder> getOrdersForCoffeeById(@Parameter(in = ParameterIn.PATH) String id) {
3030
return Flux.interval(Duration.ofSeconds(1))
3131
.onBackpressureDrop()
32-
.map(i -> new CoffeeOrder(coffeeId, Instant.now()));
32+
.map(i -> new CoffeeOrder(id, Instant.now()));
3333
}
3434
}

springdoc-openapi-webflux-core/src/test/resources/results/app70.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
"operationId": "getOrdersForCoffeeById",
7373
"parameters": [
7474
{
75-
"name": "coffeeId",
75+
"name": "id",
7676
"in": "path",
7777
"required": true,
7878
"schema": {

springdoc-openapi-webflux-core/src/test/resources/results/app83.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
"operationId": "getOrdersForCoffeeById",
7373
"parameters": [
7474
{
75-
"name": "coffeeId",
75+
"name": "id",
7676
"in": "path",
7777
"required": true,
7878
"schema": {

springdoc-openapi-webflux-ui/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
<artifactId>reactor-netty-http</artifactId>
3838
<scope>test</scope>
3939
</dependency>
40+
<dependency>
41+
<groupId>io.netty</groupId>
42+
<artifactId>netty-resolver-dns-native-macos</artifactId>
43+
<classifier>osx-aarch_64</classifier>
44+
<scope>test</scope>
45+
</dependency>
4046
</dependencies>
4147
<build>
4248
<plugins>

springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/v30/app120/MetaAnnotationController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ String simpleTest(@PathVariable @AccountId String accountId) {
5757
* When there is a top level {@code @Parameter} annotation it has precedence over the meta-annotation
5858
* So the id parameter should have all the defaults, with a name of "id"
5959
*/
60-
@GetMapping(value = "/testTopLevelParamAnnotationOverrides/{accountId}")
60+
@GetMapping(value = "/testTopLevelParamAnnotationOverrides/{id}")
6161
String testTopLevelParamAnnotationOverrides(@PathVariable @AccountId @Parameter(name = "id") String accountId) {
6262
return accountId;
6363
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void list(
5353
@Parameter(name = "end", in = ParameterIn.QUERY, required = false, schema = @Schema(type = "string", format = "date-time", required = false, example = "1970-01-01T00:10:00.000Z")) @RequestParam(value = "end", required = false) Instant endDate) {
5454
}
5555

56-
@GetMapping(value = "/secondlist")
56+
@GetMapping(value = "/secondlist/{trackerId}")
5757
void secondlist(
5858
@Parameter(name = "trackerId", in = ParameterIn.PATH, required = true, schema = @Schema(type = "string", example = "the-tracker-id")) @PathVariable String trackerId,
5959
@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-webmvc-core/src/test/java/test/org/springdoc/api/v30/app42/HelloController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
@RestController("/api")
3131
public class HelloController {
3232

33-
@GetMapping(value = "/tweets")
33+
@GetMapping(value = "/tweets/{id}")
3434
public void tweets(@PathVariable TweetId id) {
3535

3636
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public String test3(
6464
return "test";
6565
}
6666

67-
@GetMapping("/test")
67+
@GetMapping("/test/{path}")
6868
public String get(
6969
@PathVariable String path,
7070
@RequestParam(required = false) Map<String, String> params) {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ public class HelloController {
4040

4141
@DeleteMapping("/{id}")
4242
@ResponseStatus(HttpStatus.NO_CONTENT)
43-
public void delete(@Parameter(name = "country_code", in = ParameterIn.PATH) String countryCode, @PathVariable("id") String id) {
43+
public void delete(@Parameter(name = "country_code", in = ParameterIn.QUERY) String countryCode, @PathVariable("id") String id) {
4444

4545
}
4646

4747
@GetMapping("/{id}")
48-
public String get(@Parameter(name = "country_code", in = ParameterIn.PATH) String countryCode, @PathVariable("id") String id) {
48+
public String get(@Parameter(name = "country_code", in = ParameterIn.QUERY) String countryCode, @PathVariable("id") String id) {
4949
return null;
5050
}
5151
}

0 commit comments

Comments
 (0)