From 3ae20e3b6fe2a06e70dd588e4f7804297c73a9cf Mon Sep 17 00:00:00 2001 From: Bruno Coelho <4brunu@gmail.com> Date: Wed, 2 Oct 2024 11:18:57 +0100 Subject: [PATCH] [swift6] oneOf-default-case --- docs/generators/swift6.md | 1 + .../codegen/languages/Swift6ClientCodegen.java | 11 +++++++++++ .../src/main/resources/swift6/modelOneOf.mustache | 12 ++++++++++++ .../options/Swift6ClientCodegenOptionsProvider.java | 1 + 4 files changed, 25 insertions(+) diff --git a/docs/generators/swift6.md b/docs/generators/swift6.md index 955e74b87d48..aec4c81817f7 100644 --- a/docs/generators/swift6.md +++ b/docs/generators/swift6.md @@ -34,6 +34,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl |mapFileBinaryToData|[WARNING] This option will be removed and enabled by default in the future once we've enhanced the code to work with `Data` in all the different situations. Map File and Binary to Data (default: false)| |false| |nonPublicApi|Generates code with reduced access modifiers; allows embedding elsewhere without exposing non-public API calls to consumers.(default: false)| |null| |objcCompatible|Add additional properties and methods for Objective-C compatibility (default: false)| |null| +|oneOfUnknownDefaultCase|Add unknownDefault case to oneOf enum (default: false)| |false| |podAuthors|Authors used for Podspec| |null| |podDescription|Description used for Podspec| |null| |podDocumentationURL|Documentation URL used for Podspec| |null| diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift6ClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift6ClientCodegen.java index e9b5023c1c55..e075c95c7f9b 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift6ClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift6ClientCodegen.java @@ -67,6 +67,7 @@ public class Swift6ClientCodegen extends DefaultCodegen implements CodegenConfig public static final String LENIENT_TYPE_CAST = "lenientTypeCast"; public static final String USE_SPM_FILE_STRUCTURE = "useSPMFileStructure"; public static final String SWIFT_PACKAGE_PATH = "swiftPackagePath"; + public static final String ONE_OF_UNKNOWN_DEFAULT_CASE = "oneOfUnknownDefaultCase"; public static final String USE_CLASSES = "useClasses"; public static final String USE_BACKTICK_ESCAPES = "useBacktickEscapes"; public static final String GENERATE_MODEL_ADDITIONAL_PROPERTIES = "generateModelAdditionalProperties"; @@ -104,6 +105,8 @@ public class Swift6ClientCodegen extends DefaultCodegen implements CodegenConfig @Setter protected String swiftPackagePath = "Classes" + File.separator + "OpenAPIs"; @Setter + protected boolean oneOfUnknownDefaultCase = false; + @Setter protected boolean useClasses = false; @Setter protected boolean useBacktickEscapes = false; @@ -321,6 +324,9 @@ public Swift6ClientCodegen() { cliOptions.add(new CliOption(GENERATE_MODEL_ADDITIONAL_PROPERTIES, "Generate model additional properties (default: true)") .defaultValue(Boolean.TRUE.toString())); + cliOptions.add(new CliOption(ONE_OF_UNKNOWN_DEFAULT_CASE, + "Add unknownDefault case to oneOf enum (default: false)") + .defaultValue(Boolean.FALSE.toString())); cliOptions.add(new CliOption(CodegenConstants.API_NAME_PREFIX, CodegenConstants.API_NAME_PREFIX_DESC)); cliOptions.add(new CliOption(USE_SPM_FILE_STRUCTURE, "Use SPM file structure" @@ -573,6 +579,11 @@ public void processOpts() { typeMapping.put("date", "Date"); } + if (additionalProperties.containsKey(ONE_OF_UNKNOWN_DEFAULT_CASE)) { + setOneOfUnknownDefaultCase(convertPropertyToBooleanAndWriteBack(ONE_OF_UNKNOWN_DEFAULT_CASE)); + } + additionalProperties.put(ONE_OF_UNKNOWN_DEFAULT_CASE, oneOfUnknownDefaultCase); + if (additionalProperties.containsKey(USE_CLASSES)) { setUseClasses(convertPropertyToBooleanAndWriteBack(USE_CLASSES)); } diff --git a/modules/openapi-generator/src/main/resources/swift6/modelOneOf.mustache b/modules/openapi-generator/src/main/resources/swift6/modelOneOf.mustache index 629de6611bfb..312cda2f1ccd 100644 --- a/modules/openapi-generator/src/main/resources/swift6/modelOneOf.mustache +++ b/modules/openapi-generator/src/main/resources/swift6/modelOneOf.mustache @@ -2,6 +2,9 @@ {{#oneOf}} case type{{.}}({{.}}) {{/oneOf}} + {{#oneOfUnknownDefaultCase}} + case unknownDefaultOpenApi + {{/oneOfUnknownDefaultCase}} public func encode(to encoder: Encoder) throws { var container = encoder.singleValueContainer() @@ -10,6 +13,10 @@ case .type{{.}}(let value): try container.encode(value) {{/oneOf}} + {{#oneOfUnknownDefaultCase}} + case unknownDefaultOpenApi(let type): + try container.encodeNil() + {{/oneOfUnknownDefaultCase}} } } @@ -25,7 +32,12 @@ self = .type{{.}}(value) {{/oneOf}} } else { + {{#oneOfUnknownDefaultCase}} + self = .unknownDefaultOpenApi + {{/oneOfUnknownDefaultCase}} + {{^oneOfUnknownDefaultCase}} throw DecodingError.typeMismatch(Self.Type.self, .init(codingPath: decoder.codingPath, debugDescription: "Unable to decode instance of {{classname}}")) + {{/oneOfUnknownDefaultCase}} } } } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/Swift6ClientCodegenOptionsProvider.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/Swift6ClientCodegenOptionsProvider.java index 35076e5d7f61..880fde924053 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/Swift6ClientCodegenOptionsProvider.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/Swift6ClientCodegenOptionsProvider.java @@ -104,6 +104,7 @@ public Map createOptions() { .put(Swift6ClientCodegen.MAP_FILE_BINARY_TO_DATA, "false") .put(Swift6ClientCodegen.USE_CUSTOM_DATE_WITHOUT_TIME, "false") .put(Swift6ClientCodegen.VALIDATABLE, "true") + .put(Swift6ClientCodegen.ONE_OF_UNKNOWN_DEFAULT_CASE, "false") .put(Swift6ClientCodegen.USE_CLASSES, "false") .put(Swift6ClientCodegen.API_STATIC_METHOD, API_STATIC_METHOD_VALUE)