restdocs SnippetException due to HAL "_links" elements from spring-data-rest
Seems like the _links are always going to have that self-reference back to the same entity, right?
Yes, that's right.
I may have your solution for ignoring some links in a small github sample. Especially the part:
mockMvc.perform(RestDocumentationRequestBuilders.get(beerLocation)).andExpect(status().isOk()) .andDo(document("beer-get", links( linkWithRel("self").ignored(), linkWithRel("beerapi:beer").description("The <<beers, Beer resource>> itself"), linkWithRel("curies").ignored() ), responseFields( fieldWithPath("name").description("The name of the tasty fresh liquid"), fieldWithPath("_links").description("<<beer-links,Links>> to other resources") ) ));
where I completely ignore all "generated" fields and only create a documentation entry for the domain. Your item
link would be my beerapi:beer
.
I really don't know what is best practice here, but I would always document as much as possible since you can use asciidoctor links (like <<beer-links,Links>>
) wherever possible to reference other parts with more documentation.
canonical way seems to be using things from restdocs documentation. Approach is in line with approach from https://stackoverflow.com/users/2650436/meistermeier solution.
Documentation can be found on https://docs.spring.io/spring-restdocs/docs/current/reference/html5/#documenting-your-api-hypermedia-link-formats
Sample code:
.consumeWith(document("items", links( halLinks(), // <- this shorten things a bit linkWithRel("self").ignored(), linkWithRel("profile").ignored() ),