Skip to content

Commit 72383b3

Browse files
Create an Unresolved* typealias for all supported OpenAPI types (apple#74)
### Motivation Currently, when referencing OpenAPI types that may not be referenceable, it requires a very long and unintuitive type name (e.g., `Either<JSONReference<OpenAPI.Request>, OpenAPI.Request>`). We can clean this up, by exposing more usable, and practical typealiases to improve quality of life and speed while working on the generator. ### Modifications **Added and used `Unresolved*` typaliases for:** - OpenAPI.Request - OpenAPI.Response - OpenAPI.Header - JSONSchema ### Result New typealiases will be added, decreasing confusion, and increasing symmetry when using OpenAPI types. ### Resolves Resolves apple#12 ### Edits 1. Removed additions of `Resolved*` typealiases --------- Co-authored-by: andrewelliott <> Co-authored-by: Si Beaumont <[email protected]>
1 parent c41395a commit 72383b3

File tree

11 files changed

+37
-20
lines changed

11 files changed

+37
-20
lines changed

Sources/_OpenAPIGeneratorCore/Translator/CommonTranslations/translateSchema.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ extension FileTranslator {
3030
/// instead of extracted from the schema.
3131
func translateSchema(
3232
typeName: TypeName,
33-
schema: Either<JSONReference<JSONSchema>, JSONSchema>?,
33+
schema: UnresolvedSchema?,
3434
overrides: SchemaOverrides
3535
) throws -> [Declaration] {
3636
let unwrappedSchema: JSONSchema

Sources/_OpenAPIGeneratorCore/Translator/Content/SchemaContent.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct SchemaContent {
2424
///
2525
/// Can be nil for unstructured JSON payloads, or for unstructured
2626
/// content types such as binary data.
27-
var schema: Either<JSONReference<JSONSchema>, JSONSchema>?
27+
var schema: UnresolvedSchema?
2828
}
2929

3030
/// A type grouping schema content and its computed Swift type usage.
@@ -44,3 +44,8 @@ struct TypedSchemaContent {
4444
typeUsage ?? TypeName.valueContainer.asUsage
4545
}
4646
}
47+
48+
/// An unresolved OpenAPI schema.
49+
///
50+
/// Can be either a reference or an inline schema.
51+
typealias UnresolvedSchema = Either<JSONReference<JSONSchema>, JSONSchema>

Sources/_OpenAPIGeneratorCore/Translator/Operations/OperationDescription.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ extension OperationDescription {
138138
/// Returns all parameters by resolving any parameter references first.
139139
///
140140
/// - Throws: When an invalid JSON reference is found.
141-
var allResolvedParameters: [ResolvedParameter] {
141+
var allResolvedParameters: [OpenAPI.Parameter] {
142142
get throws {
143143
try allParameters.map { try $0.resolve(in: components) }
144144
}

Sources/_OpenAPIGeneratorCore/Translator/Parameters/TypedParameter.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct TypedParameter {
2020
var parameter: OpenAPI.Parameter
2121

2222
/// The underlying schema.
23-
var schema: Either<JSONReference<JSONSchema>, JSONSchema>
23+
var schema: UnresolvedSchema
2424

2525
/// The computed type usage.
2626
var typeUsage: TypeUsage
@@ -66,7 +66,7 @@ extension TypedParameter {
6666
}
6767
}
6868

69-
extension Either where A == JSONReference<JSONSchema>, B == JSONSchema {
69+
extension UnresolvedSchema {
7070

7171
/// A schema to be inlined.
7272
///
@@ -128,7 +128,7 @@ extension FileTranslator {
128128
let locationTypeName = parameter.location.typeName(in: parent)
129129
let foundIn = "\(locationTypeName.description)/\(parameter.name)"
130130

131-
let schema: Either<JSONReference<JSONSchema>, JSONSchema>
131+
let schema: UnresolvedSchema
132132
let codingStrategy: CodingStrategy
133133
switch parameter.schemaOrContent {
134134
case let .a(schemaContext):
@@ -228,9 +228,6 @@ extension FileTranslator {
228228
/// Can be either a reference or an inline parameter.
229229
typealias UnresolvedParameter = Either<JSONReference<OpenAPI.Parameter>, OpenAPI.Parameter>
230230

231-
/// A resolved OpenAPI parameter.
232-
typealias ResolvedParameter = OpenAPI.Parameter
233-
234231
extension OpenAPI.Parameter.Context.Location {
235232

236233
/// A name of the location usable as a Swift type name.

Sources/_OpenAPIGeneratorCore/Translator/RequestBody/TypedRequestBody.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ extension FileTranslator {
5555
/// - Returns: Typed request content; nil if the request body is
5656
/// unsupported.
5757
func typedRequestBody(
58-
from unresolvedRequest: Either<JSONReference<OpenAPI.Request>, OpenAPI.Request>,
58+
from unresolvedRequest: UnresolvedRequest,
5959
inParent parent: TypeName
6060
) throws -> TypedRequestBody? {
6161
let type: TypeName
@@ -81,7 +81,7 @@ extension FileTranslator {
8181
/// unsupported.
8282
func typedRequestBody(
8383
typeName: TypeName,
84-
from unresolvedRequest: Either<JSONReference<OpenAPI.Request>, OpenAPI.Request>
84+
from unresolvedRequest: UnresolvedRequest
8585
) throws -> TypedRequestBody? {
8686

8787
let request: OpenAPI.Request
@@ -113,3 +113,8 @@ extension FileTranslator {
113113
)
114114
}
115115
}
116+
117+
/// An unresolved OpenAPI request.
118+
///
119+
/// Can be either a reference or an inline request.
120+
typealias UnresolvedRequest = Either<JSONReference<OpenAPI.Request>, OpenAPI.Request>

Sources/_OpenAPIGeneratorCore/Translator/RequestBody/translateRequestBody.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ extension TypesFileTranslator {
7676
/// - unresolvedRequestBody: An unresolved request body.
7777
/// - parent: The type name of the parent structure.
7878
func parseRequestBodyAsProperty(
79-
for unresolvedRequestBody: Either<JSONReference<OpenAPI.Request>, OpenAPI.Request>?,
79+
for unresolvedRequestBody: UnresolvedRequest?,
8080
inParent parent: TypeName
8181
) throws -> PropertyBlueprint {
8282
let bodyEnumTypeName: TypeName

Sources/_OpenAPIGeneratorCore/Translator/Responses/TypedResponse.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,8 @@ extension FileTranslator {
5959
)
6060
}
6161
}
62+
63+
/// An unresolved OpenAPI response.
64+
///
65+
/// Can be either a reference or an inline response.
66+
typealias UnresolvedResponse = Either<JSONReference<OpenAPI.Response>, OpenAPI.Response>

Sources/_OpenAPIGeneratorCore/Translator/Responses/TypedResponseHeader.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct TypedResponseHeader {
2424
var name: String
2525

2626
/// The underlying schema.
27-
var schema: Either<JSONReference<JSONSchema>, JSONSchema>
27+
var schema: UnresolvedSchema
2828

2929
/// The Swift type representing the response header.
3030
var typeUsage: TypeUsage
@@ -87,7 +87,7 @@ extension FileTranslator {
8787
/// - parent: The Swift type name of the parent type of the headers.
8888
/// - Returns: Typed response header if supported, nil otherwise.
8989
func typedResponseHeader(
90-
from unresolvedHeader: Either<JSONReference<OpenAPI.Header>, OpenAPI.Header>,
90+
from unresolvedHeader: UnresolvedHeader,
9191
named name: String,
9292
inParent parent: TypeName
9393
) throws -> TypedResponseHeader? {
@@ -103,7 +103,7 @@ extension FileTranslator {
103103

104104
let foundIn = "\(parent.description)/\(name)"
105105

106-
let schema: Either<JSONReference<JSONSchema>, JSONSchema>
106+
let schema: UnresolvedSchema
107107
let codingStrategy: CodingStrategy
108108

109109
switch header.schemaOrContent {
@@ -164,3 +164,8 @@ extension FileTranslator {
164164
)
165165
}
166166
}
167+
168+
/// An unresolved OpenAPI response header.
169+
///
170+
/// Can be either a reference or an inline response header.
171+
typealias UnresolvedHeader = Either<JSONReference<OpenAPI.Header>, OpenAPI.Header>

Sources/_OpenAPIGeneratorCore/Translator/TypeAssignment/TypeAssigner.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ struct TypeAssigner {
7373
/// - Returns: A type usage; or nil if the schema is nil or unsupported.
7474
static func typeUsage(
7575
usingNamingHint hint: String,
76-
withSchema schema: Either<JSONReference<JSONSchema>, JSONSchema>?,
76+
withSchema schema: UnresolvedSchema?,
7777
inParent parent: TypeName
7878
) throws -> TypeUsage? {
7979
let associatedType: TypeUsage?

Sources/_OpenAPIGeneratorCore/Translator/TypeAssignment/TypeMatcher.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ struct TypeMatcher {
125125
/// - Parameter schema: The schema to match a referenceable type for.
126126
/// - Returns: `true` if the schema is referenceable; `false` otherwise.
127127
static func isReferenceable(
128-
_ schema: Either<JSONReference<JSONSchema>, JSONSchema>?
128+
_ schema: UnresolvedSchema?
129129
) -> Bool {
130130
guard let schema else {
131131
// fragment type is referenceable
@@ -163,7 +163,7 @@ struct TypeMatcher {
163163
/// - Parameter schema: The schema to match a referenceable type for.
164164
/// - Returns: `true` if the schema is inlinable; `false` otherwise.
165165
static func isInlinable(
166-
_ schema: Either<JSONReference<JSONSchema>, JSONSchema>?
166+
_ schema: UnresolvedSchema?
167167
) -> Bool {
168168
!isReferenceable(schema)
169169
}

Sources/_OpenAPIGeneratorCore/Translator/TypeAssignment/isSchemaSupported.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ extension FileTranslator {
4141
/// - foundIn: A description of the schema's context.
4242
/// - Returns: `true` if the schema is supported; `false` otherwise.
4343
func validateSchemaIsSupported(
44-
_ schema: Either<JSONReference<JSONSchema>, JSONSchema>?,
44+
_ schema: UnresolvedSchema?,
4545
foundIn: String
4646
) throws -> Bool {
4747
guard try isSchemaSupported(schema) else {
@@ -116,7 +116,7 @@ extension FileTranslator {
116116
/// - schema: The schema to validate.
117117
/// - Returns: `true` if the schema is supported; `false` otherwise.
118118
func isSchemaSupported(
119-
_ schema: Either<JSONReference<JSONSchema>, JSONSchema>?
119+
_ schema: UnresolvedSchema?
120120
) throws -> Bool {
121121
guard let schema else {
122122
// fragment type is supported

0 commit comments

Comments
 (0)