Skip to content

Generated code for operation with default-only response has compiler warning 'Default will never be executed' #810

@bobergj

Description

@bobergj

Description

When an openapi spec operation has a responses object containing only a default field:
https://spec.openapis.org/oas/v3.1.1.html#fixed-fields-13
the generated code for that operation triggers a compiler warning.

The spec's aren't crystal clear on whether that is allowed:
https://spec.openapis.org/oas/v3.1.1.html#responses-object
https://spec.openapis.org/oas/v3.0.4.html#responses-object
Redocly CLI lint (a popular validation tool) accepts it though, and it seems a useful declaration for an operation that only ever performs a redirect.

Reproduction

# openapi.yaml
openapi: 3.0.4
info:
  title: My API
  version: 0.0.1
paths:
  /authorize:
    get:
      operationId: authorize
      summary: Initiate OpenID Connect (OIDC) login
      security: []
      responses:
        default:
          description: A redirect to the OIDC provider.
# openapi-generator-config.yaml
generate:
  - types
  - client
accessModifier: package
namingStrategy: idiomatic
# Types.swift
    package enum Authorize {
        package static let id: Swift.String = "authorize"
        package struct Input: Sendable, Hashable {
            /// Creates a new `Input`.
            package init() {}
        }
        @frozen package enum Output: Sendable, Hashable {
            package struct Default: Sendable, Hashable {
                /// Creates a new `Default`.
                package init() {}
            }
            /// A redirect to the OIDC provider.
            ///
            /// - Remark: Generated from `#/paths//authorize/get(authorize)/responses/default`.
            ///
            /// HTTP response code: `default`.
            case `default`(statusCode: Swift.Int, Operations.Authorize.Output.Default)
            /// The associated value of the enum case if `self` is `.`default``.
            ///
            /// - Throws: An error if `self` is not `.`default``.
            /// - SeeAlso: `.`default``.
            package var `default`: Operations.Authorize.Output.Default {
                get throws {
                    switch self {
                    case let .`default`(_, response):
                        return response
                    default: // <----------- WARNING: "Default will never be executed"
                        try throwUnexpectedResponseStatus(
                            expectedStatus: "default",
                            response: self
                        )
                    }
                }
            }
        }
    }

Package version(s)

    {
      "identity" : "swift-openapi-generator",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/apple/swift-openapi-generator",
      "state" : {
        "revision" : "bb9a13596af11db9bb83389295d91cd335810fe8",
        "version" : "1.10.2"
      }
    },
    {
      "identity" : "swift-openapi-runtime",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/apple/swift-openapi-runtime",
      "state" : {
        "revision" : "8f33cc5dfe81169fb167da73584b9c72c3e8bc23",
        "version" : "1.8.2"
      }
    },
    {
      "identity" : "swift-openapi-urlsession",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/apple/swift-openapi-urlsession",
      "state" : {
        "revision" : "6fac6f7c428d5feea2639b5f5c8b06ddfb79434b",
        "version" : "1.1.0"
      }
    },

Expected behavior

Generated code never triggers a compiler warning

Environment

swift -version
swift-driver version: 1.120.5 Apple Swift version 6.1.2 (swiftlang-6.1.2.1.2 clang-1700.0.13.5)
Target: arm64-apple-macosx15.0

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/generatorAffects: plugin, CLI, config file.kind/bugFeature doesn't work as expected.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions