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

Response headers not being displayed for either 200 or other responses

Open tlbdk opened this issue 11 years ago • 53 comments

Response headers not being displayed for either 200 or other results, error sample below shows the issue.

In the sample below the ETag header is defined for the parameters and for the 200 and 405 responses, but it's only being show for the parameters.

image

{
    "swagger": "2.0",
    "info": {
        "version": "1.0.0",
        "title": "Test sample showing swagger error",
        "description": "Show that response headers are not rendered",
        "contact": {
            "name": "Troels Liebe Bentsen",
            "email": "[email protected]"
        }
    },
    "host": "localhost",
    "basePath": "/",
    "schemes": [
        "http"
    ],
    "consumes": [
        "application/json"
    ],
    "produces": [
        "application/json"
    ],
    "paths": {
        "/test": {
            "post": {
                "summary": "Test",
                "description": "Test stuff",
                "operationId": "doTest",
                "parameters": [
                    {
                        "in": "body",
                        "name": "body",
                        "required": true,
                        "schema": {
                            "type": "array",
                            "items": {
                                "$ref": "#/definitions/test"
                            }
                        }                       
                    },
                    {
                        "in": "header",
                        "name": "ETag",
                        "description": "Used as caching key for future requests",
                        "type": "string"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Test stuff response",
                        "schema": {
                            "type": "array",
                            "items": {
                                "$ref": "#/definitions/test"
                            }
                        },
                        "headers": {
                            "ETag": {
                                "description": "Used as caching key for future requests",
                                "type": "string"
                            }
                        }
                    },
                    "405": {
                        "description": "Invalid input",
                        "headers": {
                            "ETag": {
                                "description": "Used as caching key for future requests",
                                "type": "string"
                            }
                        }
                    }
                },
                "tags": ["test"]
            }
        }
    },
    "definitions": {
        "test": {
            "required": [
                "test1"
            ],
            "properties": {
                "test1": {
                    "type": "string"
                }
            }
        }
    }
}

tlbdk avatar Apr 03 '15 07:04 tlbdk

I've encountered the same problem with 2.1.1 M2 and develop 2.0.

tsz662 avatar Apr 27 '15 13:04 tsz662

@mohsen1 - shouldn't this have been resolved with #930?

webron avatar Apr 27 '15 13:04 webron

@tlbdk, @tsz662 - can you retest with develop_2.0?

webron avatar May 04 '15 16:05 webron

Tested with develop_2.0 but no luck. I guess my swagger.json is pretty straightforward and it looks like this.

{
    "swagger": "2.0",
    "info": {
        "title": "Swagger demo",
        "description": "Some description here.",
        "version": "1.0.0"
    },
    "host": "localhost:8080",
    "schemes": [
        "http",
        "https"
    ],
    "basePath": "/api",
    "paths": {
        "/contacts": {
            "get": {
                "summary": "Get the Contact list.",
                "description": "Some description",
                "tags": [
                    "Contacts"
                ],
                "produces": [
                    "application/json"
                ],
                "responses": {
                    "200": {
                        "description": "Success",
                        "schema": {
                            "type": "array",
                            "items": {
                                "$ref": "#/definitions/Contact"
                            }
                        }
                    },
                    "404": {
                        "description": "No Contacts registered.",
                        "schema": {
                            "$ref": "#/definitions/ErrorInfo"
                        }
                    }
                }
            },
            "post": {
                "summary": "Creates a new Contact.",
                "description": "Returns the URL of the newly created Contact on success.",
                "tags": [
                    "Contacts"
                ],
                "parameters": [
                    {
                        "name": "X-Auth",
                        "in": "header",
                        "description": "Authentication token",
                        "required": true,
                        "type": "string"
                    },
                    {
                        "name": "X-CSRF",
                        "in": "header",
                        "description": "CSRF token",
                        "required": true,
                        "type": "string"
                    },
                    {
                        "name": "contact",
                        "in": "body",
                        "description": "Attributes of the Contact to create.",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/Contact"
                        }
                    }
                ],
                "responses": {
                    "201": {
                        "description": "Success",
                        "headers": {
                            "Location": {
                                "description": "URL of the new Contact.",
                                "type": "string"
                            }
                        },
                        "schema": {
                            "$ref": "#/definitions/Contact"
                        }
                    },
                    "400": {
                        "description": "Invalid request",
                        "schema": {
                            "$ref": "#/definitions/ErrorInfo"
                        }
                    }
                }
            }
        },
....

tsz662 avatar May 07 '15 00:05 tsz662

Please retest from develop_2.0. We just updated the swagger-js client. Reopen if still an issue...

fehguy avatar May 08 '15 21:05 fehguy

Re-tested with the latest develop_2.0 but still no headers rendered for either 2xx or 4xx responses. The Response Class section where successful responses are to be rendered does not seemingly have the place for headers in the first place, and the Response Messages section where other responses are to be rendered does not render headers.

tsz662 avatar May 10 '15 23:05 tsz662

This is not resolved.

webron avatar May 11 '15 08:05 webron

Can you tell us when v2.1.2 wil be released?

tsz662 avatar Aug 11 '15 10:08 tsz662

@tsz662 - that's not the right question. The right question is when will this be resolved. The 2.1.2 milestone is an estimate, not a guarantee.

And as always, PRs are welcome.

webron avatar Aug 11 '15 16:08 webron

I found a solution for this (old) problem. A comment in swagger UI indicates "getAllResponseHeaders sometimes falsely returns "" for CORS requests". So I found this:

http://stackoverflow.com/questions/14686769/xmlhttp-getresponseheader-not-working-for-cors

So all the headers that you allow with "Access-Control-Allow-Headers" you also have to expose with "Access-Control-Expose-Headers". I my case I'm using RestEasy in JBoss on the server side. I'm also adding a custom header line, that indicates the response time the server needed. So I had this line:

cres.getHeaders().add("Access-Control-Allow-Headers", "origin, content-type, accept, authorization, Response-Time-Millis");

and now I added this line additinally:

cres.getHeaders().add("Access-Control-Expose-Headers", "origin, content-type, accept, authorization, Response-Time-Millis");

does that make sense?

ar- avatar Aug 12 '15 08:08 ar-

Since this is not fixable in Swagger, but still an ugly usability bug, I would suggest, that the solution should be added as a hint to Swagger UI, like this: "The headers here can be incomplete for CORS requests, because ...... you have to ...... Access-Control-Allow-Headers and Access-Control-Expose-Headers ...... ".

ar- avatar Aug 12 '15 08:08 ar-

Maybe I'm missing something here... I thought the response headers are simply rendered based on the response header descriptions in the swagger.json (like in Swagger Editor).
Are they supposed to be rendered after successful communication with the server? (I mean, based on the actual headers returned by the server?)

If so, what are the response headers described in the swagger.json used for?

tsz662 avatar Aug 17 '15 10:08 tsz662

@tsz662 - documentation. You can also document an operation as returning Foo while it actually returns Bar. So the documentation would show Foo, but we'll still show you whatever the server returns (Bar in this case) when you try out the operation.

webron avatar Aug 17 '15 10:08 webron

@webron That makes sense for the Try It Out button, but it should display exactly what's in the json file for the documentation section, right?

I read all of the posts in this issue and all of the related issues shown above, but it's not clear to me if there's a way to work around this until a fix is released. Will I be able to make it work by following a procedure or do I need to wait for the official fix?

bandtank avatar Aug 17 '15 17:08 bandtank

@bandtank - yes, it should display it as part of the documentation, and this is what this feature request is for. I'm not entirely sure if the information is exposed by swagger-js, but it is, you could probably make it work (and submit a PR too in the process ;)).

webron avatar Aug 17 '15 19:08 webron

@bandtank Maybe you have overseen it. I have described a workaround/fix in my posting above. You can follow that. And there wont/can't be a fix for Swagger-UI. So you will always have to follow that.

The only thing to add to Swagger-UI is a description of the workaround on the UI.

ar- avatar Aug 18 '15 02:08 ar-

@ar- I have no idea how to implement your workaround as the details are too sparse. It's not even clear to me if it has anything to do with my problem or if the extra lines go in any of the swagger-ui files.

bandtank avatar Aug 18 '15 14:08 bandtank

@ar- Could you please be more specific about how to implement your workaround? Your post is hard to understand.

bandtank avatar Aug 25 '15 21:08 bandtank

There's no voting on GitHub, so here's me expressing interest in this getting fixed.

erikvanzijst avatar Sep 14 '15 23:09 erikvanzijst

+1 to a fix for this. Almost pulled my hair out thinking something was wrong with my JSON...

jakepeyser avatar Sep 27 '15 04:09 jakepeyser