Skip to content

Commit cf15973

Browse files
authored
Fix inline response headers (#355)
Fix inline response headers ### Motivation When generating a response header that points to an inline schema, the generator didn't generate the definition of the inline schema, so the code wouldn't compile. ### Modifications Make changes to properly compute optionality (drive-by improvement) and generate the inline type, if needed. ### Result Inline response header schemas now compile. ### Test Plan Added a snippet test for this. Reviewed by: simonjbeaumont Builds: ✔︎ pull request validation (5.10) - Build finished. ✔︎ pull request validation (5.8) - Build finished. ✔︎ pull request validation (5.9) - Build finished. ✔︎ pull request validation (compatibility test) - Build finished. ✔︎ pull request validation (docc test) - Build finished. ✔︎ pull request validation (integration test) - Build finished. ✔︎ pull request validation (nightly) - Build finished. ✔︎ pull request validation (soundness) - Build finished. #355
1 parent 9e62a21 commit cf15973

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

Sources/_OpenAPIGeneratorCore/Translator/Responses/TypedResponseHeader.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ extension FileTranslator {
129129
)
130130
}
131131
}
132-
let usage = type.withOptional(!header.required)
132+
let isOptional = try !header.required || typeMatcher.isOptional(schema, components: components)
133+
let usage = type.withOptional(isOptional)
133134
return .init(
134135
header: header,
135136
name: name,

Sources/_OpenAPIGeneratorCore/Translator/Responses/translateResponseHeader.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,21 @@ extension TypesFileTranslator {
2424
/// - Returns: A property blueprint.
2525
/// - Throws: An error if there's an issue while parsing the response header.
2626
func parseResponseHeaderAsProperty(for header: TypedResponseHeader, parent: TypeName) throws -> PropertyBlueprint {
27+
let schema = header.schema
28+
let typeUsage = header.typeUsage
29+
let associatedDeclarations: [Declaration]
30+
if TypeMatcher.isInlinable(schema) {
31+
associatedDeclarations = try translateSchema(typeName: typeUsage.typeName, schema: schema, overrides: .none)
32+
} else {
33+
associatedDeclarations = []
34+
}
2735
let comment = parent.docCommentWithUserDescription(header.header.description, subPath: header.name)
2836
return .init(
2937
comment: comment,
3038
originalName: header.name,
31-
typeUsage: header.typeUsage,
39+
typeUsage: typeUsage,
3240
default: header.header.required ? nil : .nil,
33-
associatedDeclarations: [],
41+
associatedDeclarations: associatedDeclarations,
3442
asSwiftSafeName: swiftSafeName
3543
)
3644
}

Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,6 +1673,41 @@ final class SnippetBasedReferenceTests: XCTestCase {
16731673
)
16741674
}
16751675

1676+
func testComponentsResponsesResponseWithInlineHeader() throws {
1677+
try self.assertResponsesTranslation(
1678+
"""
1679+
responses:
1680+
BadRequest:
1681+
description: Bad request
1682+
headers:
1683+
X-Reason:
1684+
schema:
1685+
type: string
1686+
enum:
1687+
- badLuck
1688+
""",
1689+
"""
1690+
public enum Responses {
1691+
public struct BadRequest: Sendable, Hashable {
1692+
public struct Headers: Sendable, Hashable {
1693+
@frozen public enum X_hyphen_ReasonPayload: String, Codable, Hashable, Sendable {
1694+
case badLuck = "badLuck"
1695+
}
1696+
public var X_hyphen_Reason: Components.Responses.BadRequest.Headers.X_hyphen_ReasonPayload?
1697+
public init(X_hyphen_Reason: Components.Responses.BadRequest.Headers.X_hyphen_ReasonPayload? = nil) {
1698+
self.X_hyphen_Reason = X_hyphen_Reason
1699+
}
1700+
}
1701+
public var headers: Components.Responses.BadRequest.Headers
1702+
public init(headers: Components.Responses.BadRequest.Headers = .init()) {
1703+
self.headers = headers
1704+
}
1705+
}
1706+
}
1707+
"""
1708+
)
1709+
}
1710+
16761711
func testComponentsResponsesResponseWithRequiredHeader() throws {
16771712
try self.assertResponsesTranslation(
16781713
"""

0 commit comments

Comments
 (0)