Spring REST Docs - детали не документированы: - PullRequest
0 голосов
/ 10 октября 2019

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

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

{
  "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]

1 Ответ

0 голосов
/ 11 октября 2019

Поскольку поля 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")
...