Skip to content

Consistency with the subsection path. #937

@ddaaac

Description

@ddaaac
Contributor

Hi, I have two JSON string.

{
  "a": {
    "b": [
      {
        "c": "c1"
      }
    ]
  }
}
{
  "a": [
    {
      "b": [
        {
          "c": "c1"
        }
      ]
    }
  ]
}

First one, A(B(List))
responseFields(fieldWithPath("a.b[].c")) <- Test passed.
responseFields(beneathPath("a.b[]"), listOf(fieldWithPath("c"))) <- Test passed.
responseFields(beneathPath("a.b[]"), listOf(fieldWithPath("[].c"))) <- Test failed.

Second one, A(List<B(List)>)
responseFields(fieldWithPath("a[].b[].c")) <- Test passed.
responseFields(beneathPath("a[].b[]"), listOf(fieldWithPath("c"))) <- Test failed.
responseFields(beneathPath("a[].b[]"), listOf(fieldWithPath("[].c"))) <- Test passed.

Is this intentional? Does the way to find the path change if there is an array in the root?


Additional information.

responseFields(fieldWithPath("a.b[].c[].d")) <- Test passed.
responseFields(beneathPath("a.b[].c[]"), listOf(fieldWithPath("d"))) <- Test failed.
responseFields(beneathPath("a.b[].c[]"), listOf(fieldWithPath("[].d"))) <- Test Passed.

responseFields(fieldWithPath("a.b[].c.d")) <- Test passed.
responseFields(beneathPath("a.b[].c"), listOf(fieldWithPath("d"))) <- Test Passed.
responseFields(beneathPath("a.b[].c"), listOf(fieldWithPath("[].d"))) <- Test failed.

I think an array of 1 depth behaves differently from an array of n depth(n >= 2).

Activity

wilkinsona

wilkinsona commented on Aug 12, 2024

@wilkinsona
Member

See also #627.

wilkinsona

wilkinsona commented on Aug 13, 2024

@wilkinsona
Member

@ddaaac could you please share your complete tests? I'd like to be able to run them to see exactly what's happening with the field paths.

ddaaac

ddaaac commented on Aug 19, 2024

@ddaaac
ContributorAuthor
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.restdocs.RestDocumentationExtension
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get
import org.springframework.restdocs.payload.JsonFieldType
import org.springframework.restdocs.payload.PayloadDocumentation.*
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController


@SpringBootTest
@AutoConfigureMockMvc
@AutoConfigureRestDocs
@ExtendWith(RestDocumentationExtension::class)
class ArrayTest {

    @Autowired
    private lateinit var mockMvc: MockMvc

    @Test
    fun `test response1`() {
        mockMvc.perform(get("/response1"))
            .andDo(
                document(
                    "response1",
                    responseFields(
                        fieldWithPath("a.b[].c").type(JsonFieldType.STRING).description("root")
                    ),
                    responseFields(
                        beneathPath("a.b[]").withSubsectionId("array"),
                        fieldWithPath("c").type(JsonFieldType.STRING).description("c")
                    )
//                    responseFields(
//                        beneathPath("a.b[]").withSubsectionId("nestedArray"),
//                        fieldWithPath("[].c").type(JsonFieldType.STRING).description("c")
//                    )
                )
            )
    }

    @Test
    fun `test response2`() {
        mockMvc.perform(get("/response2"))
            .andExpect(status().isOk)
            .andDo(
                document(
                    "response2",
                    responseFields(
                        fieldWithPath("a[].b[].c").type(JsonFieldType.STRING).description("root")
                    ),
//                    responseFields(
//                        beneathPath("a[].b[]").withSubsectionId("array"),
//                        fieldWithPath("c").type(JsonFieldType.STRING).description("c")
//                    )
                    responseFields(
                        beneathPath("a[].b[]").withSubsectionId("nestedArray"),
                        fieldWithPath("[].c").type(JsonFieldType.STRING).description("c")
                    )
                )
            )
    }

}

@RestController
class MyController {

    @GetMapping("/response1")
    fun getResponse1(): Map<String, Any> {
        return mapOf(
            "a" to mapOf(
                "b" to listOf(
                    mapOf(
                        "c" to "c1"
                    )
                )
            )
        )
    }

    @GetMapping("/response2")
    fun getResponse2(): Map<String, Any> {
        return mapOf(
            "a" to listOf(
                mapOf(
                    "b" to listOf(
                        mapOf(
                            "c" to "c1"
                        )
                    )
                )
            )
        )
    }
}

I use kotlin. The commented part is the part that fails.

wilkinsona

wilkinsona commented on Aug 19, 2024

@wilkinsona
Member

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @wilkinsona@spring-projects-issues@ddaaac

        Issue actions

          Consistency with the subsection path. · Issue #937 · spring-projects/spring-restdocs