diff --git a/docs/src/docs/asciidoc/documenting-your-api.adoc b/docs/src/docs/asciidoc/documenting-your-api.adoc index 3e409f6e..9550aaf5 100644 --- a/docs/src/docs/asciidoc/documenting-your-api.adoc +++ b/docs/src/docs/asciidoc/documenting-your-api.adoc @@ -348,7 +348,8 @@ table describing the parameters that are supported by the resource. When documenting request parameters, the test will fail if an undocumented request parameter is used in the request. Similarly, the test will also fail if a documented -request parameter is not found in the request. +request parameter is not found in the request and the request parameter has not been +marked as optional. If you do not want to document a request parameter, you can mark it as ignored. This will prevent it from appearing in the generated snippet while avoiding the failure described @@ -396,7 +397,7 @@ built using one of the methods on `RestDocumentationRequestBuilders` rather than When documenting path parameters, the test will fail if an undocumented path parameter is used in the request. Similarly, the test will also fail if a documented path parameter -is not found in the request. +is not found in the request and the path parameter has not been marked as optional. If you do not want to document a path parameter, you can mark it as ignored. This will prevent it from appearing in the generated snippet while avoiding the failure described 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 21d900fe..b132aa08 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 @@ -85,7 +85,13 @@ public abstract class AbstractParametersSnippet extends TemplatedSnippet { private void verifyParameterDescriptors(Operation operation) { Set actualParameters = extractActualParameters(operation); - Set expectedParameters = this.descriptorsByName.keySet(); + Set expectedParameters = new HashSet<>(); + for (Entry entry : this.descriptorsByName + .entrySet()) { + if (!entry.getValue().isOptional()) { + expectedParameters.add(entry.getKey()); + } + } Set undocumentedParameters = new HashSet<>(actualParameters); undocumentedParameters.removeAll(expectedParameters); Set missingParameters = new HashSet<>(expectedParameters); diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/request/ParameterDescriptor.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/request/ParameterDescriptor.java index 9172a552..d19eab4f 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/request/ParameterDescriptor.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/request/ParameterDescriptor.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2016 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. @@ -29,6 +29,8 @@ public class ParameterDescriptor extends IgnorableDescriptor