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

[Swift] generated ClientSDK - excute callback response type

Open junyun0516 opened this issue 10 years ago • 19 comments

Are using Swagger codegen, when the execute, request to API (in my case UserAPI) looks like can only model that is mapped to. There is no problem if http code is 200, when receiving the other StatusCode, if the model is the return comes to the other model (in my case Error) could only given model when you request excute it troubled they become. Do you not allow you to specify each type made of a excute? Well I want to use. help me.

junyun0516 avatar Feb 11 '16 03:02 junyun0516

Hi @wing328,

@junyun0516 works with me and additional phenomenon would be help. Our YAML file includes below:

  responses:
    '200':
      description: User info
      schema:
        $ref: '#/definitions/User'
    '400':
      description: Error
      schema:
        $ref: '#/definitions/Error'

definitions: Error: type: object required: - reason properties: reason: type: string User: type: object required: - id properties: id: type: integer format: int64 api_token: type: string

Swagger Client for Swift works well when sever returns with status code 200, but when returns 400, client seems can't get Error object. I don't have experience with Swift so this would be problem of own code, but @junyun0516 is in trouble with client.

Ruby or Python clients work well with above YAML when server returns 400 or others like as response object contains status code and response body.

Is there any way to handling return object following status code?

Best Regards, Inska

inska avatar Feb 11 '16 03:02 inska

@junyun0516 would suggest you guys to pull the latest master if you guys have not done so as recently there are a few PRs to enhancement the Swift client.

wing328 avatar Feb 11 '16 05:02 wing328

@wing328, just pulled master, run "mvn install", and re-generate swift client. However, above situation not resolved..

inska avatar Feb 11 '16 05:02 inska

@inska thanks for performing more tests with the latest master. I'm afraid more works need to be done to improve the exceptions handling in Swift client.

One suggestion is to create an ApiException object (which extends the basic Exception object) in Swift to capture the HTTP status code, header, body, etc similar to how other languages capture exceptions (4xx, 5xx):

  • https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/php/SwaggerClient-php/lib/ApiException.php
  • https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/java/default/src/main/java/io/swagger/client/ApiException.java

(not sure if Swift has similar mechanism for error handling)

cc @jaz-ah who has recently made a lot of contribution to the Swift client.

wing328 avatar Feb 11 '16 09:02 wing328

yeah the error handling isn't too great - right now I have added code in my local branch to AlamofireImplementations.swift right after request.responseJSON that grabs any error > 400 and returns that in the promise's error - you don't get any custom error objects but at least you get an NSError that describes the url/statuscode that hit the error...

        // AlamoFire doesn't automatically check status responses for error codes
        // so we do it manually here - also we could call validate() on the AlamoFire
        // request but it doesn't put the status code in the error message
        // which isn't too helpful in the client code
        if request.response?.statusCode >= 400 {
            let error = NSError(domain: (request.response?.URL?.URLString)!, code: (request.response?.statusCode)!, userInfo: nil)
            error.log()
            completion(response: nil, error: error)
            return
        }

this still doesn't solve the custom error handling though - but that would be the place to modify the code to add it...

jaz-ah avatar Feb 11 '16 18:02 jaz-ah

@jaz-ah do you mind submitting a PR for the solution you proposed?

wing328 avatar Mar 19 '16 10:03 wing328

@wing328 sure I can push a change in this week.

jaz-ah avatar Mar 19 '16 23:03 jaz-ah

Any news on this, not being able to handle errors gracefully is a major flaw in the generated client in my opinion. Is any work done on improving handling error-responses from APIs, or are you looking for contributions for this matter?

grEvenX avatar Sep 24 '16 09:09 grEvenX

sorry guys - it looks like some error handling has been added recently, so after the Swift3 merge let's look at what we have and we can augment that.

jaz-ah avatar Sep 26 '16 16:09 jaz-ah

Hi @jaz-ah, are there any in-progress branches that are working on addressing this issue that I could look at for inspiration for a temporary solution? Thanks!

normand1 avatar Oct 31 '16 21:10 normand1

@normand1 ah sorry to just get back here - could you try the swift3 language - that has some better work for this....

jaz-ah avatar Oct 31 '16 21:10 jaz-ah

@jaz-ah thanks, I'll give it a shot

normand1 avatar Oct 31 '16 22:10 normand1

@normand1 may I know if you've a chance to test the Swift3 API client?

wing328 avatar Feb 15 '17 14:02 wing328

Hi @wing328, the problem still exists! I tested Swift3 API client. generated error is enum public enum ErrorResponse : Error { case Error(Int, Data?, Error) }

but it is not possible to get data from this error there are only possibility to get status code from this answer https://github.com/swagger-api/swagger-codegen/issues/4190

navinov avatar Apr 29 '17 10:04 navinov

I would suggest you to test with the Swift3 API client in 2.3.0 branch as well (which contains breaking changes)

On Sat, Apr 29, 2017 at 6:48 PM, inovik [email protected] wrote:

Hi @wing328 https://github.com/wing328, the problem still exists! I tested Swift3 API client. generated error is enum `public enum ErrorResponse : Error {

case Error(Int, Data?, Error)

}`

but it is not possible to get data from this error there are only possibility to get status code from this answer #4190 https://github.com/swagger-api/swagger-codegen/issues/4190

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/swagger-api/swagger-codegen/issues/2110#issuecomment-298161617, or mute the thread https://github.com/notifications/unsubscribe-auth/AA5BdABahAlKpb3R5-6XWrU0jLkPdA1lks5r0xWEgaJpZM4HX4S8 .

wing328 avatar Apr 29 '17 11:04 wing328

You will need clone the repo, checkout 2.3.0 branch and build the JAR locally.

On Apr 29, 2017 8:00 PM, "inovik" [email protected] wrote:

Can you please help me install version 2.3.0? I use OSX and when I try call brew install swagger-codegen installed version is 2.2.2 also I try call brew install swagger-codegen 2.3.0 -> Error: No formulae found in taps.

От 29 апреля 2017 г. в 14:08:58, wing328 ([email protected]) написал:

I would suggest you to test with the Swift3 API client in 2.3.0 branch as well (which contains breaking changes)

On Sat, Apr 29, 2017 at 6:48 PM, inovik [email protected] wrote:

Hi @wing328 https://github.com/wing328, the problem still exists! I tested Swift3 API client. generated error is enum `public enum ErrorResponse : Error {

case Error(Int, Data?, Error)

}`

but it is not possible to get data from this error there are only possibility to get status code from this answer #4190 https://github.com/swagger-api/swagger-codegen/issues/4190

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub < https://github.com/swagger-api/swagger-codegen/issues/ 2110#issuecomment-298161617 , or mute the thread < https://github.com/notifications/unsubscribe-auth/AA5BdABahAlKpb3R5- 6XWrU0jLkPdA1lks5r0xWEgaJpZM4HX4S8

.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub <https://github.com/swagger-api/swagger-codegen/issues/ 2110#issuecomment-298162491>, or mute the thread <https://github.com/notifications/unsubscribe-auth/ AGbuOOBoToRehotJy6xyChHsN5JFm2iTks5r0xpJgaJpZM4HX4S8> .

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/swagger-api/swagger-codegen/issues/2110#issuecomment-298164668, or mute the thread https://github.com/notifications/unsubscribe-auth/AA5BdFmiCXSFHDDUv_u4cizDHhNowAAjks5r0yZIgaJpZM4HX4S8 .

wing328 avatar Apr 29 '17 12:04 wing328

I am also having the same issue. How do I get the Data from ErrorResponse?

akanksharma avatar Aug 08 '17 09:08 akanksharma

You can use switch operator like in this answer https://github.com/swagger-api/swagger-codegen/issues/4190, but a little different way:

 if let err = error as? ErrorResponse {

    switch(err){

    case .Error(409, let dataValue, _):

        //now there are possibility to use dataValue object

    break

}

navinov avatar Aug 08 '17 09:08 navinov

You answer helped me a lot, I just had to change a few things for my swift3 generated api, mainly because I don't have the .Error enum case but HttpError(statusCode: Int, data: Data?, error: Error) :

if error == nil {
	print("success")
} else {
	switch(error!){
	case .HttpError(statusCode: 404, data: _, error: _):
		print("not found")
		break
	default:
		print("other error")
	}
}

nmDejamobile avatar Sep 14 '17 08:09 nmDejamobile