Skip to content

Commit 8d56c51

Browse files
Merge remote-tracking branch 'upstream/main' into refactor/generate-server-variable-enums
2 parents 924362c + f1d7d8b commit 8d56c51

33 files changed

+146
-162
lines changed

.github/workflows/pull_request.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
uses: apple/swift-nio/.github/workflows/swift_matrix.yml@main
3434
with:
3535
name: "Integration test"
36-
matrix_linux_command: "apt-get update -yq && apt-get install -yq jq && ./scripts/run-integration-test.sh"
36+
matrix_linux_command: "apt-get update -yq && apt-get install -yq jq && SWIFT_OPENAPI_GENERATOR_REPO_URL=file://${GITHUB_WORKSPACE} ./scripts/run-integration-test.sh"
3737
matrix_linux_5_8_enabled: false
3838
matrix_linux_nightly_main_enabled: false
3939

.github/workflows/scheduled.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
uses: apple/swift-nio/.github/workflows/swift_matrix.yml@main
2121
with:
2222
name: "Integration test"
23-
matrix_linux_command: "apt-get update -yq && apt-get install -yq jq && ./scripts/run-integration-test.sh"
23+
matrix_linux_command: "apt-get update -yq && apt-get install -yq jq && SWIFT_OPENAPI_GENERATOR_REPO_URL=file://${GITHUB_WORKSPACE} ./scripts/run-integration-test.sh"
2424
matrix_linux_5_8_enabled: false
2525

2626
example-packages:

Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/ClientTranslator.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ struct ClientFileTranslator: FileTranslator {
3737
let imports =
3838
Constants.File.clientServerImports + config.additionalImports.map { ImportDescription(moduleName: $0) }
3939

40-
let clientMethodDecls =
41-
try OperationDescription.all(from: doc.paths, in: components, asSwiftSafeName: swiftSafeName)
40+
let clientMethodDecls = try OperationDescription.all(from: doc.paths, in: components, context: context)
4241
.map(translateClientMethod(_:))
4342

4443
let clientStructPropertyDecl: Declaration = .commentable(

Sources/_OpenAPIGeneratorCore/Translator/CommonTranslations/SwiftSafeNames.swift

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414
import Foundation
1515

16-
extension FileTranslator {
17-
18-
/// Returns a copy of the string modified to be a valid Swift identifier.
19-
///
20-
/// - Parameter string: The string to convert to be safe for Swift.
21-
/// - Returns: A Swift-safe version of the input string.
22-
func swiftSafeName(for string: String) -> String { string.safeForSwiftCode }
23-
}
24-
25-
fileprivate extension String {
16+
extension String {
2617

2718
/// Returns a string sanitized to be usable as a Swift identifier.
2819
///

Sources/_OpenAPIGeneratorCore/Translator/CommonTranslations/translateAllAnyOneOf.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ extension TypesFileTranslator {
6464
parent: typeName
6565
)
6666
let associatedDeclarations: [Declaration]
67-
if TypeMatcher.isInlinable(schema) {
67+
if typeMatcher.isInlinable(schema) {
6868
associatedDeclarations = try translateSchema(
6969
typeName: propertyType.typeName,
7070
schema: schema,
@@ -78,10 +78,10 @@ extension TypesFileTranslator {
7878
originalName: key,
7979
typeUsage: propertyType,
8080
associatedDeclarations: associatedDeclarations,
81-
asSwiftSafeName: swiftSafeName
81+
context: context
8282
)
8383
var referenceStack = ReferenceStack.empty
84-
let isKeyValuePairSchema = try TypeMatcher.isKeyValuePair(
84+
let isKeyValuePairSchema = try typeMatcher.isKeyValuePair(
8585
schema,
8686
referenceStack: &referenceStack,
8787
components: components
@@ -173,7 +173,7 @@ extension TypesFileTranslator {
173173
parent: typeName
174174
)
175175
let associatedDeclarations: [Declaration]
176-
if TypeMatcher.isInlinable(schema) {
176+
if typeMatcher.isInlinable(schema) {
177177
associatedDeclarations = try translateSchema(
178178
typeName: childType.typeName,
179179
schema: schema,
@@ -183,7 +183,7 @@ extension TypesFileTranslator {
183183
associatedDeclarations = []
184184
}
185185
var referenceStack = ReferenceStack.empty
186-
let isKeyValuePair = try TypeMatcher.isKeyValuePair(
186+
let isKeyValuePair = try typeMatcher.isKeyValuePair(
187187
schema,
188188
referenceStack: &referenceStack,
189189
components: components
@@ -209,7 +209,7 @@ extension TypesFileTranslator {
209209
let decoder: Declaration
210210
if let discriminator {
211211
let originalName = discriminator.propertyName
212-
let swiftName = swiftSafeName(for: originalName)
212+
let swiftName = context.asSwiftSafeName(originalName)
213213
codingKeysDecls = [
214214
.enum(
215215
accessModifier: config.access,

Sources/_OpenAPIGeneratorCore/Translator/CommonTranslations/translateObjectStruct.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ extension TypesFileTranslator {
8585
parent: typeName
8686
)
8787
let associatedDeclarations: [Declaration]
88-
if TypeMatcher.isInlinable(value) {
88+
if typeMatcher.isInlinable(value) {
8989
associatedDeclarations = try translateSchema(
9090
typeName: propertyType.typeName,
9191
schema: value,
@@ -100,7 +100,7 @@ extension TypesFileTranslator {
100100
originalName: key,
101101
typeUsage: propertyType,
102102
associatedDeclarations: associatedDeclarations,
103-
asSwiftSafeName: swiftSafeName
103+
context: context
104104
)
105105
}
106106

@@ -153,7 +153,7 @@ extension TypesFileTranslator {
153153
components: components,
154154
inParent: parent
155155
)
156-
if TypeMatcher.isInlinable(schema) {
156+
if typeMatcher.isInlinable(schema) {
157157
associatedDeclarations = try translateSchema(
158158
typeName: valueTypeUsage.typeName,
159159
schema: schema,
@@ -175,7 +175,7 @@ extension TypesFileTranslator {
175175
default: .emptyInit,
176176
isSerializedInTopLevelDictionary: false,
177177
associatedDeclarations: associatedDeclarations,
178-
asSwiftSafeName: swiftSafeName
178+
context: context
179179
)
180180
return (.allowingAdditionalProperties, extraProperty)
181181
}

Sources/_OpenAPIGeneratorCore/Translator/CommonTranslations/translateStringEnum.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ extension FileTranslator {
4949
// In nullable enum schemas, empty strings are parsed as Void.
5050
// This is unlikely to be fixed, so handling that case here.
5151
// https://github.com/apple/swift-openapi-generator/issues/118
52-
if isNullable && anyValue is Void { return (swiftSafeName(for: ""), .string("")) }
52+
if isNullable && anyValue is Void { return (context.asSwiftSafeName(""), .string("")) }
5353
guard let rawValue = anyValue as? String else {
5454
throw GenericError(message: "Disallowed value for a string enum '\(typeName)': \(anyValue)")
5555
}
56-
let caseName = swiftSafeName(for: rawValue)
56+
let caseName = context.asSwiftSafeName(rawValue)
5757
return (caseName, .string(rawValue))
5858
case .integer:
5959
let rawValue: Int

Sources/_OpenAPIGeneratorCore/Translator/CommonTypes/DiscriminatorExtensions.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ extension FileTranslator {
7979
/// component.
8080
/// - Parameter type: The `OneOfMappedType` for which to determine the case name.
8181
/// - Returns: A string representing the safe Swift name for the specified `OneOfMappedType`.
82-
func safeSwiftNameForOneOfMappedType(_ type: OneOfMappedType) -> String { swiftSafeName(for: type.rawNames[0]) }
82+
func safeSwiftNameForOneOfMappedType(_ type: OneOfMappedType) -> String {
83+
context.asSwiftSafeName(type.rawNames[0])
84+
}
8385
}
8486

8587
extension OpenAPI.Discriminator {

Sources/_OpenAPIGeneratorCore/Translator/CommonTypes/StructBlueprint.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,14 @@ struct PropertyBlueprint {
146146
/// referring to them in the property.
147147
var associatedDeclarations: [Declaration] = []
148148

149-
/// A converted function from user-provided strings to strings
150-
/// safe to be used as a Swift identifier.
151-
var asSwiftSafeName: (String) -> String
149+
/// A set of configuration values that inform translation.
150+
var context: TranslatorContext
152151
}
153152

154153
extension PropertyBlueprint {
155154

156155
/// A name that is verified to be a valid Swift identifier.
157-
var swiftSafeName: String { asSwiftSafeName(originalName) }
156+
var swiftSafeName: String { context.asSwiftSafeName(originalName) }
158157

159158
/// The JSON path to the property.
160159
///

Sources/_OpenAPIGeneratorCore/Translator/FileTranslator.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,19 @@ protocol FileTranslator {
4343
/// - Throws: An error if translation encounters issues or errors during the process.
4444
func translateFile(parsedOpenAPI: ParsedOpenAPIRepresentation) throws -> StructuredSwiftRepresentation
4545
}
46+
47+
extension FileTranslator {
48+
49+
/// A new context from the file translator.
50+
var context: TranslatorContext { TranslatorContext(asSwiftSafeName: { $0.safeForSwiftCode }) }
51+
}
52+
53+
/// A set of configuration values for concrete file translators.
54+
struct TranslatorContext {
55+
56+
/// A closure that returns a copy of the string modified to be a valid Swift identifier.
57+
///
58+
/// - Parameter string: The string to convert to be safe for Swift.
59+
/// - Returns: A Swift-safe version of the input string.
60+
var asSwiftSafeName: (String) -> String
61+
}

0 commit comments

Comments
 (0)