Swagger - Set the “response” based on “consumes”

筅森魡賤 提交于 2019-12-24 00:38:16

问题


Is it possible to specify the "response" based on the "consumes" option on Swagger?

My API can return "json" or "text" and I'd like that the "example value", when the response's status is 200 changes if the user selects from Response Content Type the option application/json or text/plain.

This is a piece of my swagger.json file:

{
    "swagger": "2.0",
    "produces": [
        "application/json",
        "text/plain"
    ],
    "consumes" : [
        "application\/json"
    ],
    "paths": {
        "/writer/1/": {
            "get": {
                "summary": "Get all writers",
                "description": "Writer description.",
                "tags": [
                    "writer"
                ],
                "responses": {
                    "200": {
                        "description" : "successful operation",
                        "schema" : {
                            "$ref" : "#\/definitions\/writerResponse"
                        }
                    },
                }
         }
    }
    "definitions": {
        "writerResponse": {
            "properties": {
                "meta": {
                    "type" :"object",
                    "schema" : {
                      "$ref" : "#\/definitions\/metaDefinition"
                    }
                },
                "data": {
                    "type" :"array",
                    "items": {
                        "$ref" : "#\/definitions\/writer"
                    }
                }
            }
        },
        "writer": {
            "properties": {
                "id": {
                    "type": "integer",
                    "description": "writer id.",
                    "example": "1477"
                },
                "short": {
                    "type": "string",
                    "description": "short description.",
                    "example": "short example"
                },
                "modified": {
                    "type": "string",
                    "description": "modified description.",
                    "example": "2016-05-21 22:58:36"
                }
            }                          
        },
    }

This is an example of the JSON output when user selects application/json:

{
    "meta": {
    "total": "1234",
    "last_page": "967",
    "per_page": "4000",
    "current_page": "1",
    "next_page_url": "http://localhost/api/<ws>/1?page=2",
    "prev_page_url": "http://localhost/api/<ws>/1?page=1",
    "from": "1",
    "to": "4000"
  },
  "data": [
    {
      "id": "1",
      "short": "TEST1",
      "modified": "2011-03-07 14:17:23"
    },
    {
      "id": "5",
      "short": "TEST2",
      "modified": "2015-05-26 12:39:45"
    }
  ]
}

And this is the output when the user selects text/plain:

id|short|modified
1|TEST1|2011-03-07 14:17:23
5|TEST2|2015-05-26 12:39:45

来源:https://stackoverflow.com/questions/38827278/swagger-set-the-response-based-on-consumes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!