Skip to content

Commit ef2c033

Browse files
committed
Emit switch without fallback for single-case Output enums (default-only responses)
Fixes #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.
1 parent 340ee95 commit ef2c033

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

Sources/_OpenAPIGeneratorCore/Translator/Responses/translateResponseOutcome.swift

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,39 @@ extension TypesFileTranslator {
8787
staticMemberDecl = nil
8888
}
8989

90+
let isSingleCaseOutput = operation.containsDefaultResponse && operation.responseOutcomes.count == 1
91+
92+
var throwingGetterCases: [SwitchCaseDescription] = [
93+
SwitchCaseDescription(
94+
kind: .case(
95+
.dot(responseKind.identifier),
96+
responseKind.wantsStatusCode ? ["_", "response"] : ["response"]
97+
),
98+
body: [.expression(.return(.identifierPattern("response")))]
99+
)
100+
]
101+
if !isSingleCaseOutput {
102+
throwingGetterCases.append(
103+
SwitchCaseDescription(
104+
kind: .default,
105+
body: [
106+
.expression(
107+
.try(
108+
.identifierPattern("throwUnexpectedResponseStatus")
109+
.call([
110+
.init(
111+
label: "expectedStatus",
112+
expression: .literal(.string(responseKind.prettyName))
113+
),
114+
.init(label: "response", expression: .identifierPattern("self")),
115+
])
116+
)
117+
)
118+
]
119+
)
120+
)
121+
}
122+
90123
let throwingGetterDesc = VariableDescription(
91124
accessModifier: config.access,
92125
kind: .var,
@@ -96,31 +129,7 @@ extension TypesFileTranslator {
96129
.expression(
97130
.switch(
98131
switchedExpression: .identifierPattern("self"),
99-
cases: [
100-
SwitchCaseDescription(
101-
kind: .case(
102-
.dot(responseKind.identifier),
103-
responseKind.wantsStatusCode ? ["_", "response"] : ["response"]
104-
),
105-
body: [.expression(.return(.identifierPattern("response")))]
106-
),
107-
SwitchCaseDescription(
108-
kind: .default,
109-
body: [
110-
.expression(
111-
.try(
112-
.identifierPattern("throwUnexpectedResponseStatus")
113-
.call([
114-
.init(
115-
label: "expectedStatus",
116-
expression: .literal(.string(responseKind.prettyName))
117-
), .init(label: "response", expression: .identifierPattern("self")),
118-
])
119-
)
120-
)
121-
]
122-
),
123-
]
132+
cases: throwingGetterCases
124133
)
125134
)
126135
],

Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3416,11 +3416,6 @@ final class SnippetBasedReferenceTests: XCTestCase {
34163416
switch self {
34173417
case let .`default`(_, response):
34183418
return response
3419-
default:
3420-
try throwUnexpectedResponseStatus(
3421-
expectedStatus: "default",
3422-
response: self
3423-
)
34243419
}
34253420
}
34263421
}

0 commit comments

Comments
 (0)