Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ public struct APIRequestTestSwiftGen: SwiftGenerator {
.filter { !$0.context.inQuery } // for now these are handled as a block rather than each as separate args
.map(APIRequestTestSwiftGen.argument)

let undefinedPaarameterArgNames = Array(
pathComponents.components
.compactMap(Self.urlParameterSwiftName)
.filter { !parameterArgs.lazy.map(\.name).contains($0) }
)

if !undefinedPaarameterArgNames.isEmpty {
throw Error.pathParametersNotDefined(names: undefinedPaarameterArgNames)
}

let requestBodyTypeDef = SwiftTypeDef(name: "RequestBody", specializationReps: [])
let responseBodyTypeDef = SwiftTypeDef(name: "ResponseBody", specializationReps: [])

Expand Down Expand Up @@ -169,12 +179,11 @@ public struct APIRequestTestSwiftGen: SwiftGenerator {
originatingAt hostUrl: URL
) -> Decl {
let pathString = path.components.map { component in
guard component.first == "{",
component.last == "}" else {
return component
guard let swiftName = urlParameterSwiftName(component) else {
return component
}
return "\\("
+ propertyCased(String(component.dropFirst().dropLast()))
+ swiftName
+ ")"
}.joined(separator: "/")

Expand All @@ -185,6 +194,18 @@ public struct APIRequestTestSwiftGen: SwiftGenerator {
)
}

/// Get a property-cased name if the given string is an OpenAPI path parameter variable
/// (which means it is enclosed in brackets `{ }`). Otherwise, returns `nil`.
static func urlParameterSwiftName(
_ pathComponent: String
) -> String? {
guard pathComponent.first == "{",
pathComponent.last == "}" else {
return nil
}
return propertyCased(String(pathComponent.dropFirst().dropLast()))
}

private static func parameterSnippet(from parameter: DereferencedParameter) throws -> Decl {
let (parameterName, parameterType) = try argument(for: parameter)

Expand Down Expand Up @@ -226,6 +247,8 @@ public struct APIRequestTestSwiftGen: SwiftGenerator {
case parameterContentMapNotSupported
case unsupportedParameterSchema

case pathParametersNotDefined(names: [String])

case duplicateFunctionArgumentDetected
}
}
Expand Down
31 changes: 31 additions & 0 deletions Tests/JSONAPISwiftGenTests/APIRequestTestSwiftGenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,44 @@
import XCTest
import JSONAPI
import JSONAPITesting
import OpenAPIKit
import JSONAPISwiftGen

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

final class APIRequestTestSwiftGenTests: XCTestCase {
func test_undefinedPathParameters() {
XCTAssertThrowsError(
try APIRequestTestSwiftGen(
method: .post,
server: OpenAPI.Server(url: URL(string: "http://website.com")!),
pathComponents: "/widgets/{widget_id}",
parameters: []
)
)
}

func test_noUndefinedPathParameters() throws {
// just prove this does not throw for now.

// TODO: test could actually test for the given parameter
// being part of the resulting function signature or even more.
_ = try APIRequestTestSwiftGen(
method: .post,
server: OpenAPI.Server(url: URL(string: "http://website.com")!),
pathComponents: "/widgets/{widget_id}",
parameters: [
OpenAPI.Parameter(
name: "widget_id",
context: .path,
schema: .string
).dereferenced(in: .noComponents)
]
)
}
}

// MARK: - START - Function written to generated test suites
Expand Down