Skip to content

Commit 5be3f9f

Browse files
committed
Add a CLI option for selecting the naming strategy
1 parent 9c01b32 commit 5be3f9f

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

Sources/_OpenAPIGeneratorCore/Config.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
/// A strategy for turning OpenAPI identifiers into Swift identifiers.
16-
public enum NamingStrategy: String, Sendable, Codable, Equatable {
16+
public enum NamingStrategy: String, Sendable, Codable, Equatable, CaseIterable {
1717

1818
/// A defensive strategy that can handle any OpenAPI identifier and produce a non-conflicting Swift identifier.
1919
///
@@ -54,6 +54,9 @@ public struct Config: Sendable {
5454
/// Defaults to `defensive`.
5555
public var namingStrategy: NamingStrategy
5656

57+
/// The default naming strategy.
58+
public static let defaultNamingStrategy: NamingStrategy = .defensive
59+
5760
/// A map of OpenAPI identifiers to desired Swift identifiers, used instead of the naming strategy.
5861
public var nameOverrides: [String: String]
5962

Sources/_OpenAPIGeneratorCore/Layers/StructuredSwiftRepresentation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct ImportDescription: Equatable, Codable {
5050
/// A description of an access modifier.
5151
///
5252
/// For example: `public`.
53-
public enum AccessModifier: String, Sendable, Equatable, Codable {
53+
public enum AccessModifier: String, Sendable, Equatable, Codable, CaseIterable {
5454

5555
/// A declaration accessible outside of the module.
5656
case `public`

Sources/swift-openapi-generator/GenerateOptions.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ struct _GenerateOptions: ParsableArguments {
2828
"The Swift files to generate. Options: \(GeneratorMode.prettyListing). Note that '\(GeneratorMode.client.rawValue)' and '\(GeneratorMode.server.rawValue)' depend on declarations in '\(GeneratorMode.types.rawValue)'."
2929
) var mode: [GeneratorMode] = []
3030

31+
@Option(help: "The access modifier to use for the API of generated code. Default: \(Config.defaultAccessModifier.rawValue)")
32+
var accessModifier: AccessModifier?
33+
3134
@Option(
3235
help:
33-
"The access modifier to use for the API of generated code. Default: \(Config.defaultAccessModifier.rawValue)"
34-
) var accessModifier: AccessModifier?
36+
"The strategy for converting OpenAPI names into Swift names. Default: \(Config.defaultNamingStrategy.rawValue)"
37+
) var namingStrategy: NamingStrategy?
3538

3639
@Option(help: "Additional import to add to all generated files.") var additionalImport: [String] = []
3740

@@ -44,6 +47,7 @@ struct _GenerateOptions: ParsableArguments {
4447
}
4548

4649
extension AccessModifier: ExpressibleByArgument {}
50+
extension NamingStrategy: ExpressibleByArgument {}
4751

4852
extension _GenerateOptions {
4953

@@ -78,7 +82,10 @@ extension _GenerateOptions {
7882
/// Returns the naming strategy requested by the user.
7983
/// - Parameter config: The configuration specified by the user.
8084
/// - Returns: The naming strategy requestd by the user.
81-
func resolvedNamingStrategy(_ config: _UserConfig?) -> NamingStrategy { config?.namingStrategy ?? .defensive }
85+
func resolvedNamingStrategy(_ config: _UserConfig?) -> NamingStrategy {
86+
if let namingStrategy { return namingStrategy }
87+
return config?.namingStrategy ?? .defensive
88+
}
8289

8390
/// Returns the name overrides requested by the user.
8491
/// - Parameter config: The configuration specified by the user.

0 commit comments

Comments
 (0)