add oneOf-default-case option for swift5 (#19094)

Co-authored-by: erokha <dev@erokha.com>
This commit is contained in:
Nikita Erokhin 2024-10-02 12:56:04 +03:00 committed by GitHub
parent 1cdd7b7ff2
commit f409bf1440
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 24 additions and 0 deletions

View File

@ -32,6 +32,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|

View File

@ -67,6 +67,7 @@ public class Swift5ClientCodegen 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";
@ -92,6 +93,7 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig
@Setter protected boolean swiftUseApiNamespace = false;
@Setter protected boolean useSPMFileStructure = false;
@Setter protected String swiftPackagePath = "Classes" + File.separator + "OpenAPIs";
@Setter protected boolean oneOfUnknownDefaultCase = false;
@Setter protected boolean useClasses = false;
@Setter protected boolean useBacktickEscapes = false;
@Setter protected boolean generateModelAdditionalProperties = true;
@ -292,6 +294,9 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig
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"
@ -532,6 +537,11 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig
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));
}

View File

@ -2,6 +2,9 @@
{{#oneOf}}
case type{{.}}({{.}})
{{/oneOf}}
{{#oneOfUnknownDefaultCase}}
case unknownDefault
{{/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 unknownDefault(let type):
try container.encodeNil()
{{/oneOfUnknownDefaultCase}}
}
}
@ -25,7 +32,12 @@
self = .type{{.}}(value)
{{/oneOf}}
} else {
{{#oneOfUnknownDefaultCase}}
self = .unknownDefault
{{/oneOfUnknownDefaultCase}}
{{^oneOfUnknownDefaultCase}}
throw DecodingError.typeMismatch(Self.Type.self, .init(codingPath: decoder.codingPath, debugDescription: "Unable to decode instance of {{classname}}"))
{{/oneOfUnknownDefaultCase}}
}
}
}

View File

@ -100,6 +100,7 @@ public class Swift5OptionsProvider implements OptionsProvider {
.put(Swift5ClientCodegen.MAP_FILE_BINARY_TO_DATA, "false")
.put(Swift5ClientCodegen.USE_CUSTOM_DATE_WITHOUT_TIME, "false")
.put(Swift5ClientCodegen.VALIDATABLE, "true")
.put(Swift5ClientCodegen.ONE_OF_UNKNOWN_DEFAULT_CASE, "false")
.put(Swift5ClientCodegen.USE_CLASSES, "false")
.put(CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE, ENUM_UNKNOWN_DEFAULT_CASE_VALUE)
.build();