Skip to content

Commit 69de944

Browse files
committed
Some PR feedback, some temporary updates to snippet reference tests (will be moved around in subsequent commits, not final
1 parent cda4a4c commit 69de944

File tree

8 files changed

+63
-33
lines changed

8 files changed

+63
-33
lines changed

Sources/_OpenAPIGeneratorCore/Config.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ public struct Config: Sendable {
5252
/// The naming strategy to use for deriving Swift identifiers from OpenAPI identifiers.
5353
///
5454
/// Defaults to `defensive`.
55-
public var namingStrategy: NamingStrategy?
55+
public var namingStrategy: NamingStrategy
5656

5757
/// A map of OpenAPI identifiers to desired Swift identifiers, used instead of the naming strategy.
58-
public var nameOverrides: [String: String]?
58+
public var nameOverrides: [String: String]
5959

6060
/// Additional pre-release features to enable.
6161
public var featureFlags: FeatureFlags
@@ -76,8 +76,8 @@ public struct Config: Sendable {
7676
access: AccessModifier,
7777
additionalImports: [String] = [],
7878
filter: DocumentFilter? = nil,
79-
namingStrategy: NamingStrategy? = nil,
80-
nameOverrides: [String: String]? = nil,
79+
namingStrategy: NamingStrategy = .defensive,
80+
nameOverrides: [String: String] = [:],
8181
featureFlags: FeatureFlags = []
8282
) {
8383
self.mode = mode

Sources/_OpenAPIGeneratorCore/Translator/FileTranslator.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,17 @@ extension FileTranslator {
5050
var context: TranslatorContext {
5151
let asSwiftSafeName: (String, SwiftNameOptions) -> String
5252
switch config.namingStrategy {
53-
case .defensive, .none: asSwiftSafeName = { $0.safeForSwiftCode_defensive(options: $1) }
53+
case .defensive: asSwiftSafeName = { $0.safeForSwiftCode_defensive(options: $1) }
5454
case .idiomatic: asSwiftSafeName = { $0.safeForSwiftCode_idiomatic(options: $1) }
5555
}
56-
let overrides = config.nameOverrides ?? [:]
57-
return TranslatorContext(asSwiftSafeName: { name, options in
58-
if let override = overrides[name] { return override }
59-
return asSwiftSafeName(name, options)
60-
})
56+
let overrides = config.nameOverrides
57+
return TranslatorContext(
58+
asSwiftSafeName: { name, options in
59+
if let override = overrides[name] { return override }
60+
return asSwiftSafeName(name, options)
61+
},
62+
namingStrategy: config.namingStrategy
63+
)
6164
}
6265
}
6366

@@ -69,4 +72,7 @@ struct TranslatorContext {
6972
/// - Parameter string: The string to convert to be safe for Swift.
7073
/// - Returns: A Swift-safe version of the input string.
7174
var asSwiftSafeName: (String, SwiftNameOptions) -> String
75+
76+
/// The naming strategy.
77+
var namingStrategy: NamingStrategy
7278
}

Sources/_OpenAPIGeneratorCore/Translator/TypeAssignment/TypeAssigner.swift

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -531,19 +531,34 @@ struct TypeAssigner {
531531
default:
532532
let safedType = context.asSwiftSafeName(contentType.originallyCasedType, .noncapitalized)
533533
let safedSubtype = context.asSwiftSafeName(contentType.originallyCasedSubtype, .noncapitalized)
534-
let prefix = "\(safedType)_\(safedSubtype)"
534+
let componentSeparator: String
535+
let capitalizeNonFirstWords: Bool
536+
switch context.namingStrategy {
537+
case .defensive:
538+
componentSeparator = "_"
539+
capitalizeNonFirstWords = false
540+
case .idiomatic:
541+
componentSeparator = ""
542+
capitalizeNonFirstWords = true
543+
}
544+
let prettifiedSubtype = capitalizeNonFirstWords ? safedSubtype.capitalized : safedSubtype
545+
let prefix = "\(safedType)\(componentSeparator)\(prettifiedSubtype)"
535546
let params = contentType.lowercasedParameterPairs
536547
guard !params.isEmpty else { return prefix }
537548
let safedParams =
538-
params.map { pair in
539-
pair.split(separator: "=").map { context.asSwiftSafeName(String($0), .noncapitalized) }
540-
.joined(separator: "_")
541-
}
542-
.joined(separator: "_")
543-
return prefix + "_" + safedParams
549+
params.map { pair in
550+
pair
551+
.split(separator: "=")
552+
.map { component in
553+
let safedComponent = context.asSwiftSafeName(String(component), .noncapitalized)
554+
return capitalizeNonFirstWords ? safedComponent.capitalized : safedComponent
555+
}
556+
.joined(separator: componentSeparator)
557+
}
558+
.joined(separator: componentSeparator)
559+
return prefix + componentSeparator + safedParams
544560
}
545561
}
546-
547562
}
548563

549564
extension FileTranslator {

Sources/swift-openapi-generator/FilterCommand.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ private let sampleConfig = _UserConfig(
8787
tags: ["greetings"],
8888
paths: ["/greeting"],
8989
schemas: ["Greeting"]
90-
),
91-
namingStrategy: .idiomatic,
92-
nameOverrides: ["SPECIAL_NAME": "SpecialName"]
90+
)
9391
)
9492

9593
enum OutputFormat: String, ExpressibleByArgument {

Sources/swift-openapi-generator/GenerateOptions.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,16 @@ extension _GenerateOptions {
7575
return []
7676
}
7777

78+
/// Returns the naming strategy requested by the user.
79+
/// - Parameter config: The configuration specified by the user.
80+
/// - Returns: The naming strategy requestd by the user.
7881
func resolvedNamingStrategy(_ config: _UserConfig?) -> NamingStrategy { config?.namingStrategy ?? .defensive }
82+
83+
/// Returns the name overrides requested by the user.
84+
/// - Parameter config: The configuration specified by the user.
85+
/// - Returns: The name overrides requested by the user
7986
func resolvedNameOverrides(_ config: _UserConfig?) -> [String: String] { config?.nameOverrides ?? [:] }
87+
8088
/// Returns a list of the feature flags requested by the user.
8189
/// - Parameter config: The configuration specified by the user.
8290
/// - Returns: A set of feature flags requested by the user.

Tests/OpenAPIGeneratorCoreTests/TestUtilities.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Test_Core: XCTestCase {
2828
func makeTranslator(
2929
components: OpenAPI.Components = .noComponents,
3030
diagnostics: any DiagnosticCollector = PrintingDiagnosticCollector(),
31-
namingStrategy: NamingStrategy? = nil,
31+
namingStrategy: NamingStrategy = .defensive,
3232
nameOverrides: [String: String] = [:],
3333
featureFlags: FeatureFlags = []
3434
) -> TypesFileTranslator {
@@ -44,7 +44,7 @@ class Test_Core: XCTestCase {
4444
func makeTypesTranslator(
4545
components: OpenAPI.Components = .noComponents,
4646
diagnostics: any DiagnosticCollector = PrintingDiagnosticCollector(),
47-
namingStrategy: NamingStrategy? = nil,
47+
namingStrategy: NamingStrategy = .defensive,
4848
nameOverrides: [String: String] = [:],
4949
featureFlags: FeatureFlags = []
5050
) -> TypesFileTranslator {
@@ -60,7 +60,7 @@ class Test_Core: XCTestCase {
6060
}
6161

6262
func makeConfig(
63-
namingStrategy: NamingStrategy? = nil,
63+
namingStrategy: NamingStrategy = .defensive,
6464
nameOverrides: [String: String] = [:],
6565
featureFlags: FeatureFlags = []
6666
) -> Config {

Tests/OpenAPIGeneratorCoreTests/Translator/Operations/Test_OperationDescription.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,10 @@ final class Test_OperationDescription: Test_Core {
144144
endpoint: endpoint,
145145
pathParameters: pathItem.parameters,
146146
components: .init(),
147-
context: .init(asSwiftSafeName: { string, _ in string })
147+
context: .init(
148+
asSwiftSafeName: { string, _ in string },
149+
namingStrategy: .defensive
150+
)
148151
)
149152
}
150153
}

Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3106,7 +3106,7 @@ final class SnippetBasedReferenceTests: XCTestCase {
31063106
public enum RequestBodies {
31073107
@frozen public enum MultipleContentTypes: Sendable, Hashable {
31083108
case json(Swift.Int)
3109-
case application_json_foo_bar(Swift.Int)
3109+
case applicationJsonFooBar(Swift.Int)
31103110
case plainText(OpenAPIRuntime.HTTPBody)
31113111
case binary(OpenAPIRuntime.HTTPBody)
31123112
}
@@ -3131,7 +3131,7 @@ final class SnippetBasedReferenceTests: XCTestCase {
31313131
headerFields: &request.headerFields,
31323132
contentType: "application/json; charset=utf-8"
31333133
)
3134-
case let .application_json_foo_bar(value):
3134+
case let .applicationJsonFooBar(value):
31353135
body = try converter.setRequiredRequestBodyAsJSON(
31363136
value,
31373137
headerFields: &request.headerFields,
@@ -3180,7 +3180,7 @@ final class SnippetBasedReferenceTests: XCTestCase {
31803180
Swift.Int.self,
31813181
from: requestBody,
31823182
transforming: { value in
3183-
.application_json_foo_bar(value)
3183+
.applicationJsonFooBar(value)
31843184
}
31853185
)
31863186
case "text/plain":
@@ -3268,11 +3268,11 @@ final class SnippetBasedReferenceTests: XCTestCase {
32683268
}
32693269
}
32703270
}
3271-
case application_json_foo_bar(Swift.Int)
3272-
public var application_json_foo_bar: Swift.Int {
3271+
case applicationJsonFooBar(Swift.Int)
3272+
public var applicationJsonFooBar: Swift.Int {
32733273
get throws {
32743274
switch self {
3275-
case let .application_json_foo_bar(body):
3275+
case let .applicationJsonFooBar(body):
32763276
return body
32773277
default:
32783278
try throwUnexpectedResponseBody(
@@ -3337,7 +3337,7 @@ final class SnippetBasedReferenceTests: XCTestCase {
33373337
headerFields: &response.headerFields,
33383338
contentType: "application/json; charset=utf-8"
33393339
)
3340-
case let .application_json_foo_bar(value):
3340+
case let .applicationJsonFooBar(value):
33413341
try converter.validateAcceptIfPresent(
33423342
"application/json; foo=bar",
33433343
in: request.headerFields
@@ -3401,7 +3401,7 @@ final class SnippetBasedReferenceTests: XCTestCase {
34013401
Swift.Int.self,
34023402
from: responseBody,
34033403
transforming: { value in
3404-
.application_json_foo_bar(value)
3404+
.applicationJsonFooBar(value)
34053405
}
34063406
)
34073407
case "text/plain":

0 commit comments

Comments
 (0)