swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

swagger-ui not displaying details on some methods

Open gaffa opened this issue 13 years ago • 10 comments

Hi! I got this method:

    @RequestMapping(value = PING_RESOURCE_PATH, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(notes = "will return 'pong'", value = "ping")
    @ResponseBody
    public String ping() {
        return "pong";
    }

swagger-spring is generating this:

"path": "/rest/dev/ping",
        "description": "RESTful API for debugging operations",
        "operations": [
            {
                "httpMethod": "GET",
                "summary": "ping",
                "notes": "will return 'pong'",
                "deprecated": false,
                "responseClass": "String",
                "nickname": "ping"
            }
        ]

Swagger-ui will execute the request but not show response or information:

image

I have another method (error) that always returns a 400 and its working fine. Some other methods as well. Must have to do with the triviality :)

gaffa avatar Aug 22 '13 12:08 gaffa

Hi, I believe the issue is, you're telling swagger that you're going to produce content-type application/json but you're really returning a string. The proper fix is to set the @Produces annotation to include "text/plain", which be produced by the spring-mvc integration.

So I'd change your sample to return proper json.

fehguy avatar Aug 22 '13 20:08 fehguy

Well... You may be right about the slightly incorrect spring configuration but when you look at the json its expecting type String, no? Also the request is successfully executed and returning "pong". By the way: if I use an older version of swagger-ui its working flawlessly... Regards.

gaffa avatar Aug 23 '13 07:08 gaffa

The following is not valid JSON:

"pong"

The following is valid JSON:

{ "ping": "pong" }

However, the UI should be able to handle return values that are not JSON/XML. There needs to be a way to specify the return value as a simple string (content type of "text/plain") if there isn't already.

rage-shadowman avatar Aug 23 '13 17:08 rage-shadowman

...maybe the UI should treat all unknown "responseClass" values (those with no matching models) as text/plain?

rage-shadowman avatar Aug 23 '13 17:08 rage-shadowman

"pong" IS valid JSON it's just not an object

casualjim avatar Aug 23 '13 17:08 casualjim

I'm sorry but according to the spec you're right, just looked it up... http://www.json.org/

casualjim avatar Aug 23 '13 17:08 casualjim

The UI will definitely handle the text/plain response type. But if you tell it that it's json, it will fail to parse.

See here for an example of sending response type:

http://petstore.swagger.wordnik.com/api/api-docs/pet

produces: [
"application/json",
"application/xml"
]

Currently, swagger-ui supports json, xml, text and html responses.

fehguy avatar Aug 23 '13 18:08 fehguy

A String is valid Json I think. Spring/Jackson does not moan about it as well. The method response value is serialized by jackson and it does nothing to the string. I am also pretty sure that this worked before in previous swagger-ui versions.

gaffa avatar Aug 24 '13 07:08 gaffa

According to the website mentioned above, only arrays and objects are valid JSON (as a whole).

The array is a list of values. The object (hash table) contains mapped key/value pairs. Values can be strings, numbers, objects, arrays, booleans or nulls. Keys can only be strings.

Anything else that makes up a value (string, number, boolean, null) or anything that makes up a string or number is apparently not valid JSON (unless put together with other parts to make a valid JSON object or array).

rage-shadowman avatar Aug 28 '13 17:08 rage-shadowman

please reopen if you feel this is still an issue

fehguy avatar Sep 24 '13 13:09 fehguy