From cdb020d04436e9f5dbf01517443af928b42654c5 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Tue, 5 Dec 2023 15:06:31 +0800 Subject: [PATCH] add enum name mapping option to swift generators (#17297) --- bin/configs/swift-combine-petstore-new.yaml | 2 ++ bin/configs/swift5-default.yaml | 2 ++ .../codegen/languages/Swift5ClientCodegen.java | 8 ++++++++ .../codegen/languages/SwiftCombineClientCodegen.java | 8 ++++++++ .../client/PetstoreOpenAPI/Sources/Models/Order.swift | 2 +- .../PetstoreClient/Classes/OpenAPIs/Models/Order.swift | 2 +- .../Classes/OpenAPIs/Models/OuterEnum.swift | 2 +- 7 files changed, 23 insertions(+), 3 deletions(-) diff --git a/bin/configs/swift-combine-petstore-new.yaml b/bin/configs/swift-combine-petstore-new.yaml index 5349e056a5b8..2a132fb34e58 100644 --- a/bin/configs/swift-combine-petstore-new.yaml +++ b/bin/configs/swift-combine-petstore-new.yaml @@ -5,3 +5,5 @@ templateDir: modules/openapi-generator/src/main/resources/swift-combine additionalProperties: hideGenerationTimestamp: "true" projectName: "PetstoreOpenAPI" +enumNameMappings: + delivered: shipped diff --git a/bin/configs/swift5-default.yaml b/bin/configs/swift5-default.yaml index fffd318be1ad..c419a704699a 100644 --- a/bin/configs/swift5-default.yaml +++ b/bin/configs/swift5-default.yaml @@ -8,3 +8,5 @@ additionalProperties: podSummary: PetstoreClient projectName: PetstoreClient podHomepage: https://github.com/openapitools/openapi-generator +enumNameMappings: + delivered: shipped diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java index 77c4e5016dbf..afec866d600d 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java @@ -1027,6 +1027,10 @@ public String toEnumDefaultValue(String value, String datatype) { @Override public String toEnumVarName(String name, String datatype) { + if (enumNameMapping.containsKey(name)) { + return enumNameMapping.get(name); + } + if (name.length() == 0) { return "empty"; } @@ -1127,6 +1131,10 @@ private String titleCase(final String input) { @Override public String toEnumName(CodegenProperty property) { + if (enumNameMapping.containsKey(property.name)) { + return enumNameMapping.get(property.name); + } + String enumName = toModelName(property.name); // Ensure that the enum type doesn't match a reserved word or diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SwiftCombineClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SwiftCombineClientCodegen.java index 36daf075bbf4..fd7e9022f30d 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SwiftCombineClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SwiftCombineClientCodegen.java @@ -575,6 +575,10 @@ public String toEnumDefaultValue(String value, String datatype) { @Override public String toEnumVarName(String name, String datatype) { + if (enumNameMapping.containsKey(name)) { + return enumNameMapping.get(name); + } + if (name.length() == 0) { return "empty"; } @@ -630,6 +634,10 @@ public String toEnumVarName(String name, String datatype) { @Override public String toEnumName(CodegenProperty property) { + if (enumNameMapping.containsKey(property.name)) { + return enumNameMapping.get(property.name); + } + String enumName = toModelName(property.name); // Ensure that the enum type doesn't match a reserved word or diff --git a/samples/client/petstore/swift-combine/client/PetstoreOpenAPI/Sources/Models/Order.swift b/samples/client/petstore/swift-combine/client/PetstoreOpenAPI/Sources/Models/Order.swift index 9a9b4e2bf3b1..5bdf9cc1212b 100644 --- a/samples/client/petstore/swift-combine/client/PetstoreOpenAPI/Sources/Models/Order.swift +++ b/samples/client/petstore/swift-combine/client/PetstoreOpenAPI/Sources/Models/Order.swift @@ -12,7 +12,7 @@ public struct Order: Codable { public enum Status: String, Codable, CaseIterable { case placed = "placed" case approved = "approved" - case delivered = "delivered" + case shipped = "delivered" } public var id: Int64? public var petId: Int64? diff --git a/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/Models/Order.swift b/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/Models/Order.swift index bf7da6a3f862..818233c5fcee 100644 --- a/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/Models/Order.swift +++ b/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/Models/Order.swift @@ -15,7 +15,7 @@ public struct Order: Codable, JSONEncodable, Hashable { public enum Status: String, Codable, CaseIterable { case placed = "placed" case approved = "approved" - case delivered = "delivered" + case shipped = "delivered" } public var id: Int64? public var petId: Int64? diff --git a/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/Models/OuterEnum.swift b/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/Models/OuterEnum.swift index 76c34b3c2ce6..a1981c09f18f 100644 --- a/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/Models/OuterEnum.swift +++ b/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/Models/OuterEnum.swift @@ -13,5 +13,5 @@ import AnyCodable public enum OuterEnum: String, Codable, CaseIterable { case placed = "placed" case approved = "approved" - case delivered = "delivered" + case shipped = "delivered" }