Skip to content

Commit 123b45e

Browse files
author
bnasslahsen
committed
Add support for spring.data.rest.default... properties.
1 parent c4eb575 commit 123b45e

File tree

8 files changed

+321
-11
lines changed

8 files changed

+321
-11
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,15 @@ public class SpringDocDataRestConfiguration {
8484
/**
8585
* Delegating method parameter customizer delegating method parameter customizer.
8686
*
87+
* @param optionalSpringDataWebProperties the optional spring data web properties
88+
* @param optionalRepositoryRestConfiguration the optional repository rest configuration
8789
* @return the delegating method parameter customizer
8890
*/
8991
@Bean
9092
@ConditionalOnMissingBean
9193
@Lazy(false)
92-
DelegatingMethodParameterCustomizer delegatingMethodParameterCustomizer(Optional<SpringDataWebProperties> optionalSpringDataWebProperties) {
93-
return new DataRestDelegatingMethodParameterCustomizer(optionalSpringDataWebProperties);
94+
DelegatingMethodParameterCustomizer delegatingMethodParameterCustomizer(Optional<SpringDataWebProperties> optionalSpringDataWebProperties, Optional<RepositoryRestConfiguration> optionalRepositoryRestConfiguration) {
95+
return new DataRestDelegatingMethodParameterCustomizer(optionalSpringDataWebProperties,optionalRepositoryRestConfiguration);
9496
}
9597

9698
/**

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

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
2727
import org.springframework.core.MethodParameter;
28+
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
2829
import org.springframework.data.web.PageableDefault;
2930

3031
/**
@@ -37,16 +38,31 @@ public class DataRestDelegatingMethodParameterCustomizer implements DelegatingMe
3738
*/
3839
private static final Logger LOGGER = LoggerFactory.getLogger(DataRestDelegatingMethodParameterCustomizer.class);
3940

41+
/**
42+
* The Optional spring data web properties.
43+
*/
4044
private final Optional<SpringDataWebProperties> optionalSpringDataWebProperties;
4145

42-
public DataRestDelegatingMethodParameterCustomizer(Optional<SpringDataWebProperties> optionalSpringDataWebProperties) {
46+
/**
47+
* The Optional repository rest configuration.
48+
*/
49+
private final Optional<RepositoryRestConfiguration> optionalRepositoryRestConfiguration;
50+
51+
/**
52+
* Instantiates a new Data rest delegating method parameter customizer.
53+
*
54+
* @param optionalSpringDataWebProperties the optional spring data web properties
55+
* @param optionalRepositoryRestConfiguration the optional repository rest configuration
56+
*/
57+
public DataRestDelegatingMethodParameterCustomizer(Optional<SpringDataWebProperties> optionalSpringDataWebProperties, Optional<RepositoryRestConfiguration> optionalRepositoryRestConfiguration) {
4358
this.optionalSpringDataWebProperties = optionalSpringDataWebProperties;
59+
this.optionalRepositoryRestConfiguration = optionalRepositoryRestConfiguration;
4460
}
4561

4662
@Override
4763
public void customize(MethodParameter originalParameter, MethodParameter methodParameter) {
4864
PageableDefault pageableDefault = originalParameter.getParameterAnnotation(PageableDefault.class);
49-
if (pageableDefault != null || optionalSpringDataWebProperties.isPresent()) {
65+
if (pageableDefault != null || optionalSpringDataWebProperties.isPresent() || optionalRepositoryRestConfiguration.isPresent()) {
5066
Field field = FieldUtils.getDeclaredField(DelegatingMethodParameter.class, "additionalParameterAnnotations", true);
5167
try {
5268
Annotation[] parameterAnnotations = (Annotation[]) field.get(methodParameter);
@@ -369,29 +385,45 @@ public String ref() {
369385
return parameterNew;
370386
}
371387

388+
/**
389+
* Gets name.
390+
*
391+
* @param parameterName the parameter name
392+
* @param pageableDefault the pageable default
393+
* @param originalName the original name
394+
* @return the name
395+
*/
372396
private String getName(String parameterName, PageableDefault pageableDefault, String originalName) {
373397
String name = null;
374398
switch (parameterName) {
375399
case "size":
376-
if (optionalSpringDataWebProperties.isPresent())
400+
if (optionalRepositoryRestConfiguration.isPresent())
401+
name = optionalRepositoryRestConfiguration.get().getLimitParamName();
402+
else if (optionalSpringDataWebProperties.isPresent())
377403
name = optionalSpringDataWebProperties.get().getPageable().getSizeParameter();
378404
else
379405
name = originalName;
380406
break;
381407
case "sort":
382408
if (pageableDefault != null && ArrayUtils.isNotEmpty(pageableDefault.sort()))
383409
name = String.join(",", pageableDefault.sort());
410+
else if (optionalRepositoryRestConfiguration.isPresent())
411+
name = optionalRepositoryRestConfiguration.get().getSortParamName();
384412
else if (optionalSpringDataWebProperties.isPresent())
385413
name = optionalSpringDataWebProperties.get().getSort().getSortParameter();
414+
else
415+
name = originalName;
386416
break;
387417
case "page":
388-
if (optionalSpringDataWebProperties.isPresent())
418+
if (optionalRepositoryRestConfiguration.isPresent())
419+
name = optionalRepositoryRestConfiguration.get().getPageParamName();
420+
else if (optionalSpringDataWebProperties.isPresent())
389421
name = optionalSpringDataWebProperties.get().getPageable().getPageParameter();
390422
else
391423
name = originalName;
392424
break;
393425
case "direction":
394-
name = originalName;
426+
name = originalName;
395427
break;
396428
default:
397429
// Do nothing here
@@ -414,8 +446,12 @@ private String getDefaultValue(String parameterName, PageableDefault pageableDef
414446
case "size":
415447
if (pageableDefault != null)
416448
defaultValue = String.valueOf(pageableDefault.size());
449+
else if (optionalRepositoryRestConfiguration.isPresent())
450+
defaultValue = String.valueOf(optionalRepositoryRestConfiguration.get().getDefaultPageSize());
417451
else if (optionalSpringDataWebProperties.isPresent())
418452
defaultValue = String.valueOf(optionalSpringDataWebProperties.get().getPageable().getDefaultPageSize());
453+
else
454+
defaultValue = defaultSchemaVal;
419455
break;
420456
case "sort":
421457
if (pageableDefault != null)
@@ -424,10 +460,14 @@ else if (optionalSpringDataWebProperties.isPresent())
424460
case "page":
425461
if (pageableDefault != null)
426462
defaultValue = String.valueOf(pageableDefault.page());
463+
else
464+
defaultValue = defaultSchemaVal;
427465
break;
428466
case "direction":
429467
if (pageableDefault != null)
430468
defaultValue = pageableDefault.direction().name();
469+
else
470+
defaultValue = defaultSchemaVal;
431471
break;
432472
default:
433473
// Do nothing here

springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/app14/SpringDocApp14Test.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,50 @@
2323

2424
package test.org.springdoc.api.app14;
2525

26+
import java.util.Optional;
27+
28+
import org.springdoc.core.converters.models.Pageable;
29+
import org.springdoc.core.customizers.DelegatingMethodParameterCustomizer;
30+
import org.springdoc.data.rest.SpringDocDataRestConfiguration;
31+
import org.springdoc.data.rest.customisers.DataRestDelegatingMethodParameterCustomizer;
2632
import test.org.springdoc.api.AbstractSpringDocTest;
2733

34+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2835
import org.springframework.boot.autoconfigure.SpringBootApplication;
36+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
37+
import org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration;
2938
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
30-
import org.springframework.boot.context.properties.EnableConfigurationProperties;
39+
import org.springframework.context.annotation.Bean;
40+
import org.springframework.context.annotation.Lazy;
41+
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
3142
import org.springframework.test.context.TestPropertySource;
3243

44+
import static org.springdoc.core.SpringDocUtils.getConfig;
45+
3346
@TestPropertySource(properties = { "spring.data.web.pageable.default-page-size=25",
3447
"spring.data.web.pageable.page-parameter=pages",
3548
"spring.data.web.pageable.size-parameter=sizes",
3649
"spring.data.web.sort.sort-parameter=sorts" })
37-
@EnableConfigurationProperties(value = { SpringDataWebProperties.class})
50+
@EnableAutoConfiguration(exclude = {
51+
RepositoryRestMvcAutoConfiguration.class, SpringDocDataRestConfiguration.class
52+
})
3853
public class SpringDocApp14Test extends AbstractSpringDocTest {
3954

55+
static {
56+
getConfig().replaceWithClass(org.springframework.data.domain.Pageable.class, Pageable.class)
57+
.replaceWithClass(org.springframework.data.domain.PageRequest.class, Pageable.class);
58+
}
59+
60+
4061
@SpringBootApplication
41-
static class SpringDocTestApp {}
62+
static class SpringDocTestApp {
63+
// We only need to test spring-web with Pageable, without the use of spring-data-rest-starter
64+
@Bean
65+
@ConditionalOnMissingBean
66+
@Lazy(false)
67+
DelegatingMethodParameterCustomizer delegatingMethodParameterCustomizer(Optional<SpringDataWebProperties> optionalSpringDataWebProperties, Optional<RepositoryRestConfiguration> optionalRepositoryRestConfiguration) {
68+
return new DataRestDelegatingMethodParameterCustomizer(optionalSpringDataWebProperties, optionalRepositoryRestConfiguration);
69+
}
70+
}
4271

4372
}
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.app15;
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.app15;
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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.app15;
25+
26+
import test.org.springdoc.api.AbstractSpringDocTest;
27+
28+
import org.springframework.boot.autoconfigure.SpringBootApplication;
29+
import org.springframework.test.context.TestPropertySource;
30+
31+
@TestPropertySource(properties = "spring.data.rest.default-page-size=50")
32+
public class SpringDocApp15Test extends AbstractSpringDocTest {
33+
34+
@SpringBootApplication
35+
static class SpringDocTestApp {}
36+
37+
}

springdoc-openapi-data-rest/src/test/resources/results/app14.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"required": false,
2626
"schema": {
2727
"minimum": 0,
28-
"type": "integer"
28+
"type": "integer",
29+
"default": "0"
2930
}
3031
},
3132
{

0 commit comments

Comments
 (0)