Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inline response headers #355

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ extension FileTranslator {
)
}
}
let usage = type.withOptional(!header.required)
let isOptional = try !header.required || typeMatcher.isOptional(schema, components: components)
let usage = type.withOptional(isOptional)
return .init(
header: header,
name: name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,21 @@ extension TypesFileTranslator {
/// - Returns: A property blueprint.
/// - Throws: An error if there's an issue while parsing the response header.
func parseResponseHeaderAsProperty(for header: TypedResponseHeader, parent: TypeName) throws -> PropertyBlueprint {
let schema = header.schema
let typeUsage = header.typeUsage
let associatedDeclarations: [Declaration]
if TypeMatcher.isInlinable(schema) {
associatedDeclarations = try translateSchema(typeName: typeUsage.typeName, schema: schema, overrides: .none)
} else {
associatedDeclarations = []
}
let comment = parent.docCommentWithUserDescription(header.header.description, subPath: header.name)
return .init(
comment: comment,
originalName: header.name,
typeUsage: header.typeUsage,
typeUsage: typeUsage,
default: header.header.required ? nil : .nil,
associatedDeclarations: [],
associatedDeclarations: associatedDeclarations,
asSwiftSafeName: swiftSafeName
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1673,6 +1673,41 @@ final class SnippetBasedReferenceTests: XCTestCase {
)
}

func testComponentsResponsesResponseWithInlineHeader() throws {
try self.assertResponsesTranslation(
"""
responses:
BadRequest:
description: Bad request
headers:
X-Reason:
schema:
type: string
enum:
- badLuck
""",
"""
public enum Responses {
public struct BadRequest: Sendable, Hashable {
public struct Headers: Sendable, Hashable {
@frozen public enum X_hyphen_ReasonPayload: String, Codable, Hashable, Sendable {
case badLuck = "badLuck"
}
public var X_hyphen_Reason: Components.Responses.BadRequest.Headers.X_hyphen_ReasonPayload?
public init(X_hyphen_Reason: Components.Responses.BadRequest.Headers.X_hyphen_ReasonPayload? = nil) {
self.X_hyphen_Reason = X_hyphen_Reason
}
}
public var headers: Components.Responses.BadRequest.Headers
public init(headers: Components.Responses.BadRequest.Headers = .init()) {
self.headers = headers
}
}
}
"""
)
}

func testComponentsResponsesResponseWithRequiredHeader() throws {
try self.assertResponsesTranslation(
"""
Expand Down