4

I know fieldWithPath() has optional() but didn't find how to make parameterWithName() optional.

Johnny Lim
  • 5,623
  • 8
  • 38
  • 53

1 Answers1

3

In Spring REST Docs 1.1 you can mark request and path parameters as optional. If you want that information to also appear in the generated table you can use a custom snippet that adds the optional column (once this bug has been fixed).

In Spring REST Docs 1.0 you can't mark a request parameter as optional via a first-class API. I would recommend using a combination of a custom attribute on the parameter descriptor and, if necessary, setting the ignored flag on the descriptor to allow the test to pass in the parameter's absence.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • 1
    Just throwing in my 2 cents for feed back. I VERY much disagree that having an optional column is not warranted. That is a critical detail of each field and is something in pretty much all documentation. I know about the adding attributes option, but that requires a lot of extra code and often does not seem to work. The attributes frequently do not show up in the generated snippets. – BrianC May 04 '16 at 04:03
  • @BrianC The functionality you're looking for is coming in 1.1. I've updated my answer accordingly. If you are having trouble with adding attributes not working, please [open an issue](https://github.com/spring-projects/spring-restdocs/issues/new) with a testcase that illustrates the problem. – Andy Wilkinson May 04 '16 at 08:52