Skip to content

Commit 231db66

Browse files
committed
NPEs when using @ParameterObject with custom Pageable and adding descriptions to fields. Fixes springdoc#1333
1 parent a2a3c06 commit 231db66

File tree

6 files changed

+233
-8
lines changed

6 files changed

+233
-8
lines changed

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

+12-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.slf4j.Logger;
2525
import org.slf4j.LoggerFactory;
2626
import org.springdoc.core.DelegatingMethodParameter;
27-
import org.springdoc.core.converters.models.Pageable;
2827
import org.springdoc.core.customizers.DelegatingMethodParameterCustomizer;
2928

3029
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
@@ -72,8 +71,11 @@ public void customize(MethodParameter originalParameter, MethodParameter methodP
7271
Annotation[] parameterAnnotations = (Annotation[]) field.get(methodParameter);
7372
if (ArrayUtils.isNotEmpty(parameterAnnotations))
7473
for (int i = 0; i < parameterAnnotations.length; i++) {
75-
if (Parameter.class.equals(parameterAnnotations[i].annotationType()))
76-
parameterAnnotations[i] = getNewParameterAnnotationForField(methodParameter.getParameterName(), pageableDefault);
74+
if (Parameter.class.equals(parameterAnnotations[i].annotationType())){
75+
Optional<Annotation> annotationForField = getNewParameterAnnotationForField(methodParameter, pageableDefault);
76+
if(annotationForField.isPresent())
77+
parameterAnnotations[i] = annotationForField.get();
78+
}
7779
}
7880
}
7981
catch (IllegalAccessException e) {
@@ -85,15 +87,16 @@ public void customize(MethodParameter originalParameter, MethodParameter methodP
8587
/**
8688
* Gets new parameter annotation for field.
8789
*
88-
* @param parameterName the parameter name
90+
* @param methodParameter the method parameter
8991
* @param pageableDefault the pageable default
9092
* @return the new parameter annotation for field
9193
*/
92-
private Annotation getNewParameterAnnotationForField(String parameterName, PageableDefault pageableDefault) {
94+
private Optional<Annotation> getNewParameterAnnotationForField(MethodParameter methodParameter, PageableDefault pageableDefault) {
95+
String parameterName = methodParameter.getParameterName();
9396
Field field;
94-
Parameter parameterNew = null;
97+
Parameter parameterNew;
9598
try {
96-
field = Pageable.class.getDeclaredField(parameterName);
99+
field = methodParameter.getContainingClass().getDeclaredField(parameterName);
97100
Parameter parameter = field.getAnnotation(Parameter.class);
98101
parameterNew = new Parameter() {
99102
@Override
@@ -615,11 +618,12 @@ public String ref() {
615618
return parameter.ref();
616619
}
617620
};
621+
return Optional.of(parameterNew);
618622
}
619623
catch (NoSuchFieldException e) {
620624
LOGGER.warn(e.getMessage());
625+
return Optional.empty();
621626
}
622-
return parameterNew;
623627
}
624628

625629
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package test.org.springdoc.api.app28;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import io.swagger.v3.oas.annotations.Parameter;
5+
6+
import org.springframework.data.domain.Pageable;
7+
import org.springframework.data.domain.Sort;
8+
9+
public class ExamplePageable implements Pageable {
10+
11+
@Parameter(description = "Anything")
12+
@JsonProperty
13+
private int something;
14+
15+
@Override
16+
public int getPageNumber() {
17+
return 0;
18+
}
19+
20+
@Override
21+
public int getPageSize() {
22+
return 0;
23+
}
24+
25+
@Override
26+
public long getOffset() {
27+
return 0;
28+
}
29+
30+
@Override
31+
public Sort getSort() {
32+
return null;
33+
}
34+
35+
@Override
36+
public Pageable next() {
37+
return null;
38+
}
39+
40+
@Override
41+
public Pageable previousOrFirst() {
42+
return null;
43+
}
44+
45+
@Override
46+
public Pageable first() {
47+
return null;
48+
}
49+
50+
@Override
51+
public Pageable withPage(int pageNumber) {
52+
return null;
53+
}
54+
55+
@Override
56+
public boolean hasPrevious() {
57+
return false;
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package test.org.springdoc.api.app28;
2+
3+
import java.util.List;
4+
5+
import io.swagger.v3.oas.annotations.Parameter;
6+
import org.springdoc.core.converters.models.Pageable;
7+
8+
public class ExamplePageableReplacement extends Pageable {
9+
10+
@Parameter(description = "Anything")
11+
private int something;
12+
13+
/**
14+
* Instantiates a new Pageable.
15+
* @param page the page
16+
* @param size the size
17+
* @param sort the sort
18+
*/
19+
public ExamplePageableReplacement(int page, int size, List<String> sort) {
20+
super(page, size, sort);
21+
}
22+
23+
public int getSomething() {
24+
return something;
25+
}
26+
27+
public void setSomething(int something) {
28+
this.something = something;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package test.org.springdoc.api.app28;
2+
3+
import org.springdoc.api.annotations.ParameterObject;
4+
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
/**
10+
* @author bnasslahsen
11+
*/
12+
@RestController
13+
@RequestMapping("/api")
14+
public class HelloController {
15+
16+
17+
@GetMapping("/items/nested")
18+
public void showNestedItem(@ParameterObject ExamplePageable examplePageable) {
19+
}
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app28;
20+
21+
import org.springdoc.core.SpringDocUtils;
22+
import test.org.springdoc.api.AbstractSpringDocTest;
23+
24+
import org.springframework.boot.autoconfigure.SpringBootApplication;
25+
26+
public class SpringDocApp28Test extends AbstractSpringDocTest {
27+
28+
@SpringBootApplication
29+
static class SpringDocTestApp {}
30+
31+
static {
32+
SpringDocUtils.getConfig().replaceParameterObjectWithClass(ExamplePageable.class, ExamplePageableReplacement.class);
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
"/api/items/nested": {
15+
"get": {
16+
"tags": [
17+
"hello-controller"
18+
],
19+
"operationId": "showNestedItem",
20+
"parameters": [
21+
{
22+
"name": "something",
23+
"in": "query",
24+
"description": "Anything",
25+
"required": false,
26+
"schema": {
27+
"type": "integer",
28+
"format": "int32"
29+
}
30+
},
31+
{
32+
"name": "page",
33+
"in": "query",
34+
"description": "Zero-based page index (0..N)",
35+
"required": false,
36+
"schema": {
37+
"minimum": 0,
38+
"type": "integer",
39+
"default": 0
40+
}
41+
},
42+
{
43+
"name": "size",
44+
"in": "query",
45+
"description": "The size of the page to be returned",
46+
"required": false,
47+
"schema": {
48+
"minimum": 1,
49+
"type": "integer",
50+
"default": 20
51+
}
52+
},
53+
{
54+
"name": "sort",
55+
"in": "query",
56+
"description": "Sorting criteria in the format: property(,asc|desc). Default sort order is ascending. Multiple sort criteria are supported.",
57+
"required": false,
58+
"schema": {
59+
"type": "array",
60+
"items": {
61+
"type": "string"
62+
}
63+
}
64+
}
65+
],
66+
"responses": {
67+
"200": {
68+
"description": "OK"
69+
}
70+
}
71+
}
72+
}
73+
},
74+
"components": {
75+
"schemas": {}
76+
}
77+
}

0 commit comments

Comments
 (0)