spring-projects / spring-restdocs

Test-driven documentation for RESTful services
https://spring.io/projects/spring-restdocs
Apache License 2.0
1.16k stars 736 forks source link

JSON Arrays are being documented as Objects #292

Closed sjimenez12 closed 8 years ago

sjimenez12 commented 8 years ago

I'm having an issue that started since we upgraded restdocs from 1.1.0.RELEASE to 1.1.1.RELEASE and are still having on 1.1.2 SNAPSHOT.

Basically I have a JSON response like this one:

[ {
  "id" : 86,
  "name" : "foo",
  "description" : "Descr",
  "Profile" : "Default Profile",
  "Groups" : [ "UserGroupName1113", "UserGroupName1114" ]
}, {
  "id" : 87,
  "name" : "gkGroup2CustGroupName1110",
  "description" : "foo bar",
  "Groups" : [ "UserGroupName1114" ]
} ]

and when I try to document it using the responsefields like so:

responseFields(
                    fieldWithPath("[]").type(JsonFieldType.ARRAY).description("This and That"),
                    fieldWithPath("[].id").type(JsonFieldType.NUMBER).description("Some text here"),
                    fieldWithPath("[].name").type(JsonFieldType.STRING).description("Name description").optional(),
                    fieldWithPath("[].description").type(JsonFieldType.STRING).description("This does that").optional(),
                    fieldWithPath("[].Profile").type(JsonFieldType.STRING).description("Some explanation"),
                    fieldWithPath("[].Groups").type(JsonFieldType.ARRAY).description("Blah blah"))

I get the following exception

org.springframework.restdocs.payload.FieldTypesDoNotMatchException: The documented type of the field '[]' is Array but the actual type is Object

and, as you may imagine, when I let restdocs infer the type, It'll show Object instead of Array on the snippet. Something like this:

|Path|Type|Description

|[]
|Object
|This and That
...
etc

As I said before, It worked beautifully on restdocs 1.1.0, but it seems like it broke on 1.1.1. Maybe something changed that I'm not aware of? I've read the entire restdocs document for 1.1.1 and can't seem to find a solution for this.

I really appreciate any help you can provide.

wilkinsona commented 8 years ago

Thank you for trying out a snapshot before opening an issue and for including lots of details.

As I said before, It worked beautifully on restdocs 1.1.0

As far as I can tell, that's not quite the case. In 1.1.0.RELEASE it actually gets the type of the field wrong (Object instead of Array). The difference is that 1.1.0 lets you override the incorrect type with your descriptor:

fieldWithPath("[]").type(JsonFieldType.ARRAY).description("This and That")

The thing that's changed is that 1.1.1 now throws an exception if you explicitly specify the type and it doesn't match what's actually there. That was changed in #276. The problem is that REST Docs is getting the type wrong for what's actually there.

wilkinsona commented 8 years ago

@sjimenez12 This should now be fixed in the latest 1.1.2 snapshot

sjimenez12 commented 8 years ago

First of all, thanks for such a blazing fast answer...and fix, it is completely and utterly appreciated!

As I said before, It worked beautifully on restdocs 1.1.0

Call me a strongly typed man XD, but I always declare the type explicitly, so I meant it worked perfectly for me, but I see the problem now ;)

Everything works as expected now, types are correctly documented, both when inferred and when explicitly declared

Again, thank you for your time and for this awesome project, keep up the good work!