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

2019-07-12 16:15发布

问题:

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
标签: swagger