swagger-api / swagger-api/swagger-codegen

[Swift 3/4/5] Security Definitions not always available in request builder methods

Open
#10,413 1 comment 0 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

For Swift clients and a given Swagger JSON/Yaml (e.g. PetStore) the Security Definitions seem to get ignored when the request builder methods get generated.

Swagger-codegen version

v. 3.0.20

Swagger declaration file content or url

http://petstore.swagger.io/v2/swagger.json

/store/inventory:
    get:
      tags:
      - "store"
      summary: "Returns pet inventories by status"
      description: "Returns a map of status codes to quantities"
      operationId: "getInventory"
      produces:
      - "application/json"
      ...
      security:
      - api_key: []
Command line used for generation

swagger-codegen generate -i http://petstore.swagger.io/v2/swagger.json -l swift4 -o test/

Steps to reproduce

I generated the code for Swift, ObjC and for Java. In ObjC and Java the authSettings were always present:

ObjC

///
/// Returns pet inventories by status
/// Returns a map of status codes to quantities
///  @returns NSDictionary<NSString*, NSNumber*>*
///
-(NSURLSessionTask*) getInventoryWithCompletionHandler: 
    (void (^)(NSDictionary<NSString*, NSNumber*>* output, NSError* error)) handler {
  
    // setting up headers, path and parameters
    ...

    // Authentication setting
    NSArray *authSettings = @[@"api_key"];    <-- here

    // further setup
    ...
    
    return [self.apiClient requestWithPath: resourcePath ...];
}

The generated Swift code for the same method doesn't reference any authentication settings:

Swift

    open class func getPetByIdWithRequestBuilder(petId: Int64) -> RequestBuilder<Pet> {
        var path = "/pet/{petId}"
        let petIdPreEscape = "\(petId)"
        let petIdPostEscape = petIdPreEscape.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? ""
        path = path.replacingOccurrences(of: "{petId}", with: petIdPostEscape, options: .literal, range: nil)
        let URLString = SwaggerClientAPI.basePath + path
        let parameters: [String:Any]? = nil
        
        let url = URLComponents(string: URLString)

        let requestBuilder: RequestBuilder<Pet>.Type = SwaggerClientAPI.requestBuilderFactory.getBuilder()

        return requestBuilder.init(method: "GET", URLString: (url?.string ?? URLString), parameters: parameters, isBody: false)
    }
Related issues/PRs
Suggest a fix/enhancement

Unfortunately no fix available. Maybe you can point me to the class responsible for its generation?

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 by reproducing the issue with the PetStore Swagger definition and the documented swagger-codegen command for swift4. Compare the generated Swift request builder with the ObjC and Java output, focusing on the reported absence of authentication settings. Done means Swift request builder methods include the security definition required by the operation.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.