Skip to content

Commit 4f9da58

Browse files
committed
Path variables parameters are not assigned correctly to endpoints. Fixes #2031
1 parent e1bd75b commit 4f9da58

File tree

38 files changed

+248
-236
lines changed

38 files changed

+248
-236
lines changed

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

+12
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.Collection;
3838
import java.util.Collections;
3939
import java.util.HashSet;
40+
import java.util.Iterator;
4041
import java.util.LinkedHashMap;
4142
import java.util.List;
4243
import java.util.Locale;
@@ -1099,6 +1100,17 @@ private void fillRouterOperation(RouterFunctionData routerFunctionData, RouterOp
10991100
private PathItem buildPathItem(RequestMethod requestMethod, Operation operation, String operationPath,
11001101
Paths paths) {
11011102
PathItem pathItemObject;
1103+
if(operation!=null && !CollectionUtils.isEmpty(operation.getParameters())){
1104+
Iterator<Parameter> paramIt = operation.getParameters().iterator();
1105+
while (paramIt.hasNext()){
1106+
Parameter parameter = paramIt.next();
1107+
if(ParameterIn.PATH.toString().equals(parameter.getIn())){
1108+
// check it's present in the path
1109+
if(!operationPath.contains("{" + parameter.getName() + "}"))
1110+
paramIt.remove();
1111+
}
1112+
}
1113+
}
11021114
if (paths.containsKey(operationPath))
11031115
pathItemObject = paths.get(operationPath);
11041116
else

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ void calculatePathFromRouterOperation() {
166166
final RouterOperation routerOperation = new RouterOperation();
167167
routerOperation.setMethods(new RequestMethod[] { GET });
168168
routerOperation.setOperationModel(operation);
169-
routerOperation.setPath(PATH);
169+
routerOperation.setPath(PATH+"/{"+PARAMETER_WITH_NUMBER_SCHEMA_NAME+"}");
170170

171171
resource.calculatePath(routerOperation, Locale.getDefault(), this.openAPI);
172172

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

springdoc-openapi-starter-webflux-api/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
<artifactId>spring-boot-starter-reactor-netty</artifactId>
2929
<scope>test</scope>
3030
</dependency>
31+
<dependency>
32+
<groupId>io.netty</groupId>
33+
<artifactId>netty-resolver-dns-native-macos</artifactId>
34+
<classifier>osx-aarch_64</classifier>
35+
<scope>test</scope>
36+
</dependency>
3137
</dependencies>
3238
<build>
3339
<plugins>

springdoc-openapi-starter-webflux-api/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-starter-webflux-api/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-starter-webflux-api/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-starter-webflux-api/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-starter-webmvc-api/src/test/java/test/org/springdoc/api/v30/app120/MetaAnnotationController.java

+12-14
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
*
@@ -59,7 +57,7 @@ String simpleTest(@PathVariable @AccountId String accountId) {
5957
* When there is a top level {@code @Parameter} annotation it has precedence over the meta-annotation
6058
* So the id parameter should have all the defaults, with a name of "id"
6159
*/
62-
@GetMapping(value = "/testTopLevelParamAnnotationOverrides/{accountId}")
60+
@GetMapping(value = "/testTopLevelParamAnnotationOverrides/{id}")
6361
String testTopLevelParamAnnotationOverrides(@PathVariable @AccountId @Parameter(name = "id") String accountId) {
6462
return accountId;
6563
}

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

+12-14
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
*
@@ -55,7 +53,7 @@ void list(
5553
@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) {
5654
}
5755

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

+12-14
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
*
@@ -32,7 +30,7 @@
3230
@RestController("/api")
3331
public class HelloController {
3432

35-
@GetMapping(value = "/tweets")
33+
@GetMapping(value = "/tweets/{id}")
3634
public void tweets(@PathVariable TweetId id) {
3735

3836
}

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

+12-14
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
*
@@ -66,7 +64,7 @@ public String test3(
6664
return "test";
6765
}
6866

69-
@GetMapping("/test")
67+
@GetMapping("/test/{path}")
7068
public String get(
7169
@PathVariable String path,
7270
@RequestParam(required = false) Map<String, String> params) {

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

+13-15
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
*
@@ -42,12 +40,12 @@ public class HelloController {
4240

4341
@DeleteMapping("/{id}")
4442
@ResponseStatus(HttpStatus.NO_CONTENT)
45-
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) {
4644

4745
}
4846

4947
@GetMapping("/{id}")
50-
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) {
5149
return null;
5250
}
5351
}

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

+14-16
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
*
@@ -38,7 +36,7 @@
3836
@RestController
3937
public class HelloController {
4038

41-
@PostMapping("/test1")
39+
@PostMapping("/test1/{uuid}")
4240
@Operation(summary = "Example api that realize an ECHO operation",
4341
description = "The result of the echo is the input value of the api",
4442
parameters = { @Parameter(in = ParameterIn.PATH,
@@ -62,7 +60,7 @@ public String postMyRequestBody1() {
6260
return null;
6361
}
6462

65-
@PostMapping("/test2")
63+
@PostMapping("/test2/{uuid}")
6664
@Operation(summary = "Example api that realize an ECHO operation",
6765
description = "The result of the echo is the input value of the api",
6866
responses = {
@@ -86,7 +84,7 @@ public String postMyRequestBody2() {
8684
return null;
8785
}
8886

89-
@PostMapping("/test3")
87+
@PostMapping("/test3/{uuid}")
9088
@Operation(summary = "Example api that realize an ECHO operation",
9189
description = "The result of the echo is the input value of the api",
9290
parameters = { @Parameter(in = ParameterIn.PATH,

0 commit comments

Comments
 (0)