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
13 changes: 10 additions & 3 deletions Sources/JSONAPISwiftGen/ResourceObjectSwiftGenCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import OpenAPIKit30
public struct ResourceObjectSwiftGenCollection {
public let resourceObjectGenerators: [ResourceObjectSwiftGen]

public init(_ doc: DereferencedDocument, testSuiteConfiguration: TestSuiteConfiguration) throws {
public init(
_ doc: DereferencedDocument,
testSuiteConfiguration: TestSuiteConfiguration,
allowPlaceholders: Bool = true
) throws {
let pathItems = doc.paths

resourceObjectGenerators = OpenAPI.HttpMethod.allCases
Expand All @@ -31,7 +35,8 @@ public struct ResourceObjectSwiftGenCollection {
at: path,
on: doc.servers.first!,
given: parameters,
testSuiteConfiguration: testSuiteConfiguration
testSuiteConfiguration: testSuiteConfiguration,
allowPlaceholders: allowPlaceholders
).values.flatMap { Array($0.resourceObjectGenerators) }
}
}
Expand All @@ -46,7 +51,8 @@ func documents(
at path: OpenAPI.Path,
on server: OpenAPI.Server,
given params: [DereferencedParameter],
testSuiteConfiguration: TestSuiteConfiguration
testSuiteConfiguration: TestSuiteConfiguration,
allowPlaceholders: Bool
) -> [OpenAPI.Response.StatusCode: JSONAPIDocumentSwiftGen] {
var responseDocuments = [OpenAPI.Response.StatusCode: JSONAPIDocumentSwiftGen]()
for (statusCode, response) in responses {
Expand Down Expand Up @@ -155,6 +161,7 @@ func documents(
responseDocuments[statusCode] = try JSONAPIDocumentSwiftGen(
swiftTypeName: responseBodyTypeName,
structure: responseSchema,
allowPlaceholders: allowPlaceholders,
examples: exampleGens,
testExampleFuncs: testExampleFuncs
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public struct ResourceObjectSwiftGen: JSONSchemaSwiftGenerator, ResourceTypeSwif
public let structure: DereferencedJSONSchema
public let decls: [Decl]
public let resourceTypeName: String
public let jsonTypeName: String
public let exportedSwiftTypeNames: Set<String>
public let relatives: Set<Relative>
public let relationshipStubGenerators: Set<ResourceObjectStubSwiftGen>
Expand All @@ -41,7 +42,7 @@ public struct ResourceObjectSwiftGen: JSONSchemaSwiftGenerator, ResourceTypeSwif
) throws {
self.structure = structure

(decls, relatives, relationshipStubGenerators) = try ResourceObjectSwiftGen.swiftDecls(
(decls, jsonTypeName, relatives, relationshipStubGenerators) = try ResourceObjectSwiftGen.swiftDecls(
from: structure,
allowPlaceholders: allowPlaceholders
)
Expand All @@ -56,12 +57,12 @@ public struct ResourceObjectSwiftGen: JSONSchemaSwiftGenerator, ResourceTypeSwif
static func swiftDecls(
from structure: DereferencedJSONSchema,
allowPlaceholders: Bool
) throws -> (decls: [Decl], relatives: Set<Relative>, relationshipStubs: Set<ResourceObjectStubSwiftGen>) {
) throws -> (decls: [Decl], jsonTypeName: String, relatives: Set<Relative>, relationshipStubs: Set<ResourceObjectStubSwiftGen>) {
guard case let .object(_, resourceObjectContextB) = structure else {
throw Error.rootNotJSONObject
}

let (typeName, typeNameDecl) = try jsonAPITypeNameSnippet(
let (jsonTypeName, typeName, typeNameDecl) = try jsonAPITypeNameSnippet(
contextB: resourceObjectContextB,
allowPlaceholders: allowPlaceholders
)
Expand Down Expand Up @@ -130,7 +131,7 @@ public struct ResourceObjectSwiftGen: JSONSchemaSwiftGenerator, ResourceTypeSwif
}
)

return (decls: decls, relatives: Set(relationships.relatives), relationshipStubs: relationshipStubs)
return (decls: decls, jsonTypeName: jsonTypeName, relatives: Set(relationships.relatives), relationshipStubs: relationshipStubs)
}

/// Creates a snippet of code that declares the static Swift property
Expand All @@ -140,13 +141,14 @@ public struct ResourceObjectSwiftGen: JSONSchemaSwiftGenerator, ResourceTypeSwif
private static func jsonAPITypeNameSnippet(
contextB: DereferencedJSONSchema.ObjectContext,
allowPlaceholders: Bool
) throws -> (typeName: String, typeNameDeclCode: Decl) {
) throws -> (jsonTypeName: String, typeName: String, typeNameDeclCode: Decl) {
let typeNameString = try jsonAPITypeName(
from: contextB,
allowPlaceholders: allowPlaceholders
)

return (
jsonTypeName: typeNameString,
typeName: typeCased(typeNameString),
typeNameDeclCode: StaticDecl(.let(propName: "jsonType", swiftType: .init(String.self), .init(value: "\"\(typeNameString)\"")))
)
Expand Down