9

私のアプリは と を使用spring-data-restしてspring-restdocsいます。私のセットアップは本当に標準的です。ドキュメントからほぼ完全にコピーされていますが、何か不足している場合に備えて、以下のサンプルを含めました. 私の mvc テストを実行すると、次のように失敗します。

org.springframework.restdocs.snippet.SnippetException: The following parts of the payload were not documented:
{
  "_links" : {
    "self" : {
      "href" : "https://my-api/item/10"
    },
    "item" : {
      "href" : "https://my-api/item/10"
    }
  }
}

これは私のテストコードです:

@Rule
public JUnitRestDocumentation restDocs = new JUnitRestDocumentation("target/generated-snippets");
// ...
mockMvc = webAppContextSetup(wac) //WebApplicationContext
        .apply(documentationConfiguration(restDocs)
                       .uris()
                       .withHost("my-api")
                       .withPort(443)
                       .withScheme("https"))
        .build();
// ....
mockMvc.perform(get("/items/{id}", "10"))
               .andDo(documentation)

スタックは次のとおりです。

at org.springframework.restdocs.payload.AbstractFieldsSnippet.validateFieldDocumentation(AbstractFieldsSnippet.java:176)
at org.springframework.restdocs.payload.AbstractFieldsSnippet.createModel(AbstractFieldsSnippet.java:100)
at org.springframework.restdocs.snippet.TemplatedSnippet.document(TemplatedSnippet.java:64)
at org.springframework.restdocs.generate.RestDocumentationGenerator.handle(RestDocumentationGenerator.java:196)
at org.springframework.restdocs.mockmvc.RestDocumentationResultHandler.handle(RestDocumentationResultHandler.java:55)
at org.springframework.test.web.servlet.MockMvc$1.andDo(MockMvc.java:177)
at com.example.my.api.domain.MyRepositoryRestTest.findOne(MyRepositoryRestTest.java:36)

上手にプレイするにはどうすればいいですspring-restdocsか?spring-data-rest


編集:

私のdocumentationインスタンスは次のように定義されています。

ResultHandler documentation = document("items/findOne",
                                       preprocessRequest(prettyPrint(), maskLinks()),
                                       preprocessResponse(prettyPrint()),
                                       responseFields(
                                            fieldWithPath("name").description("Item name.")
                                            // Bunch more
                                       ));

@meistermeier が示したように (そして、リンクを無視するための restdocs ドキュメントに従って、追加できます

links(linkWithRel("self").ignored(),
      linkWithRel("_self").ignored().optional()) // docs suggest this. /shrug

しかし、それでも私には次のことが残っています:

SnippetException: 次の関係を持つリンクは文書化されていませんでした: [item]

_links常に同じエンティティへの自己参照を持っているようですよね? 次のように、すべてのテストでエンティティ固有のリンクを無視せずに、これをきれいに処理するにはどうすればよいですか。

links(linkWithRel("item").ignored())

上記の行を追加しても (すべてのフィールドすべておよびself _self curies/またはになるように)、テストの結果はこの質問の上部にある元のエラーに戻ります。itemignored()optional()

4

2 に答える 2

8

_links は常に同じエンティティへの自己参照を持っているようですよね?

はい、そうです。

小さなgithubサンプルでいくつかのリンクを無視するための解決策があるかもしれません. 特にその部分:

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")
               )
            ));

「生成された」すべてのフィールドを完全に無視し、ドメインのドキュメント エントリのみを作成します。あなたのitemリンクは私のbeerapi:beer.

ここで何がベストプラクティスなのかは本当にわかりませんが、可能な限りasciidoctorリンク( など<<beer-links,Links>>)を使用して他の部分をより多くのドキュメントで参照できるため、常に可能な限りドキュメントを作成します。

于 2016-12-13T08:21:09.773 に答える