Skip to content

Commit 371f8fe

Browse files
Update to OpenAPIKit 3.0.0-alpha.9
Signed-off-by: Si Beaumont <beaumont@apple.com>
1 parent 7245a77 commit 371f8fe

File tree

6 files changed

+85
-16
lines changed

6 files changed

+85
-16
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ let package = Package(
6464
// Read OpenAPI documents
6565
.package(
6666
url: "https://github.com/mattpolzin/OpenAPIKit.git",
67-
exact: "3.0.0-alpha.7"
67+
exact: "3.0.0-alpha.9"
6868
),
6969
.package(
7070
url: "https://github.com/jpsim/Yams.git",

Sources/_OpenAPIGeneratorCore/Translator/Operations/OperationDescription.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,20 @@ extension OperationDescription {
5959
/// - components: The components from the OpenAPI document.
6060
/// - asSwiftSafeName: A converted function from user-provided strings
6161
/// to strings safe to be used as a Swift identifier.
62+
/// - Throws: if `map` contains any references; see discussion for details.
63+
///
64+
/// This function will throw an error if `map` contains any references, because:
65+
/// 1. OpenAPI 3.0.3 only supports external path references (cf. 3.1, which supports internal references too)
66+
/// 2. Swift OpenAPI Generator currently only supports OpenAPI 3.0.x.
67+
/// 3. Swift OpenAPI Generator currently doesn't support external references.
6268
static func all(
6369
from map: OpenAPI.PathItem.Map,
6470
in components: OpenAPI.Components,
6571
asSwiftSafeName: @escaping (String) -> String
66-
) -> [OperationDescription] {
67-
map.flatMap { path, value in
68-
value.endpoints.map { endpoint in
72+
) throws -> [OperationDescription] {
73+
try map.flatMap { path, value in
74+
let value = try value.resolve(in: components)
75+
return value.endpoints.map { endpoint in
6976
OperationDescription(
7077
path: path,
7178
endpoint: endpoint,

Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/TypesFileTranslator.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,17 @@ struct TypesFileTranslator: FileTranslator {
4141
+ config.additionalImports
4242
.map { ImportDescription(moduleName: $0) }
4343

44-
let apiProtocol = translateAPIProtocol(doc.paths)
44+
let apiProtocol = try translateAPIProtocol(doc.paths)
4545

4646
let serversDecl = translateServers(doc.servers)
4747

4848
let components = try translateComponents(doc.components)
4949

50-
let operationDescriptions =
51-
OperationDescription
52-
.all(
53-
from: parsedOpenAPI.paths,
54-
in: doc.components,
55-
asSwiftSafeName: swiftSafeName
56-
)
50+
let operationDescriptions = try OperationDescription.all(
51+
from: parsedOpenAPI.paths,
52+
in: doc.components,
53+
asSwiftSafeName: swiftSafeName
54+
)
5755
let operations = try translateOperations(operationDescriptions)
5856

5957
let typesFile = FileDescription(

Sources/_OpenAPIGeneratorCore/Translator/TypesTranslator/translateAPIProtocol.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ extension TypesFileTranslator {
1919
/// per HTTP operation defined in the OpenAPI document.
2020
/// - Parameter paths: The paths object from the OpenAPI document.
2121
/// - Returns: A protocol declaration.
22-
func translateAPIProtocol(_ paths: OpenAPI.PathItem.Map) -> Declaration {
22+
/// - Throws: If `paths` contains any references.
23+
func translateAPIProtocol(_ paths: OpenAPI.PathItem.Map) throws -> Declaration {
2324

24-
let operations = OperationDescription.all(
25+
let operations = try OperationDescription.all(
2526
from: paths,
2627
in: components,
2728
asSwiftSafeName: swiftSafeName

Tests/OpenAPIGeneratorCoreTests/Parser/Test_YamsParser.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,12 @@ final class Test_YamsParser: Test_Core {
118118
description: Success
119119
"""
120120

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

Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,52 @@ final class SnippetBasedReferenceTests: XCTestCase {
726726
"""
727727
)
728728
}
729+
730+
func testPathsSimplestCase() throws {
731+
try self.assertPathsTranslation(
732+
"""
733+
/health:
734+
get:
735+
operationId: getHealth
736+
responses:
737+
'200':
738+
description: A success response with a greeting.
739+
content:
740+
text/plain:
741+
schema:
742+
type: string
743+
""",
744+
"""
745+
public protocol APIProtocol: Sendable {
746+
func getHealth(_ input: Operations.getHealth.Input) async throws -> Operations.getHealth.Output
747+
}
748+
"""
749+
)
750+
}
751+
752+
func testPathWithPathItemReference() throws {
753+
XCTAssertThrowsError(
754+
try self.assertPathsTranslation(
755+
"""
756+
/health:
757+
get:
758+
operationId: getHealth
759+
responses:
760+
'200':
761+
description: A success response with a greeting.
762+
content:
763+
text/plain:
764+
schema:
765+
type: string
766+
/health2:
767+
$ref: "#/paths/~1health"
768+
""",
769+
"""
770+
unused: This test throws an error.
771+
"""
772+
)
773+
)
774+
}
729775
}
730776

731777
extension SnippetBasedReferenceTests {
@@ -817,6 +863,19 @@ extension SnippetBasedReferenceTests {
817863
let translation = try translator.translateComponentRequestBodies(translator.components.requestBodies)
818864
try XCTAssertSwiftEquivalent(translation, expectedSwift, file: file, line: line)
819865
}
866+
867+
func assertPathsTranslation(
868+
_ pathsYAML: String,
869+
componentsYAML: String = "{}",
870+
_ expectedSwift: String,
871+
file: StaticString = #filePath,
872+
line: UInt = #line
873+
) throws {
874+
let translator = try makeTypesTranslator(componentsYAML: componentsYAML)
875+
let paths = try YAMLDecoder().decode(OpenAPI.PathItem.Map.self, from: pathsYAML)
876+
let translation = try translator.translateAPIProtocol(paths)
877+
try XCTAssertSwiftEquivalent(translation, expectedSwift, file: file, line: line)
878+
}
820879
}
821880

822881
private func XCTAssertEqualWithDiff(

0 commit comments

Comments
 (0)