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] Multiple content types support #146

Merged
merged 11 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -256,7 +256,7 @@ public struct Client: APIProtocol {
try converter.setHeaderFieldAsText(
in: &request.headerFields,
name: "accept",
value: "application/json"
value: "application/json, text/plain, application/octet-stream"
czechboy0 marked this conversation as resolved.
Show resolved Hide resolved
)
return request
},
Expand All @@ -279,6 +279,24 @@ public struct Client: APIProtocol {
from: response.body,
transforming: { value in .json(value) }
)
} else if converter.isValidContentType(
received: contentType,
expected: "text/plain"
) {
body = try converter.getResponseBodyAsText(
Swift.String.self,
from: response.body,
transforming: { value in .text(value) }
)
} else if converter.isValidContentType(
received: contentType,
expected: "application/octet-stream"
) {
body = try converter.getResponseBodyAsBinary(
Foundation.Data.self,
from: response.body,
transforming: { value in .binary(value) }
)
} else {
throw converter.makeUnexpectedContentTypeError(contentType: contentType)
}
Expand Down Expand Up @@ -310,6 +328,18 @@ public struct Client: APIProtocol {
headerFields: &request.headerFields,
contentType: "application/json; charset=utf-8"
)
case let .text(value):
request.body = try converter.setRequiredRequestBodyAsText(
value,
headerFields: &request.headerFields,
contentType: "text/plain"
)
case let .binary(value):
request.body = try converter.setRequiredRequestBodyAsBinary(
value,
headerFields: &request.headerFields,
contentType: "application/octet-stream"
)
}
return request
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,26 @@ fileprivate extension UniversalServer where APIHandler: APIProtocol {
headerFields: &response.headerFields,
contentType: "application/json; charset=utf-8"
)
case let .text(value):
try converter.validateAcceptIfPresent(
"text/plain",
in: request.headerFields
)
response.body = try converter.setResponseBodyAsText(
value,
headerFields: &response.headerFields,
contentType: "text/plain"
)
case let .binary(value):
try converter.validateAcceptIfPresent(
"application/octet-stream",
in: request.headerFields
)
response.body = try converter.setResponseBodyAsBinary(
value,
headerFields: &response.headerFields,
contentType: "application/octet-stream"
)
}
return response
case let .undocumented(statusCode, _): return .init(statusCode: statusCode)
Expand Down Expand Up @@ -335,6 +355,24 @@ fileprivate extension UniversalServer where APIHandler: APIProtocol {
from: request.body,
transforming: { value in .json(value) }
)
} else if converter.isValidContentType(
received: contentType,
expected: "text/plain"
) {
body = try converter.getRequiredRequestBodyAsText(
Swift.String.self,
from: request.body,
transforming: { value in .text(value) }
)
} else if converter.isValidContentType(
received: contentType,
expected: "application/octet-stream"
) {
body = try converter.getRequiredRequestBodyAsBinary(
Foundation.Data.self,
from: request.body,
transforming: { value in .binary(value) }
)
} else {
throw converter.makeUnexpectedContentTypeError(contentType: contentType)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,8 @@ public enum Operations {
public var headers: Operations.getStats.Output.Ok.Headers
@frozen public enum Body: Sendable, Equatable, Hashable {
case json(Components.Schemas.PetStats)
case text(Swift.String)
case binary(Foundation.Data)
}
/// Received HTTP response body
public var body: Operations.getStats.Output.Ok.Body
Expand Down Expand Up @@ -1162,6 +1164,8 @@ public enum Operations {
public var cookies: Operations.postStats.Input.Cookies
@frozen public enum Body: Sendable, Equatable, Hashable {
case json(Components.Schemas.PetStats)
case text(Swift.String)
case binary(Foundation.Data)
}
public var body: Operations.postStats.Input.Body
/// Creates a new `Input`.
Expand Down