Skip to content

Commit e7d47de

Browse files
authored
Merge pull request #26 from mattpolzin/more-versatilility
Tweaks to better support Viz generation.
2 parents 8bb3026 + ea4eb4e commit e7d47de

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

Sources/JSONAPISwiftGen/ResourceObjectSwiftGenCollection.swift

+10-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import OpenAPIKit30
1010
public struct ResourceObjectSwiftGenCollection {
1111
public let resourceObjectGenerators: [ResourceObjectSwiftGen]
1212

13-
public init(_ doc: DereferencedDocument, testSuiteConfiguration: TestSuiteConfiguration) throws {
13+
public init(
14+
_ doc: DereferencedDocument,
15+
testSuiteConfiguration: TestSuiteConfiguration,
16+
allowPlaceholders: Bool = true
17+
) throws {
1418
let pathItems = doc.paths
1519

1620
resourceObjectGenerators = OpenAPI.HttpMethod.allCases
@@ -31,7 +35,8 @@ public struct ResourceObjectSwiftGenCollection {
3135
at: path,
3236
on: doc.servers.first!,
3337
given: parameters,
34-
testSuiteConfiguration: testSuiteConfiguration
38+
testSuiteConfiguration: testSuiteConfiguration,
39+
allowPlaceholders: allowPlaceholders
3540
).values.flatMap { Array($0.resourceObjectGenerators) }
3641
}
3742
}
@@ -46,7 +51,8 @@ func documents(
4651
at path: OpenAPI.Path,
4752
on server: OpenAPI.Server,
4853
given params: [DereferencedParameter],
49-
testSuiteConfiguration: TestSuiteConfiguration
54+
testSuiteConfiguration: TestSuiteConfiguration,
55+
allowPlaceholders: Bool
5056
) -> [OpenAPI.Response.StatusCode: JSONAPIDocumentSwiftGen] {
5157
var responseDocuments = [OpenAPI.Response.StatusCode: JSONAPIDocumentSwiftGen]()
5258
for (statusCode, response) in responses {
@@ -155,6 +161,7 @@ func documents(
155161
responseDocuments[statusCode] = try JSONAPIDocumentSwiftGen(
156162
swiftTypeName: responseBodyTypeName,
157163
structure: responseSchema,
164+
allowPlaceholders: allowPlaceholders,
158165
examples: exampleGens,
159166
testExampleFuncs: testExampleFuncs
160167
)

Sources/JSONAPISwiftGen/Swift Generators/ResourceObjectSwiftGen.swift

+7-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public struct ResourceObjectSwiftGen: JSONSchemaSwiftGenerator, ResourceTypeSwif
2525
public let structure: DereferencedJSONSchema
2626
public let decls: [Decl]
2727
public let resourceTypeName: String
28+
public let jsonTypeName: String
2829
public let exportedSwiftTypeNames: Set<String>
2930
public let relatives: Set<Relative>
3031
public let relationshipStubGenerators: Set<ResourceObjectStubSwiftGen>
@@ -41,7 +42,7 @@ public struct ResourceObjectSwiftGen: JSONSchemaSwiftGenerator, ResourceTypeSwif
4142
) throws {
4243
self.structure = structure
4344

44-
(decls, relatives, relationshipStubGenerators) = try ResourceObjectSwiftGen.swiftDecls(
45+
(decls, jsonTypeName, relatives, relationshipStubGenerators) = try ResourceObjectSwiftGen.swiftDecls(
4546
from: structure,
4647
allowPlaceholders: allowPlaceholders
4748
)
@@ -56,12 +57,12 @@ public struct ResourceObjectSwiftGen: JSONSchemaSwiftGenerator, ResourceTypeSwif
5657
static func swiftDecls(
5758
from structure: DereferencedJSONSchema,
5859
allowPlaceholders: Bool
59-
) throws -> (decls: [Decl], relatives: Set<Relative>, relationshipStubs: Set<ResourceObjectStubSwiftGen>) {
60+
) throws -> (decls: [Decl], jsonTypeName: String, relatives: Set<Relative>, relationshipStubs: Set<ResourceObjectStubSwiftGen>) {
6061
guard case let .object(_, resourceObjectContextB) = structure else {
6162
throw Error.rootNotJSONObject
6263
}
6364

64-
let (typeName, typeNameDecl) = try jsonAPITypeNameSnippet(
65+
let (jsonTypeName, typeName, typeNameDecl) = try jsonAPITypeNameSnippet(
6566
contextB: resourceObjectContextB,
6667
allowPlaceholders: allowPlaceholders
6768
)
@@ -130,7 +131,7 @@ public struct ResourceObjectSwiftGen: JSONSchemaSwiftGenerator, ResourceTypeSwif
130131
}
131132
)
132133

133-
return (decls: decls, relatives: Set(relationships.relatives), relationshipStubs: relationshipStubs)
134+
return (decls: decls, jsonTypeName: jsonTypeName, relatives: Set(relationships.relatives), relationshipStubs: relationshipStubs)
134135
}
135136

136137
/// Creates a snippet of code that declares the static Swift property
@@ -140,13 +141,14 @@ public struct ResourceObjectSwiftGen: JSONSchemaSwiftGenerator, ResourceTypeSwif
140141
private static func jsonAPITypeNameSnippet(
141142
contextB: DereferencedJSONSchema.ObjectContext,
142143
allowPlaceholders: Bool
143-
) throws -> (typeName: String, typeNameDeclCode: Decl) {
144+
) throws -> (jsonTypeName: String, typeName: String, typeNameDeclCode: Decl) {
144145
let typeNameString = try jsonAPITypeName(
145146
from: contextB,
146147
allowPlaceholders: allowPlaceholders
147148
)
148149

149150
return (
151+
jsonTypeName: typeNameString,
150152
typeName: typeCased(typeNameString),
151153
typeNameDeclCode: StaticDecl(.let(propName: "jsonType", swiftType: .init(String.self), .init(value: "\"\(typeNameString)\"")))
152154
)

0 commit comments

Comments
 (0)