diff --git a/Sources/JSONAPISwiftGen/ResourceObjectSwiftGenCollection.swift b/Sources/JSONAPISwiftGen/ResourceObjectSwiftGenCollection.swift index 0995363..7da23ff 100644 --- a/Sources/JSONAPISwiftGen/ResourceObjectSwiftGenCollection.swift +++ b/Sources/JSONAPISwiftGen/ResourceObjectSwiftGenCollection.swift @@ -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 @@ -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) } } } @@ -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 { @@ -155,6 +161,7 @@ func documents( responseDocuments[statusCode] = try JSONAPIDocumentSwiftGen( swiftTypeName: responseBodyTypeName, structure: responseSchema, + allowPlaceholders: allowPlaceholders, examples: exampleGens, testExampleFuncs: testExampleFuncs ) diff --git a/Sources/JSONAPISwiftGen/Swift Generators/ResourceObjectSwiftGen.swift b/Sources/JSONAPISwiftGen/Swift Generators/ResourceObjectSwiftGen.swift index b9d42f5..e47ddc9 100644 --- a/Sources/JSONAPISwiftGen/Swift Generators/ResourceObjectSwiftGen.swift +++ b/Sources/JSONAPISwiftGen/Swift Generators/ResourceObjectSwiftGen.swift @@ -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 public let relatives: Set public let relationshipStubGenerators: Set @@ -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 ) @@ -56,12 +57,12 @@ public struct ResourceObjectSwiftGen: JSONSchemaSwiftGenerator, ResourceTypeSwif static func swiftDecls( from structure: DereferencedJSONSchema, allowPlaceholders: Bool - ) throws -> (decls: [Decl], relatives: Set, relationshipStubs: Set) { + ) throws -> (decls: [Decl], jsonTypeName: String, relatives: Set, relationshipStubs: Set) { 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 ) @@ -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 @@ -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)\""))) )