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

[Generator] Design away EncodableBodyContent #152

Merged
merged 4 commits into from
Jul 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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ let package = Package(
),

// Tests-only: Runtime library linked by generated code
.package(url: "https://github.com/apple/swift-openapi-runtime", .upToNextMinor(from: "0.1.3")),
.package(url: "https://github.com/apple/swift-openapi-runtime", .upToNextMinor(from: "0.1.6")),

// Build and preview docs
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,63 +175,60 @@ extension ClientFileTranslator {
requestVariableName: String,
inputVariableName: String
) throws -> Expression {
let contents = [requestBody.content]
var cases: [SwitchCaseDescription] = contents.map { typedContent in
let content = typedContent.content
let contentType = content.contentType
let contentTypeIdentifier = contentSwiftName(contentType)
let contentTypeHeaderValue = contentType.headerValueForSending

let typedContent = requestBody.content
let content = typedContent.content
let contentType = content.contentType
let contentTypeIdentifier = contentSwiftName(contentType)
let contentTypeHeaderValue = contentType.headerValueForSending

let transformReturnExpr: Expression = .return(
.dot("init")
.call([
.init(
label: "value",
expression: .identifier("value")
),
.init(
label: "contentType",
expression: .literal(contentTypeHeaderValue)
),
])
)
let caseDecl: SwitchCaseDescription = .init(
kind: .case(.dot(contentTypeIdentifier), ["value"]),
body: [
.expression(transformReturnExpr)
]
)
let transformExpr: Expression = .closureInvocation(
argumentNames: ["wrapped"],
body: [
.expression(
.switch(
switchedExpression: .identifier("wrapped"),
cases: [
caseDecl
]
)
let bodyAssignExpr: Expression = .assignment(
left: .identifier(requestVariableName).dot("body"),
right: .try(
.identifier("converter")
.dot(
"set\(requestBody.request.required ? "Required" : "Optional")RequestBodyAs\(contentType.codingStrategy.runtimeName)"
)
.call([
.init(label: nil, expression: .identifier("value")),
.init(
label: "headerFields",
expression: .inOut(
.identifier(requestVariableName).dot("headerFields")
)
),
.init(
label: "contentType",
expression: .literal(contentTypeHeaderValue)
),
])
)
]
)
return .assignment(
left: .identifier(requestVariableName).dot("body"),
right: .try(
.identifier("converter")
.dot(
"set\(requestBody.request.required ? "Required" : "Optional")RequestBodyAs\(contentType.codingStrategy.runtimeName)"
)
let caseDesc: SwitchCaseDescription = .init(
kind: .case(.dot(contentTypeIdentifier), ["value"]),
body: [
.expression(bodyAssignExpr)
]
)
return caseDesc
}
if !requestBody.request.required {
let noneCase: SwitchCaseDescription = .init(
kind: .case(.dot("none")),
body: [
.expression(
.assignment(
left: .identifier(requestVariableName).dot("body"),
right: .literal(.nil)
)
)
.call([
.init(label: nil, expression: .identifier(inputVariableName).dot("body")),
.init(
label: "headerFields",
expression: .inOut(
.identifier(requestVariableName).dot("headerFields")
)
),
.init(label: "transforming", expression: transformExpr),
])
]
)
cases.insert(noneCase, at: 0)
}
return .switch(
switchedExpression: .identifier(inputVariableName).dot("body"),
cases: cases
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,73 +306,66 @@ extension ServerFileTranslator {
}
codeBlocks.append(contentsOf: headerExprs.map { .expression($0) })

if let typedContent = try bestSingleTypedContent(
typedResponse.response.content,
inParent: bodyTypeName
) {
let contentTypeHeaderValue = typedContent.content.contentType.headerValueForValidation
let validateAcceptHeader: Expression = .try(
.identifier("converter").dot("validateAcceptIfPresent")
.call([
.init(label: nil, expression: .literal(contentTypeHeaderValue)),
.init(label: "in", expression: .identifier("request").dot("headerFields")),
])
let typedContents = [
try bestSingleTypedContent(
typedResponse.response.content,
inParent: bodyTypeName
)
codeBlocks.append(.expression(validateAcceptHeader))
]
.compactMap { $0 }

let contentType = typedContent.content.contentType
let switchContentCases: [SwitchCaseDescription] = [
.init(
if !typedContents.isEmpty {
let switchContentCases: [SwitchCaseDescription] = typedContents.map { typedContent in

var caseCodeBlocks: [CodeBlock] = []

let contentTypeHeaderValue = typedContent.content.contentType.headerValueForValidation
let validateAcceptHeader: Expression = .try(
.identifier("converter").dot("validateAcceptIfPresent")
.call([
.init(label: nil, expression: .literal(contentTypeHeaderValue)),
.init(label: "in", expression: .identifier("request").dot("headerFields")),
])
)
caseCodeBlocks.append(.expression(validateAcceptHeader))

let contentType = typedContent.content.contentType
let assignBodyExpr: Expression = .assignment(
left: .identifier("response").dot("body"),
right: .try(
.identifier("converter")
.dot("setResponseBodyAs\(contentType.codingStrategy.runtimeName)")
.call([
.init(label: nil, expression: .identifier("value")),
.init(
label: "headerFields",
expression: .inOut(
.identifier("response").dot("headerFields")
)
),
.init(
label: "contentType",
expression: .literal(contentType.headerValueForSending)
),
])
)
)
caseCodeBlocks.append(.expression(assignBodyExpr))

return .init(
kind: .case(.dot(contentSwiftName(contentType)), ["value"]),
body: [
.expression(
.return(
.dot("init")
.call([
.init(
label: "value",
expression: .identifier("value")
),
.init(
label: "contentType",
expression: .literal(contentType.headerValueForSending)
),
])
)
)
]
body: caseCodeBlocks
)
]
}

let transformExpr: Expression = .closureInvocation(
argumentNames: ["wrapped"],
body: [
.expression(
.switch(
switchedExpression: .identifier("wrapped"),
cases: switchContentCases
)
codeBlocks.append(
.expression(
.switch(
switchedExpression: .identifier("value").dot("body"),
cases: switchContentCases
)
]
)
let assignBodyExpr: Expression = .assignment(
left: .identifier("response").dot("body"),
right: .try(
.identifier("converter")
.dot("setResponseBodyAs\(contentType.codingStrategy.runtimeName)")
.call([
.init(label: nil, expression: .identifier("value").dot("body")),
.init(
label: "headerFields",
expression: .inOut(
.identifier("response").dot("headerFields")
)
),
.init(label: "transforming", expression: transformExpr),
])
)
)
codeBlocks.append(.expression(assignBodyExpr))
}

let returnExpr: Expression = .return(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,14 @@ public struct Client: APIProtocol {
name: "accept",
value: "application/json"
)
request.body = try converter.setRequiredRequestBodyAsJSON(
input.body,
headerFields: &request.headerFields,
transforming: { wrapped in
switch wrapped {
case let .json(value):
return .init(
value: value,
contentType: "application/json; charset=utf-8"
)
}
}
)
switch input.body {
case let .json(value):
request.body = try converter.setRequiredRequestBodyAsJSON(
value,
headerFields: &request.headerFields,
contentType: "application/json; charset=utf-8"
)
}
return request
},
deserializer: { response in
Expand Down Expand Up @@ -257,19 +252,15 @@ public struct Client: APIProtocol {
name: "accept",
value: "application/json"
)
request.body = try converter.setOptionalRequestBodyAsJSON(
input.body,
headerFields: &request.headerFields,
transforming: { wrapped in
switch wrapped {
case let .json(value):
return .init(
value: value,
contentType: "application/json; charset=utf-8"
)
}
}
)
switch input.body {
case .none: request.body = nil
case let .json(value):
request.body = try converter.setOptionalRequestBodyAsJSON(
value,
headerFields: &request.headerFields,
contentType: "application/json; charset=utf-8"
)
}
return request
},
deserializer: { response in
Expand Down Expand Up @@ -317,16 +308,14 @@ public struct Client: APIProtocol {
name: "accept",
value: "application/octet-stream, application/json, text/plain"
)
request.body = try converter.setRequiredRequestBodyAsBinary(
input.body,
headerFields: &request.headerFields,
transforming: { wrapped in
switch wrapped {
case let .binary(value):
return .init(value: value, contentType: "application/octet-stream")
}
}
)
switch input.body {
case let .binary(value):
request.body = try converter.setRequiredRequestBodyAsBinary(
value,
headerFields: &request.headerFields,
contentType: "application/octet-stream"
)
}
return request
},
deserializer: { response in
Expand Down
Loading