Skip to content

Commit d6e216e

Browse files
author
bnasslahsen
committed
@ParameterObject causes NPE when combined with spring-data-rest. Fixes #876
1 parent 247022b commit d6e216e

File tree

5 files changed

+224
-1
lines changed

5 files changed

+224
-1
lines changed

springdoc-openapi-data-rest/src/main/java/org/springdoc/data/rest/customisers/DataRestDelegatingMethodParameterCustomizer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public DataRestDelegatingMethodParameterCustomizer(Optional<SpringDataWebPropert
6262
@Override
6363
public void customize(MethodParameter originalParameter, MethodParameter methodParameter) {
6464
PageableDefault pageableDefault = originalParameter.getParameterAnnotation(PageableDefault.class);
65-
if (pageableDefault != null || optionalSpringDataWebProperties.isPresent() || optionalRepositoryRestConfiguration.isPresent()) {
65+
if (pageableDefault != null || (org.springframework.data.domain.Pageable.class.isAssignableFrom(originalParameter.getParameterType()) && (optionalSpringDataWebProperties.isPresent() || optionalRepositoryRestConfiguration.isPresent())))
66+
{
6667
Field field = FieldUtils.getDeclaredField(DelegatingMethodParameter.class, "additionalParameterAnnotations", true);
6768
try {
6869
Annotation[] parameterAnnotations = (Annotation[]) field.get(methodParameter);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package test.org.springdoc.api.app18;
2+
3+
import javax.servlet.http.HttpServletRequest;
4+
import javax.validation.Valid;
5+
6+
import io.swagger.v3.oas.annotations.Operation;
7+
import org.springdoc.api.annotations.ParameterObject;
8+
9+
import org.springframework.http.MediaType;
10+
import org.springframework.validation.annotation.Validated;
11+
import org.springframework.web.bind.annotation.GetMapping;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.RestController;
14+
15+
@Validated
16+
@RestController
17+
@RequestMapping(value = {HelloController.VERSION + "/helloWorld", "latest/helloWorld"}, produces = MediaType.APPLICATION_JSON_VALUE)
18+
public class HelloController {
19+
20+
public static final String VERSION = "v1";
21+
22+
@Operation(summary = "Example endpoint")
23+
@GetMapping("/helloWorld")
24+
public HelloWorldModel helloWorld(@Valid @ParameterObject HelloWorldModel helloWorldModel, HttpServletRequest request) {
25+
return new HelloWorldModel();
26+
}
27+
28+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package test.org.springdoc.api.app18;
2+
3+
import java.time.LocalDate;
4+
5+
import javax.validation.constraints.NotBlank;
6+
import javax.validation.constraints.NotNull;
7+
8+
import io.swagger.v3.oas.annotations.Parameter;
9+
10+
import org.springframework.format.annotation.DateTimeFormat;
11+
12+
public class HelloWorldModel {
13+
14+
@Parameter(description = "Description for abc", example = "def")
15+
@NotBlank
16+
private String abc;
17+
18+
@Parameter(description = "Description of this date", example = "2020-10-25")
19+
@NotNull
20+
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
21+
private LocalDate thisDate;
22+
23+
public String getAbc() {
24+
return abc;
25+
}
26+
27+
public void setAbc(String abc) {
28+
this.abc = abc;
29+
}
30+
31+
public LocalDate getThisDate() {
32+
return thisDate;
33+
}
34+
35+
public void setThisDate(LocalDate thisDate) {
36+
this.thisDate = thisDate;
37+
}
38+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2020 the original author or authors.
6+
* * * *
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
10+
* * * *
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.
18+
* * *
19+
* *
20+
*
21+
*/
22+
23+
package test.org.springdoc.api.app18;
24+
25+
import test.org.springdoc.api.AbstractSpringDocTest;
26+
27+
import org.springframework.boot.autoconfigure.SpringBootApplication;
28+
29+
public class SpringDocApp18Test extends AbstractSpringDocTest {
30+
31+
@SpringBootApplication
32+
static class SpringDocTestApp {}
33+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
"/v1/helloWorld/helloWorld": {
15+
"get": {
16+
"tags": [
17+
"hello-controller"
18+
],
19+
"summary": "Example endpoint",
20+
"operationId": "helloWorld",
21+
"parameters": [
22+
{
23+
"name": "abc",
24+
"in": "query",
25+
"description": "Description for abc",
26+
"required": true,
27+
"schema": {
28+
"type": "string"
29+
},
30+
"example": "def"
31+
},
32+
{
33+
"name": "thisDate",
34+
"in": "query",
35+
"description": "Description of this date",
36+
"required": true,
37+
"schema": {
38+
"type": "string",
39+
"format": "date"
40+
},
41+
"example": "2020-10-25"
42+
}
43+
],
44+
"responses": {
45+
"200": {
46+
"description": "OK",
47+
"content": {
48+
"application/json": {
49+
"schema": {
50+
"$ref": "#/components/schemas/HelloWorldModel"
51+
}
52+
}
53+
}
54+
}
55+
}
56+
}
57+
},
58+
"/latest/helloWorld/helloWorld": {
59+
"get": {
60+
"tags": [
61+
"hello-controller"
62+
],
63+
"summary": "Example endpoint",
64+
"operationId": "helloWorld_1",
65+
"parameters": [
66+
{
67+
"name": "abc",
68+
"in": "query",
69+
"description": "Description for abc",
70+
"required": true,
71+
"schema": {
72+
"type": "string"
73+
},
74+
"example": "def"
75+
},
76+
{
77+
"name": "thisDate",
78+
"in": "query",
79+
"description": "Description of this date",
80+
"required": true,
81+
"schema": {
82+
"type": "string",
83+
"format": "date"
84+
},
85+
"example": "2020-10-25"
86+
}
87+
],
88+
"responses": {
89+
"200": {
90+
"description": "OK",
91+
"content": {
92+
"application/json": {
93+
"schema": {
94+
"$ref": "#/components/schemas/HelloWorldModel"
95+
}
96+
}
97+
}
98+
}
99+
}
100+
}
101+
}
102+
},
103+
"components": {
104+
"schemas": {
105+
"HelloWorldModel": {
106+
"required": [
107+
"abc",
108+
"thisDate"
109+
],
110+
"type": "object",
111+
"properties": {
112+
"abc": {
113+
"type": "string"
114+
},
115+
"thisDate": {
116+
"type": "string",
117+
"format": "date"
118+
}
119+
}
120+
}
121+
}
122+
}
123+
}

0 commit comments

Comments
 (0)