Swagger-codegen: [python] does not honour different response types for different return codes

Created on 24 Mar 2017  路  6Comments  路  Source: swagger-api/swagger-codegen

Description

When an API spec defines different response types for several status codes, the code generator does not seem to take this into account and tries to parse the response into a particular object of the model (I suspect that defined for status 200 or maybe the first one found) without checking the code first.

I have an API that returns some json on code 200 and a file in 201, so when I get a file, the generated client tries to parse it as the object and I end up not getting the file contents at all.

I have worked around this by using the _with_http_info calls and passing the _preload_content=False argument to it, but it is rather raw and it introduces a difference to how I check the responses.

Suggest a Fix

The response_type parameter to ApiClient.call_api could be a dictionary of status-code -> response_type. Then depending on the received status code, use one type or the other to process the response.

Python Feature help wanted

Most helpful comment

Is this still being worked on? For me getting proper error handling in my client code was one of the primary reasons for using swagger-codegen, I'm rather stunned to learn that this isn't actually implemented.

All 6 comments

If you can point me to where this is generated, I can give it a go

API classes in python client are generated from modules/swagger-codegen/src/main/resources/python/api.mustache, and api client template is in api_client.mustache. Its a bit problematic in this case - return type is passed to api client before sending the request and api client does the call and deserialises response based on response type given earlier.

It works same way in PHP client. Is it intended?

@jdevera @baartosz I don't think we support different response based on HTTP status code but it should not be difficult to do so by updating the api.mustache template.

PHP: https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/php/api.mustache
Python: https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/python/api.mustache

I would suggest adding this feature to 2.3.0 branch, which already has a lot of enhancements (breaking changes) to both PHP and Python client generators.

Please let me know if you've time to contribute the enhancement.

@wing328 I might get some time to look at this, yes.

Is this still being worked on? For me getting proper error handling in my client code was one of the primary reasons for using swagger-codegen, I'm rather stunned to learn that this isn't actually implemented.

Was this page helpful?
0 / 5 - 0 ratings