From 5be3f9f6eb092e973dfe048bcf8978cf57356135 Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Sat, 21 Dec 2024 12:41:26 +0100 Subject: [PATCH 1/4] Add a CLI option for selecting the naming strategy --- Sources/_OpenAPIGeneratorCore/Config.swift | 5 ++++- .../Layers/StructuredSwiftRepresentation.swift | 2 +- .../swift-openapi-generator/GenerateOptions.swift | 13 ++++++++++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Sources/_OpenAPIGeneratorCore/Config.swift b/Sources/_OpenAPIGeneratorCore/Config.swift index f65b0ea1..4b9a0875 100644 --- a/Sources/_OpenAPIGeneratorCore/Config.swift +++ b/Sources/_OpenAPIGeneratorCore/Config.swift @@ -13,7 +13,7 @@ //===----------------------------------------------------------------------===// /// A strategy for turning OpenAPI identifiers into Swift identifiers. -public enum NamingStrategy: String, Sendable, Codable, Equatable { +public enum NamingStrategy: String, Sendable, Codable, Equatable, CaseIterable { /// A defensive strategy that can handle any OpenAPI identifier and produce a non-conflicting Swift identifier. /// @@ -54,6 +54,9 @@ public struct Config: Sendable { /// Defaults to `defensive`. public var namingStrategy: NamingStrategy + /// The default naming strategy. + public static let defaultNamingStrategy: NamingStrategy = .defensive + /// A map of OpenAPI identifiers to desired Swift identifiers, used instead of the naming strategy. public var nameOverrides: [String: String] diff --git a/Sources/_OpenAPIGeneratorCore/Layers/StructuredSwiftRepresentation.swift b/Sources/_OpenAPIGeneratorCore/Layers/StructuredSwiftRepresentation.swift index 3bd64062..4a7cacef 100644 --- a/Sources/_OpenAPIGeneratorCore/Layers/StructuredSwiftRepresentation.swift +++ b/Sources/_OpenAPIGeneratorCore/Layers/StructuredSwiftRepresentation.swift @@ -50,7 +50,7 @@ struct ImportDescription: Equatable, Codable { /// A description of an access modifier. /// /// For example: `public`. -public enum AccessModifier: String, Sendable, Equatable, Codable { +public enum AccessModifier: String, Sendable, Equatable, Codable, CaseIterable { /// A declaration accessible outside of the module. case `public` diff --git a/Sources/swift-openapi-generator/GenerateOptions.swift b/Sources/swift-openapi-generator/GenerateOptions.swift index deaf69c2..0195d589 100644 --- a/Sources/swift-openapi-generator/GenerateOptions.swift +++ b/Sources/swift-openapi-generator/GenerateOptions.swift @@ -28,10 +28,13 @@ struct _GenerateOptions: ParsableArguments { "The Swift files to generate. Options: \(GeneratorMode.prettyListing). Note that '\(GeneratorMode.client.rawValue)' and '\(GeneratorMode.server.rawValue)' depend on declarations in '\(GeneratorMode.types.rawValue)'." ) var mode: [GeneratorMode] = [] + @Option(help: "The access modifier to use for the API of generated code. Default: \(Config.defaultAccessModifier.rawValue)") + var accessModifier: AccessModifier? + @Option( help: - "The access modifier to use for the API of generated code. Default: \(Config.defaultAccessModifier.rawValue)" - ) var accessModifier: AccessModifier? + "The strategy for converting OpenAPI names into Swift names. Default: \(Config.defaultNamingStrategy.rawValue)" + ) var namingStrategy: NamingStrategy? @Option(help: "Additional import to add to all generated files.") var additionalImport: [String] = [] @@ -44,6 +47,7 @@ struct _GenerateOptions: ParsableArguments { } extension AccessModifier: ExpressibleByArgument {} +extension NamingStrategy: ExpressibleByArgument {} extension _GenerateOptions { @@ -78,7 +82,10 @@ extension _GenerateOptions { /// Returns the naming strategy requested by the user. /// - Parameter config: The configuration specified by the user. /// - Returns: The naming strategy requestd by the user. - func resolvedNamingStrategy(_ config: _UserConfig?) -> NamingStrategy { config?.namingStrategy ?? .defensive } + func resolvedNamingStrategy(_ config: _UserConfig?) -> NamingStrategy { + if let namingStrategy { return namingStrategy } + return config?.namingStrategy ?? .defensive + } /// Returns the name overrides requested by the user. /// - Parameter config: The configuration specified by the user. From 510b7e4ea905e2450ad823160c15156365a467cc Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Sat, 21 Dec 2024 12:44:53 +0100 Subject: [PATCH 2/4] Formatting, use the centralized default for naming strategy in more places --- Sources/swift-openapi-generator/GenerateOptions.swift | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Sources/swift-openapi-generator/GenerateOptions.swift b/Sources/swift-openapi-generator/GenerateOptions.swift index 0195d589..b184fbea 100644 --- a/Sources/swift-openapi-generator/GenerateOptions.swift +++ b/Sources/swift-openapi-generator/GenerateOptions.swift @@ -28,8 +28,10 @@ struct _GenerateOptions: ParsableArguments { "The Swift files to generate. Options: \(GeneratorMode.prettyListing). Note that '\(GeneratorMode.client.rawValue)' and '\(GeneratorMode.server.rawValue)' depend on declarations in '\(GeneratorMode.types.rawValue)'." ) var mode: [GeneratorMode] = [] - @Option(help: "The access modifier to use for the API of generated code. Default: \(Config.defaultAccessModifier.rawValue)") - var accessModifier: AccessModifier? + @Option( + help: + "The access modifier to use for the API of generated code. Default: \(Config.defaultAccessModifier.rawValue)" + ) var accessModifier: AccessModifier? @Option( help: @@ -84,7 +86,7 @@ extension _GenerateOptions { /// - Returns: The naming strategy requestd by the user. func resolvedNamingStrategy(_ config: _UserConfig?) -> NamingStrategy { if let namingStrategy { return namingStrategy } - return config?.namingStrategy ?? .defensive + return config?.namingStrategy ?? Config.defaultNamingStrategy } /// Returns the name overrides requested by the user. From d9b65fc80b7f9c60e2389386440290640cd2ff68 Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Sat, 21 Dec 2024 12:45:34 +0100 Subject: [PATCH 3/4] Use the default in _even more_ places --- Sources/_OpenAPIGeneratorCore/Config.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/_OpenAPIGeneratorCore/Config.swift b/Sources/_OpenAPIGeneratorCore/Config.swift index 4b9a0875..8aa16af5 100644 --- a/Sources/_OpenAPIGeneratorCore/Config.swift +++ b/Sources/_OpenAPIGeneratorCore/Config.swift @@ -79,7 +79,7 @@ public struct Config: Sendable { access: AccessModifier, additionalImports: [String] = [], filter: DocumentFilter? = nil, - namingStrategy: NamingStrategy = .defensive, + namingStrategy: NamingStrategy = Config.defaultNamingStrategy, nameOverrides: [String: String] = [:], featureFlags: FeatureFlags = [] ) { From 25a0433daf599c900e4c0a7043dd63afedccc400 Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Mon, 6 Jan 2025 10:34:00 +0100 Subject: [PATCH 4/4] PR feedback --- .../GenerateOptions+runGenerator.swift | 2 +- Sources/swift-openapi-generator/GenerateOptions.swift | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/swift-openapi-generator/GenerateOptions+runGenerator.swift b/Sources/swift-openapi-generator/GenerateOptions+runGenerator.swift index 9cdf9f06..d91f7ad8 100644 --- a/Sources/swift-openapi-generator/GenerateOptions+runGenerator.swift +++ b/Sources/swift-openapi-generator/GenerateOptions+runGenerator.swift @@ -30,7 +30,7 @@ extension _GenerateOptions { func runGenerator(outputDirectory: URL, pluginSource: PluginSource?, isDryRun: Bool) async throws { let config = try loadedConfig() let sortedModes = try resolvedModes(config) - let resolvedAccessModifier = resolvedAccessModifier(config) ?? Config.defaultAccessModifier + let resolvedAccessModifier = resolvedAccessModifier(config) let resolvedAdditionalImports = resolvedAdditionalImports(config) let resolvedNamingStragy = resolvedNamingStrategy(config) let resolvedNameOverrides = resolvedNameOverrides(config) diff --git a/Sources/swift-openapi-generator/GenerateOptions.swift b/Sources/swift-openapi-generator/GenerateOptions.swift index b184fbea..6935a134 100644 --- a/Sources/swift-openapi-generator/GenerateOptions.swift +++ b/Sources/swift-openapi-generator/GenerateOptions.swift @@ -66,10 +66,10 @@ extension _GenerateOptions { /// Returns the access modifier requested by the user. /// - Parameter config: The configuration specified by the user. /// - Returns: The access modifier requested by the user, or nil if the default should be used. - func resolvedAccessModifier(_ config: _UserConfig?) -> AccessModifier? { + func resolvedAccessModifier(_ config: _UserConfig?) -> AccessModifier { if let accessModifier { return accessModifier } if let accessModifier = config?.accessModifier { return accessModifier } - return nil + return Config.defaultAccessModifier } /// Returns a list of additional imports requested by the user.