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 8e2184f8..d6e384cf 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 @@ -1,5 +1,5 @@ /* - * Copyright 2014-2017 the original author or authors. + * Copyright 2014-2018 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. @@ -296,6 +296,16 @@ public abstract class AbstractFieldsSnippet extends TemplatedSnippet { return this.ignoreUndocumentedFields; } + /** + * Returns the {@link PayloadSubsectionExtractor}, if any, used by this snippet. + * + * @return the subsection extractor or {@code null} + * @since 1.2.4 + */ + protected final PayloadSubsectionExtractor getSubsectionExtractor() { + return this.subsectionExtractor; + } + /** * 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 b882526a..bc8b8f7c 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-2017 the original author or authors. + * Copyright 2014-2018 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,8 +206,8 @@ public class RequestFieldsSnippet extends AbstractFieldsSnippet { combinedDescriptors.addAll(getFieldDescriptors()); combinedDescriptors.addAll(PayloadDocumentation.applyPathPrefix(pathPrefix, Arrays.asList(additionalDescriptors))); - return new RequestFieldsSnippet(combinedDescriptors, getAttributes(), - isIgnoredUndocumentedFields()); + return new RequestFieldsSnippet(getSubsectionExtractor(), combinedDescriptors, + getAttributes(), isIgnoredUndocumentedFields()); } /** @@ -226,8 +226,8 @@ public class RequestFieldsSnippet extends AbstractFieldsSnippet { getFieldDescriptors()); combinedDescriptors.addAll( PayloadDocumentation.applyPathPrefix(pathPrefix, additionalDescriptors)); - return new RequestFieldsSnippet(combinedDescriptors, getAttributes(), - isIgnoredUndocumentedFields()); + return new RequestFieldsSnippet(getSubsectionExtractor(), 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 f652440a..144e468d 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-2017 the original author or authors. + * Copyright 2014-2018 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,8 +210,8 @@ public class ResponseFieldsSnippet extends AbstractFieldsSnippet { combinedDescriptors.addAll(getFieldDescriptors()); combinedDescriptors.addAll(PayloadDocumentation.applyPathPrefix(pathPrefix, Arrays.asList(additionalDescriptors))); - return new ResponseFieldsSnippet(combinedDescriptors, this.getAttributes(), - isIgnoredUndocumentedFields()); + return new ResponseFieldsSnippet(getSubsectionExtractor(), combinedDescriptors, + this.getAttributes(), isIgnoredUndocumentedFields()); } /** @@ -230,8 +230,8 @@ public class ResponseFieldsSnippet extends AbstractFieldsSnippet { getFieldDescriptors()); combinedDescriptors.addAll( PayloadDocumentation.applyPathPrefix(pathPrefix, additionalDescriptors)); - return new ResponseFieldsSnippet(combinedDescriptors, this.getAttributes(), - isIgnoredUndocumentedFields()); + return new ResponseFieldsSnippet(getSubsectionExtractor(), 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 7fe022a2..ffb4d4dc 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 @@ -103,6 +103,18 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests { .build()); } + @Test + public void subsectionOfMapRequestWithCommonPrefix() throws IOException { + this.snippets.expect("request-fields-beneath-a") + .withContents(tableWithHeader("Path", "Type", "Description").row("`b.c`", + "`String`", "two")); + + requestFields(beneathPath("a")) + .andWithPrefix("b.", fieldWithPath("c").description("two")) + .document(this.operationBuilder.request("http://localhost") + .content("{\"a\": {\"b\": {\"c\": \"charlie\"}}}").build()); + } + @Test public void arrayRequestWithFields() 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 43995020..cd6fdb52 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 @@ -95,6 +95,17 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests { .build()); } + @Test + public void subsectionOfMapResponseWithCommonsPrefix() throws IOException { + this.snippets.expect("response-fields-beneath-a") + .withContents(tableWithHeader("Path", "Type", "Description").row("`b.c`", + "`String`", "two")); + responseFields(beneathPath("a")) + .andWithPrefix("b.", fieldWithPath("c").description("two")) + .document(this.operationBuilder.response() + .content("{\"a\": {\"b\": {\"c\": \"charlie\"}}}").build()); + } + @Test public void arrayResponseWithFields() throws IOException { this.snippets.expectResponseFields()