Skip to content

Commit c4eb575

Browse files
author
bnasslahsen
committed
Add support for spring.data.web properties.
1 parent 9cdc93c commit c4eb575

File tree

6 files changed

+300
-18
lines changed

6 files changed

+300
-18
lines changed

springdoc-openapi-data-rest/src/main/java/org/springdoc/data/rest/SpringDocDataRestConfiguration.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
4747
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
4848
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
49+
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
4950
import org.springframework.context.annotation.Bean;
5051
import org.springframework.context.annotation.Configuration;
5152
import org.springframework.context.annotation.Lazy;
@@ -88,8 +89,8 @@ public class SpringDocDataRestConfiguration {
8889
@Bean
8990
@ConditionalOnMissingBean
9091
@Lazy(false)
91-
DelegatingMethodParameterCustomizer delegatingMethodParameterCustomizer() {
92-
return new DataRestDelegatingMethodParameterCustomizer();
92+
DelegatingMethodParameterCustomizer delegatingMethodParameterCustomizer(Optional<SpringDataWebProperties> optionalSpringDataWebProperties) {
93+
return new DataRestDelegatingMethodParameterCustomizer(optionalSpringDataWebProperties);
9394
}
9495

9596
/**

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

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.lang.annotation.Annotation;
44
import java.lang.reflect.Field;
5+
import java.util.Optional;
56

67
import io.swagger.v3.oas.annotations.ExternalDocumentation;
78
import io.swagger.v3.oas.annotations.Parameter;
@@ -22,6 +23,7 @@
2223
import org.springdoc.core.converters.models.Pageable;
2324
import org.springdoc.core.customizers.DelegatingMethodParameterCustomizer;
2425

26+
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
2527
import org.springframework.core.MethodParameter;
2628
import org.springframework.data.web.PageableDefault;
2729

@@ -35,11 +37,16 @@ public class DataRestDelegatingMethodParameterCustomizer implements DelegatingMe
3537
*/
3638
private static final Logger LOGGER = LoggerFactory.getLogger(DataRestDelegatingMethodParameterCustomizer.class);
3739

40+
private final Optional<SpringDataWebProperties> optionalSpringDataWebProperties;
41+
42+
public DataRestDelegatingMethodParameterCustomizer(Optional<SpringDataWebProperties> optionalSpringDataWebProperties) {
43+
this.optionalSpringDataWebProperties = optionalSpringDataWebProperties;
44+
}
3845

3946
@Override
4047
public void customize(MethodParameter originalParameter, MethodParameter methodParameter) {
4148
PageableDefault pageableDefault = originalParameter.getParameterAnnotation(PageableDefault.class);
42-
if (pageableDefault != null) {
49+
if (pageableDefault != null || optionalSpringDataWebProperties.isPresent()) {
4350
Field field = FieldUtils.getDeclaredField(DelegatingMethodParameter.class, "additionalParameterAnnotations", true);
4451
try {
4552
Annotation[] parameterAnnotations = (Annotation[]) field.get(methodParameter);
@@ -62,8 +69,7 @@ public void customize(MethodParameter originalParameter, MethodParameter methodP
6269
* @param pageableDefault the pageable default
6370
* @return the new parameter annotation for field
6471
*/
65-
private static Annotation getNewParameterAnnotationForField(String parameterName, PageableDefault pageableDefault) {
66-
String defaultValue = getDefaultValue(parameterName, pageableDefault);
72+
private Annotation getNewParameterAnnotationForField(String parameterName, PageableDefault pageableDefault) {
6773
Field field;
6874
Parameter parameterNew = null;
6975
try {
@@ -77,10 +83,7 @@ public Class<? extends Annotation> annotationType() {
7783

7884
@Override
7985
public String name() {
80-
if (parameterName.equals("sort"))
81-
return defaultValue;
82-
else
83-
return parameter.name();
86+
return getName(parameterName, pageableDefault, parameter.name());
8487
}
8588

8689
@Override
@@ -289,10 +292,7 @@ public String[] allowableValues() {
289292

290293
@Override
291294
public String defaultValue() {
292-
if (!parameterName.equals("sort"))
293-
return defaultValue;
294-
else
295-
return parameter.schema().defaultValue();
295+
return getDefaultValue(parameterName, pageableDefault, parameter.schema().defaultValue());
296296
}
297297

298298
@Override
@@ -369,27 +369,65 @@ public String ref() {
369369
return parameterNew;
370370
}
371371

372+
private String getName(String parameterName, PageableDefault pageableDefault, String originalName) {
373+
String name = null;
374+
switch (parameterName) {
375+
case "size":
376+
if (optionalSpringDataWebProperties.isPresent())
377+
name = optionalSpringDataWebProperties.get().getPageable().getSizeParameter();
378+
else
379+
name = originalName;
380+
break;
381+
case "sort":
382+
if (pageableDefault != null && ArrayUtils.isNotEmpty(pageableDefault.sort()))
383+
name = String.join(",", pageableDefault.sort());
384+
else if (optionalSpringDataWebProperties.isPresent())
385+
name = optionalSpringDataWebProperties.get().getSort().getSortParameter();
386+
break;
387+
case "page":
388+
if (optionalSpringDataWebProperties.isPresent())
389+
name = optionalSpringDataWebProperties.get().getPageable().getPageParameter();
390+
else
391+
name = originalName;
392+
break;
393+
case "direction":
394+
name = originalName;
395+
break;
396+
default:
397+
// Do nothing here
398+
break;
399+
}
400+
return name;
401+
}
402+
372403
/**
373404
* Gets default value.
374405
*
375406
* @param parameterName the parameter name
376407
* @param pageableDefault the pageable default
408+
* @param defaultSchemaVal the default schema val
377409
* @return the default value
378410
*/
379-
private static String getDefaultValue(String parameterName, PageableDefault pageableDefault) {
411+
private String getDefaultValue(String parameterName, PageableDefault pageableDefault, String defaultSchemaVal) {
380412
String defaultValue = null;
381413
switch (parameterName) {
382414
case "size":
383-
defaultValue = String.valueOf(pageableDefault.size());
415+
if (pageableDefault != null)
416+
defaultValue = String.valueOf(pageableDefault.size());
417+
else if (optionalSpringDataWebProperties.isPresent())
418+
defaultValue = String.valueOf(optionalSpringDataWebProperties.get().getPageable().getDefaultPageSize());
384419
break;
385420
case "sort":
386-
defaultValue = String.join(",", pageableDefault.sort());
421+
if (pageableDefault != null)
422+
defaultValue = defaultSchemaVal;
387423
break;
388424
case "page":
389-
defaultValue = String.valueOf(pageableDefault.page());
425+
if (pageableDefault != null)
426+
defaultValue = String.valueOf(pageableDefault.page());
390427
break;
391428
case "direction":
392-
defaultValue = pageableDefault.direction().name();
429+
if (pageableDefault != null)
430+
defaultValue = pageableDefault.direction().name();
393431
break;
394432
default:
395433
// Do nothing here
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.app14;
20+
21+
import java.util.List;
22+
23+
import org.springdoc.api.annotations.ParameterObject;
24+
25+
import org.springframework.data.domain.Pageable;
26+
import org.springframework.http.ResponseEntity;
27+
import org.springframework.web.bind.annotation.GetMapping;
28+
import org.springframework.web.bind.annotation.RestController;
29+
30+
@RestController
31+
public class HelloController {
32+
33+
@GetMapping(value = "/search", produces = { "application/xml", "application/json" })
34+
public ResponseEntity<List<PersonDTO>> getAllPets(@ParameterObject Pageable pageable) {
35+
return null;
36+
}
37+
38+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.app14;
20+
21+
import io.swagger.v3.oas.annotations.media.Schema;
22+
23+
@Schema
24+
public class PersonDTO {
25+
private String email;
26+
27+
private String firstName;
28+
29+
private String lastName;
30+
31+
public PersonDTO() {
32+
}
33+
34+
public PersonDTO(final String email, final String firstName, final String lastName) {
35+
this.email = email;
36+
this.firstName = firstName;
37+
this.lastName = lastName;
38+
}
39+
40+
public String getEmail() {
41+
return email;
42+
}
43+
44+
public void setEmail(final String email) {
45+
this.email = email;
46+
}
47+
48+
public String getFirstName() {
49+
return firstName;
50+
}
51+
52+
public void setFirstName(final String firstName) {
53+
this.firstName = firstName;
54+
}
55+
56+
public String getLastName() {
57+
return lastName;
58+
}
59+
60+
public void setLastName(final String lastName) {
61+
this.lastName = lastName;
62+
}
63+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
24+
package test.org.springdoc.api.app14;
25+
26+
import test.org.springdoc.api.AbstractSpringDocTest;
27+
28+
import org.springframework.boot.autoconfigure.SpringBootApplication;
29+
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
30+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
31+
import org.springframework.test.context.TestPropertySource;
32+
33+
@TestPropertySource(properties = { "spring.data.web.pageable.default-page-size=25",
34+
"spring.data.web.pageable.page-parameter=pages",
35+
"spring.data.web.pageable.size-parameter=sizes",
36+
"spring.data.web.sort.sort-parameter=sorts" })
37+
@EnableConfigurationProperties(value = { SpringDataWebProperties.class})
38+
public class SpringDocApp14Test extends AbstractSpringDocTest {
39+
40+
@SpringBootApplication
41+
static class SpringDocTestApp {}
42+
43+
}

0 commit comments

Comments
 (0)