4
votes

I'm having a response which is an json array. Each element has it's meaning and I would be able to describe it.

My array:

["1525032694","300","true"]

I've found an example in the documentation that describes an array and each element which is the same:

https://docs.spring.io/spring-restdocs/docs/current/reference/html5/#documenting-your-api-request-response-payloads-fields-reusing-field-descriptors

But I would like to know how I can describe each of it as:

current timestamp, seconds to next measurement, should perform fota

My current test:

webTestClient.post().uri("/api//measurement/${SampleData.deviceId}")
    .syncBody(SampleData.sampleMeasurement())
    .exchange()
    .expectStatus()
    .isOk
        .expectBody()
        .consumeWith(document("add-measurement", responseFields(
                fieldWithPath("[]")
                        .description("An array of device settings"))
        ))
1
I've not used spring-restdocs. Does fieldWithPath("0").description("current timestamp") work? In JavaScript each array element is accessible like any other property using "0", "1", "2", ..., "${n}"... but this isn't JavaScript. :-) If "0" doesn't work I wonder if "[0]" works... - mfulton26
Do you have control of the API response? Why don't you return a proper object instead of an array with the device settings? - Pedro Tavares
Unfortunately I can't control this, that's why I need to document response even more precisely - pixel

1 Answers

0
votes

Ok, it turns that be fairly easy:

        responseFields(
            fieldWithPath("[0]").description("Current timestamp"),
            fieldWithPath("[1]").description("Device mode"),
            fieldWithPath("[2]").description("Device parameter")
        ),