Skip to content

Commit

Permalink
Fix inline response headers (#355)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
czechboy0 authored Oct 31, 2023
1 parent 9e62a21 commit cf15973
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
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

0 comments on commit cf15973

Please sign in to comment.