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

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

Open machasamurai opened this issue 5 years ago • 1 comments

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?

machasamurai avatar Jul 29 '20 08:07 machasamurai

Hey ~2 years later I'm running into the same thing. Were there any updates here?

ltsuchiya avatar Sep 19 '22 22:09 ltsuchiya