Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit b063569

Browse files
authored
request parameters paging (#132)
* request parameters paging * removed spring-data-commons as transitive dependency * merge
1 parent 9955e08 commit b063569

File tree

15 files changed

+181
-46
lines changed

15 files changed

+181
-46
lines changed

docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,7 @@ <h4 id="contributing-building-build"><a class="link" href="#contributing-buildin
15241524
</div>
15251525
<div id="footer">
15261526
<div id="footer-text">
1527-
Last updated 2017-08-19 17:44:51 CEST
1527+
Last updated 2017-08-30 12:55:11 CEST
15281528
</div>
15291529
</div>
15301530
<link rel="stylesheet" href="highlight/styles/github.min.css">

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,11 @@
275275
<artifactId>javax.el-api</artifactId>
276276
<version>2.2.5</version>
277277
</dependency>
278+
<dependency>
279+
<groupId>org.slf4j</groupId>
280+
<artifactId>slf4j-api</artifactId>
281+
<version>1.7.22</version>
282+
</dependency>
278283
<dependency>
279284
<groupId>org.slf4j</groupId>
280285
<artifactId>slf4j-simple</artifactId>

spring-auto-restdocs-core/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<dependency>
5151
<groupId>org.springframework.data</groupId>
5252
<artifactId>spring-data-commons</artifactId>
53+
<scope>test</scope>
5354
</dependency>
5455
<dependency>
5556
<groupId>javax.validation</groupId>
@@ -89,6 +90,10 @@
8990
<version>3.1.0</version>
9091
<scope>provided</scope>
9192
</dependency>
93+
<dependency>
94+
<groupId>org.slf4j</groupId>
95+
<artifactId>slf4j-api</artifactId>
96+
</dependency>
9297
<dependency>
9398
<groupId>org.slf4j</groupId>
9499
<artifactId>slf4j-simple</artifactId>

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/payload/JacksonResponseFieldSnippet.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
import java.lang.reflect.Type;
2121
import java.util.Map;
2222

23-
import org.springframework.data.domain.Page;
2423
import org.springframework.http.HttpEntity;
2524
import org.springframework.http.ResponseEntity;
2625
import org.springframework.web.method.HandlerMethod;
2726

2827
public class JacksonResponseFieldSnippet extends AbstractJacksonFieldSnippet {
2928

3029
public static final String RESPONSE_FIELDS = "response-fields";
30+
public static final String SPRING_DATA_PAGE_CLASS = "org.springframework.data.domain.Page";
3131

3232
private final Type responseBodyType;
3333
private final boolean failOnUndocumentedFields;
@@ -61,7 +61,7 @@ protected Type getType(final HandlerMethod method) {
6161
return firstGenericType(method.getReturnType());
6262
} else if (returnType == HttpEntity.class) {
6363
return firstGenericType(method.getReturnType());
64-
} else if (returnType == Page.class) {
64+
} else if (SPRING_DATA_PAGE_CLASS.equals(returnType.getCanonicalName())) {
6565
return firstGenericType(method.getReturnType());
6666
} else if (isCollection(returnType)) {
6767
return new GenericArrayType() {
@@ -80,11 +80,12 @@ public Type getGenericComponentType() {
8080

8181
@Override
8282
protected void enrichModel(Map<String, Object> model, HandlerMethod handlerMethod) {
83-
model.put("isPagedResponse", handlerMethod != null && isPageResponse(handlerMethod));
83+
model.put("isPageResponse", isPageResponse(handlerMethod));
8484
}
8585

8686
private boolean isPageResponse(HandlerMethod handlerMethod) {
87-
return handlerMethod.getReturnType().getParameterType() == Page.class;
87+
return SPRING_DATA_PAGE_CLASS.equals(
88+
handlerMethod.getReturnType().getParameterType().getCanonicalName());
8889
}
8990

9091
@Override

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/request/RequestParametersSnippet.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@
1616

1717
package capital.scalable.restdocs.request;
1818

19+
import java.util.Map;
20+
1921
import org.springframework.core.MethodParameter;
2022
import org.springframework.web.bind.annotation.RequestParam;
2123
import org.springframework.web.bind.annotation.ValueConstants;
24+
import org.springframework.web.method.HandlerMethod;
2225

2326
public class RequestParametersSnippet extends AbstractParameterSnippet<RequestParam> {
2427

2528
public static final String REQUEST_PARAMETERS = "request-parameters";
29+
public static final String SPRING_DATA_PAGEABLE_CLASS =
30+
"org.springframework.data.domain.Pageable";
2631

2732
private final boolean failOnUndocumentedParams;
2833

@@ -55,6 +60,30 @@ protected RequestParam getAnnotation(MethodParameter param) {
5560
return param.getParameterAnnotation(RequestParam.class);
5661
}
5762

63+
@Override
64+
protected void enrichModel(Map<String, Object> model, HandlerMethod handlerMethod) {
65+
boolean isPageRequest = isPageRequest(handlerMethod);
66+
model.put("isPageRequest", isPageRequest);
67+
if (isPageRequest) {
68+
model.put("noContent", false);
69+
}
70+
}
71+
72+
private boolean isPageRequest(HandlerMethod method) {
73+
for (MethodParameter param : method.getMethodParameters()) {
74+
if (isPageable(param)) {
75+
return true;
76+
}
77+
}
78+
return false;
79+
}
80+
81+
private boolean isPageable(MethodParameter param) {
82+
return SPRING_DATA_PAGEABLE_CLASS.equals(
83+
param.getParameterType().getCanonicalName());
84+
}
85+
86+
5887
@Override
5988
public String getHeader() {
6089
return "Query parameters";

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/snippet/StandardTableSnippet.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ protected void enrichModel(Map<String, Object> model, HandlerMethod handlerMetho
6666
private Map<String, Object> createModel(HandlerMethod handlerMethod,
6767
Collection<FieldDescriptor> fieldDescriptors, String forcedLineBreak) {
6868
Map<String, Object> model = new HashMap<>();
69-
enrichModel(model, handlerMethod);
7069

7170
List<Map<String, Object>> fields = new ArrayList<>();
7271
model.put("content", fields);
@@ -75,6 +74,9 @@ private Map<String, Object> createModel(HandlerMethod handlerMethod,
7574
}
7675
model.put("hasContent", !fieldDescriptors.isEmpty());
7776
model.put("noContent", fieldDescriptors.isEmpty());
77+
78+
enrichModel(model, handlerMethod);
79+
7880
return model;
7981
}
8082

spring-auto-restdocs-core/src/main/resources/org/springframework/restdocs/templates/asciidoctor/request-parameters.snippet

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
{{#hasContent}}|===
1+
{{#isPageRequest}}Supports standard <<overview-pagination,paging>> query parameters.
2+
3+
{{/isPageRequest}}{{#hasContent}}|===
24
|Parameter|Type|Optional|Description
35

46
{{#content}}

spring-auto-restdocs-core/src/main/resources/org/springframework/restdocs/templates/asciidoctor/response-fields.snippet

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
{{#hasContent}}
2-
{{#isPagedResponse}}Standard <<overview-pagination,paging>> response where `content` field is list of following objects:{{/isPagedResponse}}
3-
|===
1+
{{#hasContent}}{{#isPageResponse}}Standard <<overview-pagination,paging>> response where `content` field is list of following objects:
2+
3+
{{/isPageResponse}}|===
44
|Path|Type|Optional|Description
55

66
{{#content}}

spring-auto-restdocs-core/src/main/resources/org/springframework/restdocs/templates/markdown/request-parameters.snippet

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
{{#hasContent}}Parameter | Type | Optional | Description
1+
{{#isPageRequest}}Supports standard [paging](#overview-pagination) query parameters.
2+
3+
{{/isPageRequest}}{{#hasContent}}Parameter | Type | Optional | Description
24
--------- | ---- | -------- | -----------
35
{{#content}}
46
{{path}} | {{type}} | {{optional}} | {{description}}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
{{#hasContent}}
2-
{{#isPagedResponse}}Standard [paging](#overview-pagination) response where `content` field is list of following objects:{{/isPagedResponse}}
3-
Path | Type | Optional | Description
1+
{{#hasContent}}{{#isPageResponse}}Standard [paging](#overview-pagination) response where `content` field is list of following objects:
2+
3+
{{/isPageResponse}}Path | Type | Optional | Description
44
---- | ---- | -------- | -----------
55
{{#content}}
66
{{path}} | {{type}} | {{optional}} | {{description}}
7-
{{/content}}
8-
{{/hasContent}}{{#noContent}}No response body.{{/noContent}}
7+
{{/content}}{{/hasContent}}{{#noContent}}No response body.{{/noContent}}

0 commit comments

Comments
 (0)