Skip to content

Commit 62b657a

Browse files
committed
Merge pull request #3259 from mcclellanmj/bug-parameterized-types
Bug: Fixes an issue with Annotated Generic properties getting applied to sibling properties
1 parent 82d60c0 commit 62b657a

File tree

9 files changed

+391
-15
lines changed

9 files changed

+391
-15
lines changed

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

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
import java.io.IOException;
3030
import java.lang.annotation.Annotation;
31+
import java.lang.reflect.AnnotatedParameterizedType;
32+
import java.lang.reflect.AnnotatedType;
3133
import java.lang.reflect.Field;
3234
import java.lang.reflect.ParameterizedType;
3335
import java.lang.reflect.Type;
@@ -379,22 +381,10 @@ Schema calculateSchema(Components components, ParameterInfo parameterInfo, Reque
379381
MethodParameter methodParameter = parameterInfo.getMethodParameter();
380382

381383
if (parameterInfo.getParameterModel() == null || parameterInfo.getParameterModel().getSchema() == null) {
382-
Type type = GenericTypeResolver.resolveType(methodParameter.getGenericParameterType(), methodParameter.getContainingClass());
383384
Annotation[] paramAnnotations = getParameterAnnotations(methodParameter);
384-
Annotation[] typeAnnotations = new Annotation[0];
385-
if (KotlinDetector.isKotlinPresent()
386-
&& KotlinDetector.isKotlinReflectPresent()
387-
&& KotlinDetector.isKotlinType(methodParameter.getContainingClass())
388-
&& type == String.class) {
389-
Class<?> restored = KotlinInlineParameterResolver
390-
.resolveInlineType(methodParameter, type);
391-
if (restored != null) {
392-
type = restored;
393-
typeAnnotations = ((Class<?>) type).getAnnotations();
394-
}
395-
} else {
396-
typeAnnotations = methodParameter.getParameterType().getAnnotations();
397-
}
385+
TypeAndTypeAnnotations resolved = resolveTypeAndTypeAnnotationsForParameter(methodParameter);
386+
Type type = resolved.type();
387+
Annotation[] typeAnnotations = resolved.typeAnnotations();
398388
Annotation[] mergedAnnotations =
399389
Stream.concat(
400390
Arrays.stream(paramAnnotations),
@@ -431,6 +421,56 @@ Schema calculateSchema(Components components, ParameterInfo parameterInfo, Reque
431421
return schemaN;
432422
}
433423

424+
/**
425+
* Resolves type and type annotations for schema extraction (flattened {@code @ParameterObject} field,
426+
* Kotlin inline {@code String}, or default).
427+
*
428+
* @param methodParameter the method parameter
429+
* @return the resolved type and type annotations
430+
*/
431+
private TypeAndTypeAnnotations resolveTypeAndTypeAnnotationsForParameter(MethodParameter methodParameter) {
432+
if (methodParameter instanceof DelegatingMethodParameter delegatingMethodParameter
433+
&& delegatingMethodParameter.getField() != null) {
434+
AnnotatedType annotated = delegatingMethodParameter.getField().getAnnotatedType();
435+
Type type = GenericTypeResolver.resolveType(annotated.getType(), methodParameter.getContainingClass());
436+
return new TypeAndTypeAnnotations(type, annotationsFromAnnotatedTypeArguments(annotated));
437+
}
438+
439+
Type type = GenericTypeResolver.resolveType(methodParameter.getGenericParameterType(), methodParameter.getContainingClass());
440+
if (KotlinDetector.isKotlinPresent()
441+
&& KotlinDetector.isKotlinReflectPresent()
442+
&& KotlinDetector.isKotlinType(methodParameter.getContainingClass())
443+
&& type == String.class) {
444+
Class<?> restored = KotlinInlineParameterResolver.resolveInlineType(methodParameter, type);
445+
return restored != null
446+
? new TypeAndTypeAnnotations(restored, restored.getAnnotations())
447+
: new TypeAndTypeAnnotations(type, new Annotation[0]);
448+
}
449+
450+
return new TypeAndTypeAnnotations(type, methodParameter.getParameterType().getAnnotations());
451+
}
452+
453+
/**
454+
* Pair of resolved Java type and type annotations merged with parameter annotations for {@code extractSchema}.
455+
*/
456+
private record TypeAndTypeAnnotations(Type type, Annotation[] typeAnnotations) {
457+
}
458+
459+
/**
460+
* Collects annotations declared on each type argument of an {@link AnnotatedParameterizedType}.
461+
*
462+
* @param annotatedType the annotated type
463+
* @return a new array, possibly empty
464+
*/
465+
private static Annotation[] annotationsFromAnnotatedTypeArguments(AnnotatedType annotatedType) {
466+
if (!(annotatedType instanceof AnnotatedParameterizedType apt)) {
467+
return new Annotation[0];
468+
}
469+
return Arrays.stream(apt.getAnnotatedActualTypeArguments())
470+
.flatMap(typeArg -> Arrays.stream(typeArg.getAnnotations()))
471+
.toArray(Annotation[]::new);
472+
}
473+
434474
/**
435475
* Calculate request body schema schema.
436476
*
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2019-2026 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package test.org.springdoc.api.v30.app267;
18+
19+
import org.springdoc.core.annotations.ParameterObject;
20+
21+
import org.springframework.web.bind.annotation.GetMapping;
22+
import org.springframework.web.bind.annotation.RestController;
23+
24+
@RestController
25+
public class HelloController {
26+
27+
@GetMapping("/items")
28+
public String list(@ParameterObject PersonQueryFilter criteria) {
29+
return "ok";
30+
}
31+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2019-2026 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package test.org.springdoc.api.v30.app267;
18+
19+
import java.util.List;
20+
21+
import jakarta.validation.constraints.Pattern;
22+
23+
/**
24+
* Sample person search criteria: several {@code List} query parameters; only one uses a type-use
25+
* {@link Pattern} on the element type.
26+
*/
27+
public record PersonQueryFilter(
28+
List<String> firstNames,
29+
List<String> middleNames,
30+
List<@Pattern(regexp = "^\\d+$") String> phoneNumbers) {
31+
32+
public PersonQueryFilter {
33+
firstNames = firstNames != null ? firstNames : List.of();
34+
middleNames = middleNames != null ? middleNames : List.of();
35+
phoneNumbers = phoneNumbers != null ? phoneNumbers : List.of();
36+
}
37+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2019-2026 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package test.org.springdoc.api.v30.app267;
18+
19+
import test.org.springdoc.api.v30.AbstractSpringDocV30Test;
20+
21+
import org.springframework.boot.autoconfigure.SpringBootApplication;
22+
23+
/**
24+
* Regression: {@code @Pattern} on one {@code List} field in a {@code @ParameterObject} must not
25+
* be applied to sibling {@code List} query parameters' item schemas.
26+
*/
27+
public class SpringDocApp267Test extends AbstractSpringDocV30Test {
28+
29+
@SpringBootApplication
30+
static class SpringDocTestApp {
31+
}
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2019-2026 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package test.org.springdoc.api.v31.app267;
18+
19+
import org.springdoc.core.annotations.ParameterObject;
20+
21+
import org.springframework.web.bind.annotation.GetMapping;
22+
import org.springframework.web.bind.annotation.RestController;
23+
24+
@RestController
25+
public class HelloController {
26+
27+
@GetMapping("/items")
28+
public String list(@ParameterObject PersonQueryFilter criteria) {
29+
return "ok";
30+
}
31+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2019-2026 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package test.org.springdoc.api.v31.app267;
18+
19+
import java.util.List;
20+
21+
import jakarta.validation.constraints.Pattern;
22+
23+
/**
24+
* Sample person search criteria: several {@code List} query parameters; only one uses a type-use
25+
* {@link Pattern} on the element type.
26+
*/
27+
public record PersonQueryFilter(
28+
List<String> firstNames,
29+
List<String> middleNames,
30+
List<@Pattern(regexp = "^\\d+$") String> phoneNumbers) {
31+
32+
public PersonQueryFilter {
33+
firstNames = firstNames != null ? firstNames : List.of();
34+
middleNames = middleNames != null ? middleNames : List.of();
35+
phoneNumbers = phoneNumbers != null ? phoneNumbers : List.of();
36+
}
37+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2019-2026 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package test.org.springdoc.api.v31.app267;
18+
19+
import test.org.springdoc.api.v31.AbstractSpringDocTest;
20+
21+
import org.springframework.boot.autoconfigure.SpringBootApplication;
22+
23+
/**
24+
* Regression: {@code @Pattern} on one {@code List} field in a {@code @ParameterObject} must not
25+
* be applied to sibling {@code List} query parameters' item schemas.
26+
*/
27+
public class SpringDocApp267Test extends AbstractSpringDocTest {
28+
29+
@SpringBootApplication
30+
static class SpringDocTestApp {
31+
}
32+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"paths": {
14+
"/items": {
15+
"get": {
16+
"tags": [
17+
"hello-controller"
18+
],
19+
"operationId": "list",
20+
"parameters": [
21+
{
22+
"name": "firstNames",
23+
"in": "query",
24+
"required": false,
25+
"schema": {
26+
"type": "array",
27+
"items": {
28+
"type": "string"
29+
}
30+
}
31+
},
32+
{
33+
"name": "middleNames",
34+
"in": "query",
35+
"required": false,
36+
"schema": {
37+
"type": "array",
38+
"items": {
39+
"type": "string"
40+
}
41+
}
42+
},
43+
{
44+
"name": "phoneNumbers",
45+
"in": "query",
46+
"required": false,
47+
"schema": {
48+
"type": "array",
49+
"items": {
50+
"pattern": "^\\d+$",
51+
"type": "string"
52+
}
53+
}
54+
}
55+
],
56+
"responses": {
57+
"200": {
58+
"description": "OK",
59+
"content": {
60+
"*/*": {
61+
"schema": {
62+
"type": "string"
63+
}
64+
}
65+
}
66+
}
67+
}
68+
}
69+
}
70+
},
71+
"components": {}
72+
}

0 commit comments

Comments
 (0)