Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3XX responses not getting response objects. #2344

Open
javirln opened this issue Jul 8, 2020 · 3 comments
Open

3XX responses not getting response objects. #2344

javirln opened this issue Jul 8, 2020 · 3 comments
Labels

Comments

@javirln
Copy link

javirln commented Jul 8, 2020

Problem statement

Hello,

I'm facing an issue where I have a 302 response in one of my endpoints. The goal of that endpoint is to return a link to on the Location header in order to take it and use it to download its content.

While generating the client I see that no response type is assigned to that response therefore, I'm unable to access it's header to continue with other operations. I believe the Location header is well set as I'm exposing another headers and they are working properly.

I've also tried to trick the response by attaching an empty response object but, as expected it's not working either. If I add another response type, let's say 200 with a response body, it does create its response struct but, changing the status code it's not an option for my use case.

Is that the expected behaviour for 3XX responses, how can I access the Location header in this case or any other additional information I could add to it?

Thanks,

Swagger specification

"responses": {
  "302": {
    "description": "random description",
    "headers": {
      "Location": {
        "type": "string"
      }
    }
   }
  }
}

Steps to reproduce

Create a basic endpoint with a 302 response, try to access its content when calling the client.

Environment

swagger version: 0.23.0
go version: 1.14.2
OS: darwin/amd64

@casualjim
Copy link
Member

302 is used for redirection. The http client follows those by default and will end up in the final destination. You can configure what needs to happen with the CheckRedirect https://golang.org/pkg/net/http/#Client

Then our generated client treats anything that's not a 2xx response as an error, the populated structs are in the error type which you can use in a type switch to get to the more concrete types.

If redirect is not your intended use case, maybe 201 is a more appropriate it also has a Location header but is treated as a success response

@fredbi fredbi added the question label Jul 8, 2020
@javirln
Copy link
Author

javirln commented Jul 9, 2020

Thanks for you answer @casualjim

Our client is working as intended and it does follow redirections which I believe it's enabled by default in the http client.

The generated code is then working as I thought it will but then, another question comes to my mind, what happen if we want to access the response headers/metadata? Not only talking about 3XX but prospectively other status code since they can have custom headers exposed by a third-party services and specified in the swagger spec, for example AWS.

What I mean is that I'm not quite sure we can assume that non 2XX responses are errors and the fact that to access those actual successful responses, we need to cast an error.

@casualjim
Copy link
Member

casualjim commented Jul 18, 2020

But that's the HTTP spec though, so we should be able to assume that such a high level spec requirement holds.

1xx: info
2xx: success
3xx: redirect
4xx: client error
5xx: server error

If the header matters it should be in the specification because an undocumented header that is important is a gap in the contract. How would users know the header matters?
If the spec is in the contract, all the codes >= 400 appear in the error side, for each of those we've generated a struct and you can type switch on them to the concrete type and access the extra info like headers etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants