From a84bb0c569fce6394b1932e18ad3dd47b5f14f98 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Sat, 1 Jul 2017 16:25:46 +0100 Subject: [PATCH] Make new snippet relaxed when adding descriptors to relaxed snippet Closes gh-400 --- .../restdocs/payload/AbstractFieldsSnippet.java | 9 +++++++++ .../restdocs/payload/RequestFieldsSnippet.java | 8 +++++--- .../restdocs/payload/ResponseFieldsSnippet.java | 8 +++++--- .../payload/RequestFieldsSnippetTests.java | 14 ++++++++++++++ .../payload/ResponseFieldsSnippetTests.java | 15 +++++++++++++++ 5 files changed, 48 insertions(+), 6 deletions(-) diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/AbstractFieldsSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/AbstractFieldsSnippet.java index 1a2364bf..988c74f0 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/AbstractFieldsSnippet.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/AbstractFieldsSnippet.java @@ -305,6 +305,15 @@ public abstract class AbstractFieldsSnippet extends TemplatedSnippet { return this.fieldDescriptors; } + /** + * Returns whether or not this snippet ignores undocumented fields. + * + * @return {@code true} if undocumented fields are ignored, otherwise {@code false} + */ + protected final boolean isIgnoredUndocumentedFields() { + return this.ignoreUndocumentedFields; + } + /** * Returns a model for the given {@code descriptor}. * diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/RequestFieldsSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/RequestFieldsSnippet.java index 9be9e32c..b882526a 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/RequestFieldsSnippet.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/RequestFieldsSnippet.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -206,7 +206,8 @@ public class RequestFieldsSnippet extends AbstractFieldsSnippet { combinedDescriptors.addAll(getFieldDescriptors()); combinedDescriptors.addAll(PayloadDocumentation.applyPathPrefix(pathPrefix, Arrays.asList(additionalDescriptors))); - return new RequestFieldsSnippet(combinedDescriptors, this.getAttributes()); + return new RequestFieldsSnippet(combinedDescriptors, getAttributes(), + isIgnoredUndocumentedFields()); } /** @@ -225,7 +226,8 @@ public class RequestFieldsSnippet extends AbstractFieldsSnippet { getFieldDescriptors()); combinedDescriptors.addAll( PayloadDocumentation.applyPathPrefix(pathPrefix, additionalDescriptors)); - return new RequestFieldsSnippet(combinedDescriptors, this.getAttributes()); + return new RequestFieldsSnippet(combinedDescriptors, getAttributes(), + isIgnoredUndocumentedFields()); } } diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/ResponseFieldsSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/ResponseFieldsSnippet.java index 79d306ea..f652440a 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/ResponseFieldsSnippet.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/payload/ResponseFieldsSnippet.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -210,7 +210,8 @@ public class ResponseFieldsSnippet extends AbstractFieldsSnippet { combinedDescriptors.addAll(getFieldDescriptors()); combinedDescriptors.addAll(PayloadDocumentation.applyPathPrefix(pathPrefix, Arrays.asList(additionalDescriptors))); - return new ResponseFieldsSnippet(combinedDescriptors, this.getAttributes()); + return new ResponseFieldsSnippet(combinedDescriptors, this.getAttributes(), + isIgnoredUndocumentedFields()); } /** @@ -229,7 +230,8 @@ public class ResponseFieldsSnippet extends AbstractFieldsSnippet { getFieldDescriptors()); combinedDescriptors.addAll( PayloadDocumentation.applyPathPrefix(pathPrefix, additionalDescriptors)); - return new ResponseFieldsSnippet(combinedDescriptors, this.getAttributes()); + return new ResponseFieldsSnippet(combinedDescriptors, this.getAttributes(), + isIgnoredUndocumentedFields()); } } diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestFieldsSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestFieldsSnippetTests.java index cf2ee2f3..93f37bbd 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestFieldsSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/RequestFieldsSnippetTests.java @@ -158,6 +158,20 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests { .content("{\"a\": 5, \"b\": 4}").build()); } + @Test + public void allUndocumentedFieldsContinueToBeIgnoredAfterAddingDescriptors() + throws IOException { + this.snippets.expectRequestFields() + .withContents(tableWithHeader("Path", "Type", "Description") + .row("`b`", "`Number`", "Field b") + .row("`c.d`", "`Number`", "Field d")); + + new RequestFieldsSnippet(Arrays.asList(fieldWithPath("b").description("Field b")), + true).andWithPrefix("c.", fieldWithPath("d").description("Field d")) + .document(this.operationBuilder.request("http://localhost") + .content("{\"a\":5,\"b\":4,\"c\":{\"d\": 3}}").build()); + } + @Test public void missingOptionalRequestField() throws IOException { this.snippets.expectRequestFields() diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/ResponseFieldsSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/ResponseFieldsSnippetTests.java index 1affb789..90600444 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/ResponseFieldsSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/payload/ResponseFieldsSnippetTests.java @@ -134,6 +134,21 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests { .content("{\"a\": 5, \"b\": 4}").build()); } + @Test + public void allUndocumentedFieldsContinueToBeIgnoredAfterAddingDescriptors() + throws IOException { + this.snippets.expectResponseFields() + .withContents(tableWithHeader("Path", "Type", "Description") + .row("`b`", "`Number`", "Field b") + .row("`c.d`", "`Number`", "Field d")); + + new ResponseFieldsSnippet( + Arrays.asList(fieldWithPath("b").description("Field b")), true) + .andWithPrefix("c.", fieldWithPath("d").description("Field d")) + .document(this.operationBuilder.response() + .content("{\"a\":5,\"b\":4,\"c\":{\"d\": 3}}").build()); + } + @Test public void responseFieldsWithCustomAttributes() throws IOException { TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);