Swagger-codegen: responseHeader annotations handling

Created on 20 Jul 2015  路  14Comments  路  Source: swagger-api/swagger-codegen

I believe, actually the responseHeader annotations can not be translated on code.

All 14 comments

@jeusdi Would you please elaborate on the issue ? is it related to spring-mvc ?

Hi @wing328.

Take a look at here.

As you can see, I've implemented two update operation, one on POST, and the other one on PUT.
The difference is I'm using POST verb to create a new resource on my system, and PUT to update it.

So, when it's created, I return the object id using the Location header, or I'd like to use another customized one, for example InstanceId. As you can see I'm using ResponseHeader annotation to describe this.

I'm generating a C# client, however, I don't know:

  • Is this information added on mustache objects?
  • How can issue this information on templates...

@jeusdi Currently, the response header is not used by the auto-generated API client. There's a way to get the HTTP response header from the last HTTP call via RestClient, would that meet your requirement?

@wing328 I am having the same issue, specifically with the generated Java client. I need to access the value of the Location header on POST. What would be the best way to move forward? Modify the generator so that all response objects implement an interface that has the headers and status code of the response?

@ivanmartinvalle For Java client, if we add a member variable (e.g. httpHeader) in ApiClient class to store the HTTP header of the last HTTP response, would that meet your requirement ?

@wing328 That would cause issues in a multi-threaded environment, but is probably fine for now, especially since it's probably best practice to spin up a new instance of a client per HTTP request anyway.

Adding a member variable of all HTTP response headers AND the HTTP status code (both are really important) would indeed meet my requirements.

Adding support for the entire HTTP Response standard ( http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html ) would be cool (more specifically, supporting the reason-phrase and HTTP-Version in addition to the aforementioned headers and status code).

@ivanmartinvalle yes, for other languages (e.g. PHP), we also recommend an instance of ApiClient per thread to avoid any potential multi-thread issue.

For Java, we're working on updating the template to support different version of Jersey library (and support other HTTP libraries in the future). We'll work on the support of HTTP response headers and status code after that.

Hi.
I have a proposal. Take a look at https://gist.github.com/evgenynacu/72c0d3300a4cadc3dca414910064a524
Especially, api.mustache, look for responseHeaders

Changed api.mustache and ApiClient.mustache to support response headers (these are files for Jersey2 Java version). I believe this can be implemented for any client.

I generate one more class encapsulating result object + response headers (when there are response headers).

Although, deserialization can be done in some other way, that's just quick solution.
This method doesn't cause multithreading issues and converts headers to needed types.

@evgenynacu the issue has been addressed in the latest master by introducing an additional method "WithHttpInfo" to return the status code and response headers.

some examples of generated code for better understanding:
https://gist.github.com/evgenynacu/43c167fcd455df5a1df99c546f97e1a3

in description.json you can see "X-Total-Count" header. And you can see it in callsUsingGETResponse generated class (private final Long xTotalCount)

@wing328 I am unable to find WithHttpInfo method in either ApiClient.mustache or api.mustache, could you point me to this?

Also, if using a new ApiClient for each request/thread is the way to go, why is an api class initialised to a singleton ApiClient instance? I am referring to Configuration.getDefaultApiClient()

@Heedster please use the okhttp-gson library for Java.

Depending on your use case, you can use the default ApiClient (singleton) if your program has nothing to do with multi-threading.

Is there an example for the result of codegen 2.2.1 and typescript-angular2?

@koppor please take a look at https://github.com/swagger-api/swagger-codegen/tree/master/samples/client/petstore/typescript-angular2/npm

I would recommend you opening a new ticket for your question moving forward.

Was this page helpful?
0 / 5 - 0 ratings