Skip to content

responseFields beneathPath does not work with arrays #473

Closed
@bsb319

Description

@bsb319

Using:

org.springframework.restdocs:spring-restdocs-core:2.0.0.RELEASE
org.springframework.restdocs:spring-restdocs-mockmvc:2.0.0.RELEASE

Error:

org.springframework.restdocs.snippet.SnippetException: The following parts of the payload were not documented:
[ {
  "details" : "test details",
  "name" : "testName"
} ]
Fields with the following paths were not found in the payload: [details, name]

Test Code that causes error:

List<FieldDescriptor> responseFieldDescriptors = new ArrayList<>();
responseFieldDescriptors.add(fieldWithPath("details").type(JsonFieldType.STRING).description("Details to display to the customer."));
responseFieldDescriptors.add(fieldWithPath("name").type(JsonFieldType.STRING).description("Customer's name."));


resultActions.andDo(
        MockMvcRestDocumentation.document("content-example",
                preprocessRequest(prettyPrint()),
                preprocessResponse(prettyPrint()),
                responseFields(beneathPath("information.customers[]").withSubsectionId("customer"), responseFieldDescriptors)
        )
);

Test Code that runs but displays unwanted "[]." in generated table

List<FieldDescriptor> responseFieldDescriptors = new ArrayList<>();
responseFieldDescriptors.add(fieldWithPath("[].details").type(JsonFieldType.STRING).description("Details to display to the customer."));
responseFieldDescriptors.add(fieldWithPath("[].name").type(JsonFieldType.STRING).description("Customer's name."));


resultActions.andDo(
        MockMvcRestDocumentation.document("content-example",
                preprocessRequest(prettyPrint()),
                preprocessResponse(prettyPrint()),
                responseFields(beneathPath("information.customers").withSubsectionId("customer"), responseFieldDescriptors)
        )
);

Sample Response:

{
    "information": {
        "customers": [
            "details": "test details",
            "name": "testName"
        ]
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions