swagger-api / swagger-api/swagger-codegen

Response-data ignored for statuscodes > 299

Open
#5,659 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

General: Suggestion
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

When a request fails with e.g. a HTTP 400 and the backend delivers some error-information in the response, the generated Swagger-code will ignore the response-data and just always return the error it gets by Alamofire, showing "Invalid statuscode received".

It would be nice to have Swagger deliver the response-data into the Error-response if there is any.

Swagger-codegen version

2.2.2

Swagger declaration file content or url
responses:
   400:
      description: "Error in your parameters"
         schema:
            $ref: '#/definitions/Fault'

definitions:
   Fault:
      properties:
         Code:
            description: "ErrorCode"
            type: string
         Message
            description: "ErrorMessage"
            type: string
Suggest a Fix
if response.result.isFailure {               
	var error: Error = ErrorResponse.Error(response.response?.statusCode ?? 500, response.data, response.result.error!);
	if let responseData = response.data {
		if let jsonDictionary = try? JSONSerialization.jsonObject(with: responseData, options:[]) as? [String : Any] {
			error = NSError(domain: "backendError", code: response.response?.statusCode ?? 500, userInfo: jsonDictionary);
		}
	}
	completion(nil, error)
	return
}

This is the workaround i currently use: I check if there is anything in the response-data and if so I parse it to json. Now of course this can't be applied to the general as not everybody uses JSONs as response.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue concerns generated Swift client error handling through Alamofire; no source file or test is named. Start by locating the generated response path for HTTP status codes above 299 and compare the response body with the current ErrorResponse behavior. Done means backend error data is preserved without assuming every response is JSON.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.