Spring REST Docs - недокументированные части:

У меня есть приложение springBoot 2.1.9.RELEASE, которое использует Spring REST Docs.

У меня есть эта полезная нагрузка

{
  "hostel" : [ {
    "bookingHostelIdentifier" : {
      "internalMasterIdentifier" : {
        "id" : "987654321"
      }
    }
  }, {
    "bookingHostelIdentifier" : {
      "customerIdentifier" : {
        "id" : "23456789"
      }
    }
  }, {
    "bookingHostelIdentifier" : {
      "internalMasterIdentifier" : {
        "id" : "87654321"
      }
    }
  } ]
}

который я задокументировал так

fieldWithPath("hostel[]").description("The list of hostels"),
fieldWithPath("hostel[].name").description("The list of hostels"),
fieldWithPath("hostel[].bookingHostelIdentifier").description("The list of hostels"),
fieldWithPath("hostel[].bookingHostelIdentifier.internalMasterIdentifier.id").description("The list of hostels"),
fieldWithPath("hostel[].bookingHostelIdentifier.customerIdentifier.id").description("The list of hostels")

но я получил это исключение

org.springframework.restdocs.snippet.SnippetException: 
Fields with the following paths were not found in the payload: 
[hostel[].bookingHostelIdentifier.internalMasterIdentifier.id, hostel[].bookingHostelIdentifier.customerIdentifier.id]

person Sandro Rey    schedule 10.10.2019    source источник
comment
Что, если вы добавите internalMasterIdentifier и к каждой сущности? в полезной нагрузке (мне все равно, действительно это или нет, просто хочу знать, будет ли это работать)   -  person Antoniossss    schedule 10.10.2019


Ответы (1)


Поскольку поля internalMasterIdentifier и customerIdentifier не всегда присутствуют в bookingHostelIdentifier, вы должны пометить те поля или поля id, которые вложены под ними, как необязательные.

fieldWithPath("hostel[].bookingHostelIdentifier.internalMasterIdentifier.id").optional().description("The list of hostels"),
fieldWithPath("hostel[].bookingHostelIdentifier.customerIdentifier.id").optional().description("The list of hostels")
person Andy Wilkinson    schedule 11.10.2019