swagger-api / swagger-api/swagger-codegen

[Swift] unable to handle response header and statusCode

Open
#7,084 0 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

In swift template, generated source code only considered about response body as below, but we'd like to use not only body, but also header and statusCode in response.

open class func {{operationId}}({{#allParams}}{{paramName}}: {{#isEnum}}{{#isContainer}}{{{dataType}}}{{/isContainer}}{{^isContainer}}{{{datatypeWithEnum}}}_{{operationId}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{^required}}? = nil{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{#hasParams}}, {{/hasParams}}completion: @escaping ((_ {{#returnType}}data: {{{returnType}}}?,_ {{/returnType}}error: Error?) -> Void)) {
    {{operationId}}WithRequestBuilder({{#allParams}}{{paramName}}: {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}).execute { (response, error) -> Void in
        completion({{#returnType}}response?.body, {{/returnType}}error);
    }
}

This template will be generated as below.

open class func testClientModel(body: Client, completion: @escaping ((_ data: Client?,_ error: Error?) -> Void)) {
  testClientModelWithRequestBuilder(body: body).execute { (response, error) -> Void in
    completion(response?.body, error);
  }
}
Swagger-codegen version

2.3.0-SNAPSHOT

Swagger declaration file content or url
Command line used for generation
Steps to reproduce
Related issues/PRs
Suggest a fix/enhancement

To keep codegen more generic, we should change mustache as below. (change response?.body to response )

    open class func {{operationId}}({{#allParams}}{{paramName}}: {{#isEnum}}{{#isContainer}}{{{dataType}}}{{/isContainer}}{{^isContainer}}{{{datatypeWithEnum}}}_{{operationId}}{{/isContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{^required}}? = nil{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}{{#hasParams}}, {{/hasParams}}completion: @escaping ((_ {{#returnType}}data: Response<{{{returnType}}}>?,_ {{/returnType}}error: Error?) -> Void)) {
        {{operationId}}WithRequestBuilder({{#allParams}}{{paramName}}: {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}).execute { (response, error) -> Void in
            completion({{#returnType}}response, {{/returnType}}error);
        }
    }

PromiseKit and RxSwift are also followed with above.

As follow my suggestion, the output will be as below.

    open class func testClientModel(body: Client, completion: @escaping ((_ data: Response<Client>?,_ error: Error?) -> Void)) {
        testClientModelWithRequestBuilder(body: body).execute { (response, error) -> Void in
            completion(response, error);
        }
    }

//usage:
API.testClientModel(body: client)
  .subscribe(onNext: { response in
     print(response.body) // T? (generated as Model)
     print(response.header) // [String : String]
     print(response.statusCode)
  }, onError: { error in
    print(error.localizedDescription)
  }).disposed(by: disposeBag)

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

Start with the Swift template that generates the completion callback and compare its current response?.body handling with the proposed Response<...> return. Check the corresponding PromiseKit and RxSwift output mentioned in the issue. Done means generated Swift clients expose the response body, headers, and status code rather than only the body.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.