Skip to content

Conversation

alxxndrv
Copy link

Motivation

OpenAPI operations that declare only a default response caused the generator to emit an accessor switch with an extra fallback branch that is statically unreachable, triggering a Swift compiler warning: Default will never be executed. The OpenAPI spec explicitly allows default as the only entry in the Responses Object, and tooling validates such specs. This change ensures generated code is clean and warning-free for these valid definitions. Fixes #810.

Result

For default-only outputs, the accessor is exhaustive and compiles without unreachable code warnings. Multi-case outputs are unchanged and continue to throw for mismatches. No changes to the public API surface or signatures of generated types

Modifications

Result

  • For default-only outputs, the accessor is exhaustive and compiles without unreachable code warnings
  • Multi-case outputs are unchanged and continue to throw for mismatches
  • No changes to the public API surface or signatures of generated types

Test Plan

  • Tests were modified to reflect the new accessor shape; additional checks needed

Links

Fixes #810, OpenAPI spec rule: https://spec.openapis.org/oas/v3.1.1.html#responses-object

…ly responses)

Fixes apple#810

OpenAPI operations that declare only a default response produced an accessor like:
switch self { case .default: return …; default: throw … }
Since the enum has a single case, the fallback was statically unreachable and triggered a compiler warning: “Default will never be executed.”

This change omits the fallback branch when the Output enum is provably single-case (default-only), eliminating the warning and keeping the accessor exhaustive. Accessor signatures remain throwing for source compatibility.

Details
- Generator: Gate the fallback default branch in [`TypesFileTranslator.translateResponseOutcomeInTypes()`](Sources/_OpenAPIGeneratorCore/Translator/Responses/translateResponseOutcome.swift:31) behind a single-case check (contains default and exactly one response outcome). Multi-case outputs remain unchanged and still throw on mismatches.
- Tests: Update default-only expected snippet to match the new, exhaustive switch without a fallback in [`SnippetBasedReferenceTests.swift`](Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift:3412). No new tests added.
Copy link
Contributor

@czechboy0 czechboy0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good, just one suggestion. Thanks @alxxndrv 🙏

@alxxndrv
Copy link
Author

Thanks @czechboy0! 🙏 I've committed the suggested change.

@alxxndrv alxxndrv requested a review from czechboy0 August 25, 2025 17:14
@czechboy0 czechboy0 added the 🔨 semver/patch No public API change. label Aug 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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