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

Response example

Open ilopmar opened this issue 13 years ago • 5 comments

Hello,

I'm trying swagger-ui to document my Restful API and it is awesome, great work! :-)

I would like to know if it is possible to include a custom response example in a new section of the documentation. I ask this because I want to document my API with swagger-ui but I don't want that the users of the documentation can click on "Try it out!" button. I have removed the button but I would like to include in some way the response of the call (I mean Response Body, Response Code and Response Headers in the documentation.

Example. Note that I've added a new "exampleResponse" section

{
    "path": "/follow/{userId}",
    "description": "Operations about follow relations",
    "operations": [
        {
            "httpMethod": "POST",
            "summary": "Follow a user",
            "notes": "This can only be done by the logged in user.",
            "responseClass": "void",
            "nickname": "followUser",
            "parameters": [
                {
                    "name": "userId",
                    "description": "the id of the user to follow",
                    "paramType": "path",
                    "required": true,
                    "allowMultiple": false,
                    "dataType": "long"
                }
            ],
            "errorResponses": [
                {
                    "code": 400,
                    "reason": "Invalid username supplied"
                },
                {
                    "code": 404,
                    "reason": "User not found"
                }
            ],
// NEW BLOCK
            "exampleResponse": {
                "responseCode": 200
                "responseBody": {
                    HERE GOES MY CUSTOM JSON RESPONSE OR A LINK TO SOMETHING LIKE A MODEL BUT WITH DATA INSTEAD OF THE TYPE OF PARAMS
                }
            }
        }
    ]
}

Thanks and regards, Iván.

ilopmar avatar Apr 16 '13 08:04 ilopmar

You'll need to make changes to swagger.js in order to parse it (but it's probably a better idea to change the CoffeeScript and rebuilding it though).

And then add a view and handlebar in swagger-ui to display it.

nizhu avatar Apr 16 '13 08:04 nizhu

Thank you for the very quick response.

I think it would be easy for me to change directly the swagger.js file because I don't know very well neither javascript nor coffeescript.

Could you please send me some hints of what I have to do? I mean, "you have to create a new function to parse the new block. This function is like the existing one called functionXXXXXX. Now, you have to create the handlebars template, bind-it this way and done"

I really apreciated that.

Thank you again.

Regards, Iván.

ilopmar avatar Apr 16 '13 09:04 ilopmar

Did this a while ago, but I just grepped for 'errorResponse' and interpolated the code to my new parameter where I thought was necessary.

nick@nick-X58A-UD3R ~/github/swagger-ui $ grep -nr errorResponse lib/swagger.js 
260:          op = new SwaggerOperation(o.nickname, resource_path, o.httpMethod, o.parameters, o.summary, o.notes, o.responseClass, o.errorResponses, this, o.supportedContentTypes);
418:    function SwaggerOperation(nickname, path, httpMethod, parameters, summary, notes, responseClass, errorResponses, resource, supportedContentTypes) {
428:      this.errorResponses = errorResponses;
453:      this.errorResponses = this.errorResponses || [];

nick@nick-X58A-UD3R ~/github/swagger-ui/src/main $ grep -nr errorResponse *
coffeescript/view/OperationView.coffee:39:    @addStatusCode statusCode for statusCode in @model.errorResponses
template/operation.handlebars:46:                    {{#if errorResponses}}

nizhu avatar Apr 16 '13 09:04 nizhu

I suggest you look at the responseSampleJSON in the models section, which is supported in swagger.js already

fehguy avatar Apr 19 '13 19:04 fehguy

please reopen if still an issue

fehguy avatar Aug 02 '14 05:08 fehguy