Repository: springdoc/springdoc-openapi
Pull request #1491 (2022-02-04 16:48:04)

Set containingClass at MethodParameter
Fix spring doc failing to determine generic type due to lack of containing class information.

Related to https://github.com/jhipster/generator-jhipster/issues/17776.


Patch:
diff --git a/springdoc-openapi-common/src/main/java/org/springdoc/core/MethodParameterPojoExtractor.java b/springdoc-openapi-common/src/main/java/org/springdoc/core/MethodParameterPojoExtractor.java
index 593781483..f17835d24 100644
--- a/springdoc-openapi-common/src/main/java/org/springdoc/core/MethodParameterPojoExtractor.java
+++ b/springdoc-openapi-common/src/main/java/org/springdoc/core/MethodParameterPojoExtractor.java
@@ -174,7 +174,7 @@ class MethodParameterPojoExtractor {
 					.filter(d -> d.getName().equals(field.getName()))
 					.map(PropertyDescriptor::getReadMethod)
 					.filter(Objects::nonNull)
-					.map(method -> new MethodParameter(method, -1))
+					.map(method -> new MethodParameter(method, -1).withContainingClass(paramClass))
 					.map(param -> new DelegatingMethodParameter(param, fieldNamePrefix + field.getName(), finalFieldAnnotations, true, isNotRequired));
 		}
 		catch (IntrospectionException e) {
@@ -246,4 +246,4 @@ class MethodParameterPojoExtractor {
 		SIMPLE_TYPES.removeAll(Arrays.asList(classes));
 	}
 
-}
\ No newline at end of file
+}


Test patch:
diff --git a/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app181/AbstractParameterObject.java b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app181/AbstractParameterObject.java
new file mode 100644
index 000000000..9fb7ca713
--- /dev/null
+++ b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app181/AbstractParameterObject.java
@@ -0,0 +1,27 @@
+package test.org.springdoc.api.app181;
+
+import io.swagger.v3.oas.annotations.Parameter;
+import io.swagger.v3.oas.annotations.media.Schema;
+
+public class AbstractParameterObject<T> {
+
+	int primitiveBaseField;
+
+	T genericField;
+
+	public int getPrimitiveBaseField() {
+		return primitiveBaseField;
+	}
+
+	public void setPrimitiveBaseField(int primitiveBaseField) {
+		this.primitiveBaseField = primitiveBaseField;
+	}
+
+	public T getGenericField() {
+		return genericField;
+	}
+
+	public void setGenericField(T genericField) {
+		this.genericField = genericField;
+	}
+}
diff --git a/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app181/ConcreteParameterObject.java b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app181/ConcreteParameterObject.java
new file mode 100644
index 000000000..7684c6ed9
--- /dev/null
+++ b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app181/ConcreteParameterObject.java
@@ -0,0 +1,14 @@
+package test.org.springdoc.api.app181;
+
+public class ConcreteParameterObject extends AbstractParameterObject<String> {
+
+	int primitiveConcreteField;
+
+	public int getPrimitiveConcreteField() {
+		return primitiveConcreteField;
+	}
+
+	public void setPrimitiveConcreteField(int primitiveConcreteField) {
+		this.primitiveConcreteField = primitiveConcreteField;
+	}
+}
diff --git a/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app181/HelloController.java b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app181/HelloController.java
new file mode 100644
index 000000000..c2502a6fd
--- /dev/null
+++ b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app181/HelloController.java
@@ -0,0 +1,38 @@
+/*
+ *
+ *  * Copyright 2019-2020 the original author or authors.
+ *  *
+ *  * Licensed under the Apache License, Version 2.0 (the "License");
+ *  * you may not use this file except in compliance with the License.
+ *  * You may obtain a copy of the License at
+ *  *
+ *  *      https://www.apache.org/licenses/LICENSE-2.0
+ *  *
+ *  * Unless required by applicable law or agreed to in writing, software
+ *  * distributed under the License is distributed on an "AS IS" BASIS,
+ *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  * See the License for the specific language governing permissions and
+ *  * limitations under the License.
+ *
+ */
+
+package test.org.springdoc.api.app181;
+
+import org.springdoc.api.annotations.ParameterObject;
+
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+public class HelloController {
+
+	@GetMapping( "/test1")
+	public ResponseEntity<String> sayHello( @ParameterObject final ConcreteParameterObject test) {
+		System.out.println("Field B = " + test);
+		return new ResponseEntity<String>("{\"Say\": \"Hello\"}", HttpStatus.OK);
+	}
+
+
+}
\ No newline at end of file
diff --git a/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app181/SpringDocApp181Test.java b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app181/SpringDocApp181Test.java
new file mode 100644
index 000000000..a2169de0a
--- /dev/null
+++ b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app181/SpringDocApp181Test.java
@@ -0,0 +1,34 @@
+/*
+ *
+ *  * Copyright 2019-2020 the original author or authors.
+ *  *
+ *  * Licensed under the Apache License, Version 2.0 (the "License");
+ *  * you may not use this file except in compliance with the License.
+ *  * You may obtain a copy of the License at
+ *  *
+ *  *      https://www.apache.org/licenses/LICENSE-2.0
+ *  *
+ *  * Unless required by applicable law or agreed to in writing, software
+ *  * distributed under the License is distributed on an "AS IS" BASIS,
+ *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  * See the License for the specific language governing permissions and
+ *  * limitations under the License.
+ *
+ */
+
+package test.org.springdoc.api.app181;
+
+
+import test.org.springdoc.api.AbstractSpringDocTest;
+
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+/**
+ * Tests Spring meta-annotations as method parameters
+ */
+public class SpringDocApp181Test extends AbstractSpringDocTest {
+
+	@SpringBootApplication
+	static class SpringDocTestApp {}
+
+}
diff --git a/springdoc-openapi-webmvc-core/src/test/resources/results/app181.json b/springdoc-openapi-webmvc-core/src/test/resources/results/app181.json
new file mode 100644
index 000000000..879ec0828
--- /dev/null
+++ b/springdoc-openapi-webmvc-core/src/test/resources/results/app181.json
@@ -0,0 +1,64 @@
+{
+  "openapi": "3.0.1",
+  "info": {
+    "title": "OpenAPI definition",
+    "version": "v0"
+  },
+  "servers": [
+    {
+      "url": "http://localhost",
+      "description": "Generated server url"
+    }
+  ],
+  "paths": {
+    "/test1": {
+      "get": {
+        "tags": [
+          "hello-controller"
+        ],
+        "operationId": "sayHello",
+        "parameters": [
+          {
+            "name": "primitiveConcreteField",
+            "in": "query",
+            "required": false,
+            "schema": {
+              "type": "integer",
+              "format": "int32"
+            }
+          },
+          {
+            "name": "primitiveBaseField",
+            "in": "query",
+            "required": false,
+            "schema": {
+              "type": "integer",
+              "format": "int32"
+            }
+          },
+          {
+            "name": "genericField",
+            "in": "query",
+            "required": false,
+            "schema": {
+              "type": "string"
+            }
+          }
+        ],
+        "responses": {
+          "200": {
+            "description": "OK",
+            "content": {
+              "*/*": {
+                "schema": {
+                  "type": "string"
+                }
+              }
+            }
+          }
+        }
+      }
+    }
+  },
+  "components": {}
+}
\ No newline at end of file