From ffe4c93c3862df712dde666c04be64faaf041bc8 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 1 Sep 2020 13:54:39 +0100 Subject: [PATCH] Polish "Propagate ignoreUndocumentedParamteres with .and()" See gh-676 --- .../restdocs/request/AbstractParametersSnippet.java | 10 ++++++++-- .../restdocs/request/PathParametersSnippet.java | 4 ++-- .../restdocs/request/RequestParametersSnippet.java | 5 +++-- .../restdocs/request/PathParametersSnippetTests.java | 12 +++++++++++- .../request/RequestParametersSnippetTests.java | 7 ++++--- 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/request/AbstractParametersSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/request/AbstractParametersSnippet.java index dd590be2..523d65c8 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/request/AbstractParametersSnippet.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/request/AbstractParametersSnippet.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2020 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. @@ -148,8 +148,14 @@ public abstract class AbstractParametersSnippet extends TemplatedSnippet { return this.descriptorsByName; } + /** + * Returns whether to ignore undocumented parameters. + * @return {@code true} if undocumented parameters should be ignored, otherwise + * {@code false} + * @since 2.0.5 + */ protected final boolean isIgnoreUndocumentedParameters() { - return ignoreUndocumentedParameters; + return this.ignoreUndocumentedParameters; } /** diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/request/PathParametersSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/request/PathParametersSnippet.java index 91acf47b..6b4f4f32 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/request/PathParametersSnippet.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/request/PathParametersSnippet.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2020 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. @@ -170,7 +170,7 @@ public class PathParametersSnippet extends AbstractParametersSnippet { public final PathParametersSnippet and(List additionalDescriptors) { List combinedDescriptors = new ArrayList<>(getParameterDescriptors().values()); combinedDescriptors.addAll(additionalDescriptors); - return new PathParametersSnippet(combinedDescriptors, this.getAttributes()); + return new PathParametersSnippet(combinedDescriptors, this.getAttributes(), isIgnoreUndocumentedParameters()); } } diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/request/RequestParametersSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/request/RequestParametersSnippet.java index 5af9abee..b1a1afc1 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/request/RequestParametersSnippet.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/request/RequestParametersSnippet.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2020 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. @@ -133,7 +133,8 @@ public class RequestParametersSnippet extends AbstractParametersSnippet { public RequestParametersSnippet and(List additionalDescriptors) { List combinedDescriptors = new ArrayList<>(getParameterDescriptors().values()); combinedDescriptors.addAll(additionalDescriptors); - return new RequestParametersSnippet(combinedDescriptors, this.getAttributes(), this.isIgnoreUndocumentedParameters()); + return new RequestParametersSnippet(combinedDescriptors, this.getAttributes(), + this.isIgnoreUndocumentedParameters()); } } diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java index 2f4b5bdc..bb455ae9 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/PathParametersSnippetTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2020 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. @@ -159,6 +159,16 @@ public class PathParametersSnippetTests extends AbstractSnippetTests { tableWithTitleAndHeader(getTitle(), "Parameter", "Description").row("`a`", "one").row("`b`", "two")); } + @Test + public void additionalDescriptorsWithRelaxedRequestParameters() throws IOException { + RequestDocumentation.relaxedPathParameters(parameterWithName("a").description("one")) + .and(parameterWithName("b").description("two")).document(this.operationBuilder + .attribute(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, "/{a}/{b}/{c}").build()); + assertThat(this.generatedSnippets.pathParameters()) + .is(tableWithTitleAndHeader(getTitle("/{a}/{b}/{c}"), "Parameter", "Description").row("`a`", "one") + .row("`b`", "two")); + } + @Test public void pathParametersWithEscapedContent() throws IOException { RequestDocumentation.pathParameters(parameterWithName("Foo|Bar").description("one|two")) diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/RequestParametersSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/RequestParametersSnippetTests.java index 19a47c65..92857a27 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/RequestParametersSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/request/RequestParametersSnippetTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2020 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. @@ -156,8 +156,9 @@ public class RequestParametersSnippetTests extends AbstractSnippetTests { @Test public void additionalDescriptorsWithRelaxedRequestParameters() throws IOException { RequestDocumentation.relaxedRequestParameters(parameterWithName("a").description("one")) - .and(parameterWithName("b").description("two")).document(this.operationBuilder - .request("http://localhost").param("a", "bravo").param("b", "bravo").param("c", "undocumented").build()); + .and(parameterWithName("b").description("two")) + .document(this.operationBuilder.request("http://localhost").param("a", "bravo").param("b", "bravo") + .param("c", "undocumented").build()); assertThat(this.generatedSnippets.requestParameters()) .is(tableWithHeader("Parameter", "Description").row("`a`", "one").row("`b`", "two")); }