Skip to content

Commit a84bb0c

Browse files
committed
Make new snippet relaxed when adding descriptors to relaxed snippet
Closes gh-400
1 parent 214c068 commit a84bb0c

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
lines changed

spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/AbstractFieldsSnippet.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,15 @@ protected final List<FieldDescriptor> getFieldDescriptors() {
305305
return this.fieldDescriptors;
306306
}
307307

308+
/**
309+
* Returns whether or not this snippet ignores undocumented fields.
310+
*
311+
* @return {@code true} if undocumented fields are ignored, otherwise {@code false}
312+
*/
313+
protected final boolean isIgnoredUndocumentedFields() {
314+
return this.ignoreUndocumentedFields;
315+
}
316+
308317
/**
309318
* Returns a model for the given {@code descriptor}.
310319
*

spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/RequestFieldsSnippet.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -206,7 +206,8 @@ public final RequestFieldsSnippet andWithPrefix(String pathPrefix,
206206
combinedDescriptors.addAll(getFieldDescriptors());
207207
combinedDescriptors.addAll(PayloadDocumentation.applyPathPrefix(pathPrefix,
208208
Arrays.asList(additionalDescriptors)));
209-
return new RequestFieldsSnippet(combinedDescriptors, this.getAttributes());
209+
return new RequestFieldsSnippet(combinedDescriptors, getAttributes(),
210+
isIgnoredUndocumentedFields());
210211
}
211212

212213
/**
@@ -225,7 +226,8 @@ public final RequestFieldsSnippet andWithPrefix(String pathPrefix,
225226
getFieldDescriptors());
226227
combinedDescriptors.addAll(
227228
PayloadDocumentation.applyPathPrefix(pathPrefix, additionalDescriptors));
228-
return new RequestFieldsSnippet(combinedDescriptors, this.getAttributes());
229+
return new RequestFieldsSnippet(combinedDescriptors, getAttributes(),
230+
isIgnoredUndocumentedFields());
229231
}
230232

231233
}

spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/ResponseFieldsSnippet.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -210,7 +210,8 @@ public final ResponseFieldsSnippet andWithPrefix(String pathPrefix,
210210
combinedDescriptors.addAll(getFieldDescriptors());
211211
combinedDescriptors.addAll(PayloadDocumentation.applyPathPrefix(pathPrefix,
212212
Arrays.asList(additionalDescriptors)));
213-
return new ResponseFieldsSnippet(combinedDescriptors, this.getAttributes());
213+
return new ResponseFieldsSnippet(combinedDescriptors, this.getAttributes(),
214+
isIgnoredUndocumentedFields());
214215
}
215216

216217
/**
@@ -229,7 +230,8 @@ public final ResponseFieldsSnippet andWithPrefix(String pathPrefix,
229230
getFieldDescriptors());
230231
combinedDescriptors.addAll(
231232
PayloadDocumentation.applyPathPrefix(pathPrefix, additionalDescriptors));
232-
return new ResponseFieldsSnippet(combinedDescriptors, this.getAttributes());
233+
return new ResponseFieldsSnippet(combinedDescriptors, this.getAttributes(),
234+
isIgnoredUndocumentedFields());
233235
}
234236

235237
}

spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestFieldsSnippetTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,20 @@ public void allUndocumentedRequestFieldsCanBeIgnored() throws IOException {
158158
.content("{\"a\": 5, \"b\": 4}").build());
159159
}
160160

161+
@Test
162+
public void allUndocumentedFieldsContinueToBeIgnoredAfterAddingDescriptors()
163+
throws IOException {
164+
this.snippets.expectRequestFields()
165+
.withContents(tableWithHeader("Path", "Type", "Description")
166+
.row("`b`", "`Number`", "Field b")
167+
.row("`c.d`", "`Number`", "Field d"));
168+
169+
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("b").description("Field b")),
170+
true).andWithPrefix("c.", fieldWithPath("d").description("Field d"))
171+
.document(this.operationBuilder.request("http://localhost")
172+
.content("{\"a\":5,\"b\":4,\"c\":{\"d\": 3}}").build());
173+
}
174+
161175
@Test
162176
public void missingOptionalRequestField() throws IOException {
163177
this.snippets.expectRequestFields()

spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/ResponseFieldsSnippetTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,21 @@ public void allUndocumentedFieldsCanBeIgnored() throws IOException {
134134
.content("{\"a\": 5, \"b\": 4}").build());
135135
}
136136

137+
@Test
138+
public void allUndocumentedFieldsContinueToBeIgnoredAfterAddingDescriptors()
139+
throws IOException {
140+
this.snippets.expectResponseFields()
141+
.withContents(tableWithHeader("Path", "Type", "Description")
142+
.row("`b`", "`Number`", "Field b")
143+
.row("`c.d`", "`Number`", "Field d"));
144+
145+
new ResponseFieldsSnippet(
146+
Arrays.asList(fieldWithPath("b").description("Field b")), true)
147+
.andWithPrefix("c.", fieldWithPath("d").description("Field d"))
148+
.document(this.operationBuilder.response()
149+
.content("{\"a\":5,\"b\":4,\"c\":{\"d\": 3}}").build());
150+
}
151+
137152
@Test
138153
public void responseFieldsWithCustomAttributes() throws IOException {
139154
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);

0 commit comments

Comments
 (0)