Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Swift3] Request body cannot be an array, only a dictionary #5480

Open
davidpasztor opened this issue Apr 25, 2017 · 5 comments
Open

[Swift3] Request body cannot be an array, only a dictionary #5480

davidpasztor opened this issue Apr 25, 2017 · 5 comments

Comments

@davidpasztor
Copy link
Contributor

Description

The request body cannot be an array of dictionaries, or in fact an array of any object, it can only be a dictionary (which can be a dictionary containing other dictionaries as values).
The issue comes from the Alamofire implementation that the request body is converted from the query parameters and the query parameters have to be a dictionary and cannot be an array.

Swagger-codegen version

2.2.2

Swagger declaration file content or url
/path/{to}/file:
    post:
      tags:
        - tag1
      operationId: something
      summary: summary
      description: 
        description
      parameters:
        - $ref: '#/parameters/param1'
        - $ref: '#/parameters/param2'
        - $ref: '#/parameters/param3'
        - name: postBody
          description: data to be posted in the body
          in: body
          required: true
          schema:
            type: array
            items:
              $ref: '#/definitions/Item1'
      responses:
        '201':
          description: Created
          schema:
            $ref: '#/definitions/Reply'

        '400':
          description: Bad request
          schema:
            $ref: '#/definitions/_Messages'
        default:
          description: Unexpected error
          schema:
            $ref: '#/definitions/_Messages'
      security:
        - basicAuth: []
Command line used for generation

swagger-codegen generate -i /path/spec.yaml -l swift3

Steps to reproduce

Generate a Swift3 client side code from a swagger spec that contains type: array in its schema for an object that is in:body.

Related issues

#2483
#4490

Suggest a Fix

The Swift file API.swift generated by swagger-codegen has the following method:

open class RequestBuilder<T> {
    var credential: URLCredential?
    var headers: [String:String]
    let parameters: [String:Any]?
    let isBody: Bool
    let method: String
    let URLString: String
    
    /// Optional block to obtain a reference to the request's progress instance when available.
    public var onProgressReady: ((Progress) -> ())?

    required public init(method: String, URLString: String, parameters: [String:Any]?, isBody: Bool, headers: [String:String] = [:]) {
        self.method = method
        self.URLString = URLString
        self.parameters = parameters
        self.isBody = isBody
        self.headers = headers
        
        addHeaders(SwaggerClientAPI.customHeaders)
    }
    
    open func addHeaders(_ aHeaders:[String:String]) {
        for (header, value) in aHeaders {
            headers[header] = value
        }
    }
    
    open func execute(_ completion: @escaping (_ response: Response<T>?, _ error: Error?) -> Void) { }

    public func addHeader(name: String, value: String) -> Self {
        if !value.isEmpty {
            headers[name] = value
        }
        return self
    }
    
    open func addCredential() -> Self {
        self.credential = SwaggerClientAPI.credential
        return self
    }
}

The problem comes from the fact that there is no body input parameter for the initialiser, only the isBody Bool, which makes it impossible �to have a query parameter and a request body of different types. This is an issue, since quite a few APIs expect an array of values as the body of the request, which is not possible with the current implementation.

@wing328
Copy link
Contributor

wing328 commented May 1, 2017

@davidpasztor thanks for reporting the issue. When you've time, I wonder if you can try the 2.3.0 branch to generate Swift3 API client, which contains some enhancements (breaking changes), to see if the same issue still occurs.

cc @jaz-ah @Edubits @jgavris @hexelon

@davidpasztor
Copy link
Contributor Author

@wing328 I have tried the 2.3.0 branch and sadly the issue still exists. The implementation of the function mentioned in my issue has not been changed and hence the issue has not been resolved in the 2.3.0 branch.

@yonaskolb
Copy link
Contributor

@davidpasztor I had this issue as well. To solve this and whole bunch of others I ended up writing a whole new generator in and for Swift here https://github.com/yonaskolb/SwagGen

@davidpasztor
Copy link
Contributor Author

@yonaskolb I finally managed to have a look at the code generated by SwagGen and it indeed looks like it solved this issue, however I had another issue with Swagger Codegen, which doesn't seem to be tackled by SwagGen either. For details please check #5452

@wing328
Copy link
Contributor

wing328 commented May 17, 2017

cc @RobBlairInq @jgavris @jaz-ah to review the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants