From 16355908065796228c3311dc70fee15b3b821bc2 Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Tue, 31 Oct 2023 12:52:13 +0100 Subject: [PATCH] Fix inline response headers --- .../Responses/TypedResponseHeader.swift | 3 +- .../Responses/translateResponseHeader.swift | 12 +++++-- .../SnippetBasedReferenceTests.swift | 35 +++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/Sources/_OpenAPIGeneratorCore/Translator/Responses/TypedResponseHeader.swift b/Sources/_OpenAPIGeneratorCore/Translator/Responses/TypedResponseHeader.swift index d9e1c2da..3980dbfe 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/Responses/TypedResponseHeader.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/Responses/TypedResponseHeader.swift @@ -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, diff --git a/Sources/_OpenAPIGeneratorCore/Translator/Responses/translateResponseHeader.swift b/Sources/_OpenAPIGeneratorCore/Translator/Responses/translateResponseHeader.swift index 5779566b..55e49ff6 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/Responses/translateResponseHeader.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/Responses/translateResponseHeader.swift @@ -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 ) } diff --git a/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift b/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift index a595532c..8e235888 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift +++ b/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift @@ -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( """