swagger-api / swagger-api/swagger-codegen

[swift5] Swift compiler warning, cast always fails

Open
#10,247 3 comments 1 reaction 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 all our generated swift5-clients we get the following method that causes a warning:

    public convenience init(response: HTTPURLResponse, body: T?) {
        let rawHeader = response.allHeaderFields
        var header = [String:String]()
        for case let (key, value) as (String, String) in rawHeader {
            header[key] = value
        }
        self.init(statusCode: response.statusCode, header: header, body: body)
    }
Swift Compiler Warning:
swift5-client/SwaggerClient/Classes/Swaggers/Models.swift:31:35:
Cast from 'Dictionary<AnyHashable, Any>.Element'
(aka '(key: AnyHashable, value: Any)')
to unrelated type '(String, String)' always fails
Swagger-codegen version

macOS, homebrew, swagger-codegen version 3.0.18

Swagger declaration file content or url
openapi: 3.0.0
info:
  title: Thingies App
  version: "1"
  description: |+
    ...

paths:
  /thingies/load_all:
    post:
      responses:
        200:
          description: Load all thingies
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Thingies"
servers:
  - url: https://thingies.test/api
components:
  schemas:
    Thingies:
      type: object
      properties:
        property_one:
          type: string
Command line used for generation
swagger-codegen generate -l swift5 -i "${YAML_FILE}" -o "${SWIFT_PATH}"
Suggest a fix/enhancement

The line that causes the warning is this. Not familiar enough with this construct to fix it:

        for case let (key, value) as (String, String) in rawHeader {

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

Locate the swift5 client template that emits the HTTPURLResponse initializer and inspect how the raw headers are iterated. Run the provided swagger-codegen command with the sample OpenAPI document, then verify the generated Swift client no longer produces the reported cast warning.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.