Grails 3 - Spring Rest Docs using Rest assured giving SnippetException when using JSON views
Asked Answered
C

2

5

I am trying to integrate Spring REST docs with rest assured with Grails 3.1.4 application. I am using JSON Views.

Complete code is at https://github.com/rohitpal99/rest-docs

In NoteController when I use

List<Note> noteList = Note.findAll()
Map response = [totalCount: noteList.size(), type: "note"]
render response as grails.converters.JSON

Document generation works well.

But I want to use JSON views like

respond Note.findAll()

where I have _notes.gson and index.gson files in /views directory. I get a SnippetException. A usual /notes GET request response is correct.

rest.docs.ApiDocumentationSpec > test and document get request for /index FAILED
    org.springframework.restdocs.snippet.SnippetException at ApiDocumentationSpec.groovy:54

with no message. Unable to track why it occurs. Please suggest.

Full stacktrace

    org.springframework.restdocs.snippet.SnippetException: The following parts of the payload were not documented:
{
  "instanceList" : [ {
    "title" : "Hello, World!",
    "body" : "Integration Test from Hello"
  }, {
    "title" : "Hello, Grails",
    "body" : "Integration Test from Grails"
  } ]
}
    at org.springframework.restdocs.payload.AbstractFieldsSnippet.validateFieldDocumentation(AbstractFieldsSnippet.java:134)
    at org.springframework.restdocs.payload.AbstractFieldsSnippet.createModel(AbstractFieldsSnippet.java:74)
    at org.springframework.restdocs.snippet.TemplatedSnippet.document(TemplatedSnippet.java:64)
    at org.springframework.restdocs.generate.RestDocumentationGenerator.handle(RestDocumentationGenerator.java:192)
    at org.springframework.restdocs.restassured.RestDocumentationFilter.filter(RestDocumentationFilter.java:63)
    at com.jayway.restassured.internal.filter.FilterContextImpl.next(FilterContextImpl.groovy:73)
    at org.springframework.restdocs.restassured.RestAssuredRestDocumentationConfigurer.filter(RestAssuredRestDocumentationConfigurer.java:65)
    at com.jayway.restassured.internal.filter.FilterContextImpl.next(FilterContextImpl.groovy:73)
    at com.jayway.restassured.internal.RequestSpecificationImpl.applyPathParamsAndSendRequest(RequestSpecificationImpl.groovy:1574)
    at com.jayway.restassured.internal.RequestSpecificationImpl.get(RequestSpecificationImpl.groovy:159)
    at rest.docs.ApiDocumentationSpec.$tt__$spock_feature_0_0(ApiDocumentationSpec.groovy:54)
    at rest.docs.ApiDocumentationSpec.test and document get request for /index_closure2(ApiDocumentationSpec.groovy)
    at groovy.lang.Closure.call(Closure.java:426)
    at groovy.lang.Closure.call(Closure.java:442)
    at grails.transaction.GrailsTransactionTemplate$1.doInTransaction(GrailsTransactionTemplate.groovy:70)
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
    at grails.transaction.GrailsTransactionTemplate.executeAndRollback(GrailsTransactionTemplate.groovy:67)
    at rest.docs.ApiDocumentationSpec.test and document get request for /index(ApiDocumentationSpec.groovy)
Callicrates answered 5/6, 2016 at 12:40 Comment(4)
What's the full stack trace of the exception?Tip
Hi Andy, Thanks for reply. I have added complete trace, there not much info in SnippetException.Callicrates
Thanks, but that's the stacktrace from Gradle when it fails the build due to the failing test. It's the stacktrace from the test itself that will be useful. It should be available in the test reportTip
Sorry about that, updated with correct stacktrace.Callicrates
T
9

REST Docs will fail a test if you try to document something that isn't there or fail to document something that is there. You've documented two fields in your test:

responseFields(
    fieldWithPath('totalCount').description('Total count'),
    fieldWithPath('type').description("Type of result")
)))

REST Docs has failed the test as some parts of the response haven't been documented. Specifically an instanceList array that contains maps with two keys: title and body. You can document those and the other two fields with something like this:

responseFields(
    fieldWithPath('totalCount').description('Total count'),
    fieldWithPath('type').description("Type of result"),
    fieldWithPath('instanceList[].title').description('Foo'),
    fieldWithPath('instanceList[].body').description('Bar')
)))
Tip answered 5/6, 2016 at 20:19 Comment(0)
R
6

If you don't care about potentially missing fields, you can use relaxedResponseFields instead of responseFields:

relaxedResponseFields(
    fieldWithPath('totalCount').description('Total count'),
    fieldWithPath('type').description("Type of result")
))

This won't fail the test if some fields are not mentioned.

Reward answered 25/9, 2018 at 15:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.