3ric-T
3ric-T

Reputation: 41

How to document path parameters both in the uri and in the query string?

In a JUnit 5 test, I'm trying to document my Spring Boot application API with the following:

(...)
this.webTestClient = WebTestClient.bindToApplicationContext(applicationContext)
                .configureClient()
                .filter(documentationConfiguration(restDocumentation)
                        .operationPreprocessors()
                        .withResponseDefaults(prettyPrint()))
                .build();
(...)
        webTestClient
                .post().uri("/tokens/{id}/random?size=16", TOKEN_TEST)
                 .exchange()
                .expectStatus().isOk()
                .expectBody(RandomResponse.class)
                .consumeWith(entityExchangeResult -> assertThat(entityExchangeResult.getResponseBody().getRandomBytes()).hasSize(16))
                .consumeWith(document(
                        "get_random_bytes_as_json",
                        pathParameters(
                                parameterWithName("id").description("Unique identifier of the token"),
                                parameterWithName("size").description("Size of the random byte array")
                        )));

Unfortunately, it does not work as I expected and I'm facing the following exception:

org.springframework.restdocs.snippet.SnippetException: Path parameters with the following names were not found in the request: [size]

    at org.springframework.restdocs.request.PathParametersSnippet.verificationFailed(PathParametersSnippet.java:149)
    at org.springframework.restdocs.request.AbstractParametersSnippet.verifyParameterDescriptors(AbstractParametersSnippet.java:108)
    at org.springframework.restdocs.request.AbstractParametersSnippet.createModel(AbstractParametersSnippet.java:74)

Looking at the code (PathParametersSnippet), it appears that the query string is removed from the URI before extracting actual parameters. This explains the error message. Could someone help me to build the right piece of code and to see what I'm doing wrong?

Thank you a lot for taking time to read me

Éric

Upvotes: 0

Views: 1388

Answers (1)

3ric-T
3ric-T

Reputation: 41

The answer is simple when you carefully read the documentation. There are two differents methods that address those two use-cases:

  • RequestDocumentation#requestParameters(ParameterDescriptor...)
  • RequestDocumentation#pathParameters(ParameterDescriptor... descriptors)

which can be combined like this:

.consumeWith(document(
    "get_random_bytes_as_json",
     pathParameters(
        parameterWithName("id").description("Unique identifier of the token")
    ),
    requestParameters(
        parameterWithName("size").description("Size of the random byte array")
    ))
)```

Upvotes: 1

Related Questions