Skip to content

Commit 0437e7e

Browse files
authored
Eliminate unreachable default branch in Output accessors for default-only responses (#816)
1 parent 0e6faec commit 0437e7e

File tree

2 files changed

+31
-37
lines changed

2 files changed

+31
-37
lines changed

Sources/_OpenAPIGeneratorCore/Translator/Responses/translateResponseOutcome.swift

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -87,43 +87,42 @@ extension TypesFileTranslator {
8787
staticMemberDecl = nil
8888
}
8989

90+
var throwingGetterCases: [SwitchCaseDescription] = [
91+
SwitchCaseDescription(
92+
kind: .case(
93+
.dot(responseKind.identifier),
94+
responseKind.wantsStatusCode ? ["_", "response"] : ["response"]
95+
),
96+
body: [.expression(.return(.identifierPattern("response")))]
97+
)
98+
]
99+
if !operation.containsDefaultResponse || operation.responseOutcomes.count > 1 {
100+
throwingGetterCases.append(
101+
SwitchCaseDescription(
102+
kind: .default,
103+
body: [
104+
.expression(
105+
.try(
106+
.identifierPattern("throwUnexpectedResponseStatus")
107+
.call([
108+
.init(
109+
label: "expectedStatus",
110+
expression: .literal(.string(responseKind.prettyName))
111+
), .init(label: "response", expression: .identifierPattern("self")),
112+
])
113+
)
114+
)
115+
]
116+
)
117+
)
118+
}
119+
90120
let throwingGetterDesc = VariableDescription(
91121
accessModifier: config.access,
92122
kind: .var,
93123
left: .identifierPattern(enumCaseName),
94124
type: .init(responseStructTypeName),
95-
getter: [
96-
.expression(
97-
.switch(
98-
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-
]
124-
)
125-
)
126-
],
125+
getter: [.expression(.switch(switchedExpression: .identifierPattern("self"), cases: throwingGetterCases))],
127126
getterEffects: [.throws]
128127
)
129128
let throwingGetterComment = Comment.doc(

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)