Swagger-codegen: [Swift] Support header parameter type

Created on 3 Oct 2016  路  9Comments  路  Source: swagger-api/swagger-codegen

Description

Swift code generator does not generate parameters described as "in": "header".

Swagger-codegen version

swagger-codegen version 2.2.0

Swagger declaration file content

I took
https://github.com/OAI/OpenAPI-Specification/blob/master/examples/v2.0/json/petstore-simple.json

and added X-CUSTOM-HEADER parameter in

"paths": {
    "/pets": {
        "get": {
             "description": "Returns all pets from the system that the user has access to",
             "operationId": "findPets",
...
"parameters": [
          {
            "name": "X-CUSTOM-HEADER",
            "in": "header",
            "type": "string"
          },

modified petstore-simple.json:
https://gist.github.com/andriuskl/d311cad766fdf04e78f1739bab8d014f

Generated code does not contain X-CUSTOM-HEADER as a request param

   public class func findPets(tags tags: [String]? = nil, limit: Int32? = nil, completion: ((data: [Pet]?, error: ErrorType?) -> Void)) 
Command line used for generation

swagger-codegen generate -i ~/Desktop/pets.json -l swift -o ~/Desktop/client

Swift Bug

All 9 comments

@andriuskl thanks for reporting the issue.

cc @jaz-ah @Edubits

@andriuskl I had a look at api.mustache for Swift and confirmed there's no logic to process header parameter at the moment: https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/swift/api.mustache

I can work with you to add that. Let me know if you've time to work on the fix together.

@wing328 , can point me to were that fix should go, I could try to fix.

@andriuskl if I understand correctly, we need to insert the code for handling header parameters here: https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/swift/api.mustache#L123

The header parameters should be stored in a dictionary and headers defined in the Swagger/OpenAPI spec can be obtained via the {{#headerParams}} ... {{/headerParams}} mustache tags. Here is an example:

{{#headerParams}}
if ({{paramName}} != null) localVarHeaderParams.Add("{{baseName}}", Configuration.ApiClient.ParameterToString({{paramName}})); // header parameter
{{/headerParams}}

Ref: https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/csharp/api.mustache#L246

Would these info be good enough as a starting point?

Despite not having logic in api.mustache to specifically handle header parameters via {{#headerParams}}...{{/headerParams}}, header parameters should at least show up in the parameter list for the method definition on account of the {{#allParams}}...{{/allParams}} tag. However, they are not. The problem is all HeaderParameters are being removed from the Operation's list of parameters:

https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SwiftCodegen.java#L495

https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/Swift3Codegen.java#L461

@abaruni I agree with you that the logic to remove header parameters should be removed.

May I know if you've time to contribute the enhancement? I really want to fix this problem as many APIs use header parameters but I'm no expert in Swift :(

@wing328 i think i can make some time in order to contribute the enhancement

https://github.com/swagger-api/swagger-codegen/pull/4678 has been created to resolve this issue

Closed via #4678. Thanks for the PR from @abaruni

Was this page helpful?
0 / 5 - 0 ratings