Skip to content

Commit

Permalink
Merge branch 'main' into hd-draft-multiple-content-types
Browse files Browse the repository at this point in the history
  • Loading branch information
czechboy0 committed Aug 1, 2023
2 parents 4b7015e + 81f8743 commit 8fac1e6
Show file tree
Hide file tree
Showing 17 changed files with 216 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ let package = Package(
// Read OpenAPI documents
.package(
url: "https://github.com/mattpolzin/OpenAPIKit.git",
exact: "3.0.0-alpha.7"
exact: "3.0.0-alpha.9"
),
.package(
url: "https://github.com/jpsim/Yams.git",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,19 @@ extension ClientFileTranslator {
.identifier(Constants.Operations.namespace)
.dot(description.methodName)

let operationArg: FunctionArgumentDescription = .init(
let operationArg = FunctionArgumentDescription(
label: "forOperation",
expression: operationTypeExpr.dot("id")
)
let inputArg: FunctionArgumentDescription = .init(
let inputArg = FunctionArgumentDescription(
label: "input",
expression: .identifier(Constants.Operation.Input.variableName)
)
let serializerArg: FunctionArgumentDescription = .init(
let serializerArg = FunctionArgumentDescription(
label: "serializer",
expression: try translateClientSerializer(description)
)
let deserializerArg: FunctionArgumentDescription = .init(
let deserializerArg = FunctionArgumentDescription(
label: "deserializer",
expression: try translateClientDeserializer(description)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ extension FileTranslator {
switch type {
case .allOf:
// AllOf uses all required properties.
propertyType = rawPropertyType
propertyType = rawPropertyType.withOptional(false)
case .anyOf:
// AnyOf uses all optional properties.
propertyType = rawPropertyType.asOptional
propertyType = rawPropertyType.withOptional(true)
}
let comment: Comment? = .property(
originalName: key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extension FileTranslator {
let knownKeys =
properties
.map(\.originalName)
let knownKeysFunctionArg: FunctionArgumentDescription = .init(
let knownKeysFunctionArg = FunctionArgumentDescription(
label: "knownKeys",
expression: .literal(
.array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ extension FileTranslator {
typeUsage = valueTypeUsage.asDictionaryValue
}

let extraProperty: PropertyBlueprint = .init(
let extraProperty = PropertyBlueprint(
comment: .doc("A container of undocumented properties."),
originalName: "additionalProperties",
typeUsage: typeUsage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ extension FileTranslator {
]
)
}
let unknownCase: SwitchCaseDescription = .init(
let unknownCase = SwitchCaseDescription(
kind: .default,
body: [
.expression(
Expand Down Expand Up @@ -126,7 +126,7 @@ extension FileTranslator {
]
)
}
let unknownCase: SwitchCaseDescription = .init(
let unknownCase = SwitchCaseDescription(
kind: .case(
.valueBinding(
kind: .let,
Expand All @@ -147,7 +147,7 @@ extension FileTranslator {
]
)

let variableDescription: VariableDescription = .init(
let variableDescription = VariableDescription(
accessModifier: config.access,
kind: .var,
left: "rawValue",
Expand Down Expand Up @@ -186,7 +186,7 @@ extension FileTranslator {
)
}

let enumDescription: EnumDescription = .init(
let enumDescription = EnumDescription(
isFrozen: true,
accessModifier: config.access,
name: typeName.shortSwiftName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extension FileTranslator {
userDescription: String?,
to existingTypeUsage: TypeUsage
) throws -> Declaration {
let typealiasDescription: TypealiasDescription = .init(
let typealiasDescription = TypealiasDescription(
accessModifier: config.access,
name: typeName.shortSwiftName,
existingType: existingTypeUsage.fullyQualifiedNonOptionalSwiftName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,20 @@ extension OperationDescription {
/// - components: The components from the OpenAPI document.
/// - asSwiftSafeName: A converted function from user-provided strings
/// to strings safe to be used as a Swift identifier.
/// - Throws: if `map` contains any references; see discussion for details.
///
/// This function will throw an error if `map` contains any references, because:
/// 1. OpenAPI 3.0.3 only supports external path references (cf. 3.1, which supports internal references too)
/// 2. Swift OpenAPI Generator currently only supports OpenAPI 3.0.x.
/// 3. Swift OpenAPI Generator currently doesn't support external references.
static func all(
from map: OpenAPI.PathItem.Map,
in components: OpenAPI.Components,
asSwiftSafeName: @escaping (String) -> String
) -> [OperationDescription] {
map.flatMap { path, value in
value.endpoints.map { endpoint in
) throws -> [OperationDescription] {
try map.flatMap { path, value in
let value = try value.resolve(in: components)
return value.endpoints.map { endpoint in
OperationDescription(
path: path,
endpoint: endpoint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ extension ClientFileTranslator {
])
)
)
let caseDesc: SwitchCaseDescription = .init(
let caseDesc = SwitchCaseDescription(
kind: .case(.dot(contentTypeIdentifier), ["value"]),
body: [
.expression(bodyAssignExpr)
Expand All @@ -210,7 +210,7 @@ extension ClientFileTranslator {
return caseDesc
}
if !requestBody.request.required {
let noneCase: SwitchCaseDescription = .init(
let noneCase = SwitchCaseDescription(
kind: .case(.dot("none")),
body: [
.expression(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,7 @@ extension ServerFileTranslator {
let responseVarDecl: Declaration = .variable(
kind: .var,
left: "response",
type: "Response",
right: .dot("init")
right: .identifier("Response")
.call([
.init(label: "statusCode", expression: statusCodeExpr)
])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,19 @@ extension ServerFileTranslator {
.identifier(Constants.Operations.namespace)
.dot(description.methodName)

let operationArg: FunctionArgumentDescription = .init(
let operationArg = FunctionArgumentDescription(
label: "forOperation",
expression: operationTypeExpr.dot("id")
)
let requestArg: FunctionArgumentDescription = .init(
let requestArg = FunctionArgumentDescription(
label: "request",
expression: .identifier("request")
)
let metadataArg: FunctionArgumentDescription = .init(
let metadataArg = FunctionArgumentDescription(
label: "with",
expression: .identifier("metadata")
)
let methodArg: FunctionArgumentDescription = .init(
let methodArg = FunctionArgumentDescription(
label: "using",
expression: .closureInvocation(
body: [
Expand All @@ -204,11 +204,11 @@ extension ServerFileTranslator {
]
)
)
let deserializerArg: FunctionArgumentDescription = .init(
let deserializerArg = FunctionArgumentDescription(
label: "deserializer",
expression: try translateServerDeserializer(description)
)
let serializerArg: FunctionArgumentDescription = .init(
let serializerArg = FunctionArgumentDescription(
label: "serializer",
expression: try translateServerSerializer(description)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,17 @@ struct TypesFileTranslator: FileTranslator {
+ config.additionalImports
.map { ImportDescription(moduleName: $0) }

let apiProtocol = translateAPIProtocol(doc.paths)
let apiProtocol = try translateAPIProtocol(doc.paths)

let serversDecl = translateServers(doc.servers)

let components = try translateComponents(doc.components)

let operationDescriptions =
OperationDescription
.all(
from: parsedOpenAPI.paths,
in: doc.components,
asSwiftSafeName: swiftSafeName
)
let operationDescriptions = try OperationDescription.all(
from: parsedOpenAPI.paths,
in: doc.components,
asSwiftSafeName: swiftSafeName
)
let operations = try translateOperations(operationDescriptions)

let typesFile = FileDescription(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ extension TypesFileTranslator {
/// per HTTP operation defined in the OpenAPI document.
/// - Parameter paths: The paths object from the OpenAPI document.
/// - Returns: A protocol declaration.
func translateAPIProtocol(_ paths: OpenAPI.PathItem.Map) -> Declaration {
/// - Throws: If `paths` contains any references.
func translateAPIProtocol(_ paths: OpenAPI.PathItem.Map) throws -> Declaration {

let operations = OperationDescription.all(
let operations = try OperationDescription.all(
from: paths,
in: components,
asSwiftSafeName: swiftSafeName
Expand All @@ -30,7 +31,7 @@ extension TypesFileTranslator {
operations
.map(translateAPIProtocolDeclaration(operation:))

let protocolDescription: ProtocolDescription = .init(
let protocolDescription = ProtocolDescription(
accessModifier: config.access,
name: Constants.APIProtocol.typeName,
conformances: Constants.APIProtocol.conformances,
Expand Down
8 changes: 6 additions & 2 deletions Tests/OpenAPIGeneratorCoreTests/Parser/Test_YamsParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,12 @@ final class Test_YamsParser: Test_Core {
description: Success
"""

let expected =
"/foo.yaml: error: Expected to find `responses` key for the **GET** endpoint under `/system` but it is missing."
let expected = """
/foo.yaml: error: Found neither a $ref nor a PathItem in Document.paths['/system'].
PathItem could not be decoded because:
Expected to find `responses` key for the **GET** endpoint under `/system` but it is missing..
"""
assertThrownError(try _test(yaml), expectedDiagnostic: expected)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import XCTest

final class Test_TextBasedRenderer: XCTestCase {

var renderer: TextBasedRenderer = .init()
var renderer = TextBasedRenderer()

func testComment() throws {
try _test(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ fileprivate extension UniversalServer where APIHandler: APIProtocol {
switch output {
case let .ok(value):
suppressUnusedWarning(value)
var response: Response = .init(statusCode: 200)
var response = Response(statusCode: 200)
suppressMutabilityWarning(&response)
try converter.setHeaderFieldAsText(
in: &response.headerFields,
Expand All @@ -152,7 +152,7 @@ fileprivate extension UniversalServer where APIHandler: APIProtocol {
return response
case let .`default`(statusCode, value):
suppressUnusedWarning(value)
var response: Response = .init(statusCode: statusCode)
var response = Response(statusCode: statusCode)
suppressMutabilityWarning(&response)
switch value.body {
case let .json(value):
Expand Down Expand Up @@ -219,7 +219,7 @@ fileprivate extension UniversalServer where APIHandler: APIProtocol {
switch output {
case let .created(value):
suppressUnusedWarning(value)
var response: Response = .init(statusCode: 201)
var response = Response(statusCode: 201)
suppressMutabilityWarning(&response)
try converter.setHeaderFieldAsJSON(
in: &response.headerFields,
Expand All @@ -241,7 +241,7 @@ fileprivate extension UniversalServer where APIHandler: APIProtocol {
return response
case let .badRequest(value):
suppressUnusedWarning(value)
var response: Response = .init(statusCode: 400)
var response = Response(statusCode: 400)
suppressMutabilityWarning(&response)
try converter.setHeaderFieldAsText(
in: &response.headerFields,
Expand Down Expand Up @@ -384,7 +384,7 @@ fileprivate extension UniversalServer where APIHandler: APIProtocol {
switch output {
case let .noContent(value):
suppressUnusedWarning(value)
var response: Response = .init(statusCode: 204)
var response = Response(statusCode: 204)
suppressMutabilityWarning(&response)
return response
case let .undocumented(statusCode, _): return .init(statusCode: statusCode)
Expand Down Expand Up @@ -441,12 +441,12 @@ fileprivate extension UniversalServer where APIHandler: APIProtocol {
switch output {
case let .noContent(value):
suppressUnusedWarning(value)
var response: Response = .init(statusCode: 204)
var response = Response(statusCode: 204)
suppressMutabilityWarning(&response)
return response
case let .badRequest(value):
suppressUnusedWarning(value)
var response: Response = .init(statusCode: 400)
var response = Response(statusCode: 400)
suppressMutabilityWarning(&response)
switch value.body {
case let .json(value):
Expand Down Expand Up @@ -517,7 +517,7 @@ fileprivate extension UniversalServer where APIHandler: APIProtocol {
switch output {
case let .ok(value):
suppressUnusedWarning(value)
var response: Response = .init(statusCode: 200)
var response = Response(statusCode: 200)
suppressMutabilityWarning(&response)
switch value.body {
case let .binary(value):
Expand All @@ -534,7 +534,7 @@ fileprivate extension UniversalServer where APIHandler: APIProtocol {
return response
case let .preconditionFailed(value):
suppressUnusedWarning(value)
var response: Response = .init(statusCode: 412)
var response = Response(statusCode: 412)
suppressMutabilityWarning(&response)
switch value.body {
case let .json(value):
Expand All @@ -551,7 +551,7 @@ fileprivate extension UniversalServer where APIHandler: APIProtocol {
return response
case let .internalServerError(value):
suppressUnusedWarning(value)
var response: Response = .init(statusCode: 500)
var response = Response(statusCode: 500)
suppressMutabilityWarning(&response)
switch value.body {
case let .text(value):
Expand Down
Loading

0 comments on commit 8fac1e6

Please sign in to comment.