[swift6] oneOf-default-case (#19754)

This commit is contained in:
Bruno Coelho 2024-10-02 21:30:36 +01:00 committed by GitHub
parent 43ce2a8e10
commit 673cd15e72
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 25 additions and 0 deletions

View File

@ -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| |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| |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| |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| |podAuthors|Authors used for Podspec| |null|
|podDescription|Description used for Podspec| |null| |podDescription|Description used for Podspec| |null|
|podDocumentationURL|Documentation URL used for Podspec| |null| |podDocumentationURL|Documentation URL used for Podspec| |null|

View File

@ -67,6 +67,7 @@ public class Swift6ClientCodegen extends DefaultCodegen implements CodegenConfig
public static final String LENIENT_TYPE_CAST = "lenientTypeCast"; public static final String LENIENT_TYPE_CAST = "lenientTypeCast";
public static final String USE_SPM_FILE_STRUCTURE = "useSPMFileStructure"; public static final String USE_SPM_FILE_STRUCTURE = "useSPMFileStructure";
public static final String SWIFT_PACKAGE_PATH = "swiftPackagePath"; 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_CLASSES = "useClasses";
public static final String USE_BACKTICK_ESCAPES = "useBacktickEscapes"; public static final String USE_BACKTICK_ESCAPES = "useBacktickEscapes";
public static final String GENERATE_MODEL_ADDITIONAL_PROPERTIES = "generateModelAdditionalProperties"; public static final String GENERATE_MODEL_ADDITIONAL_PROPERTIES = "generateModelAdditionalProperties";
@ -104,6 +105,8 @@ public class Swift6ClientCodegen extends DefaultCodegen implements CodegenConfig
@Setter @Setter
protected String swiftPackagePath = "Classes" + File.separator + "OpenAPIs"; protected String swiftPackagePath = "Classes" + File.separator + "OpenAPIs";
@Setter @Setter
protected boolean oneOfUnknownDefaultCase = false;
@Setter
protected boolean useClasses = false; protected boolean useClasses = false;
@Setter @Setter
protected boolean useBacktickEscapes = false; protected boolean useBacktickEscapes = false;
@ -321,6 +324,9 @@ public class Swift6ClientCodegen extends DefaultCodegen implements CodegenConfig
cliOptions.add(new CliOption(GENERATE_MODEL_ADDITIONAL_PROPERTIES, cliOptions.add(new CliOption(GENERATE_MODEL_ADDITIONAL_PROPERTIES,
"Generate model additional properties (default: true)") "Generate model additional properties (default: true)")
.defaultValue(Boolean.TRUE.toString())); .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(CodegenConstants.API_NAME_PREFIX, CodegenConstants.API_NAME_PREFIX_DESC));
cliOptions.add(new CliOption(USE_SPM_FILE_STRUCTURE, "Use SPM file structure" cliOptions.add(new CliOption(USE_SPM_FILE_STRUCTURE, "Use SPM file structure"
@ -573,6 +579,11 @@ public class Swift6ClientCodegen extends DefaultCodegen implements CodegenConfig
typeMapping.put("date", "Date"); 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)) { if (additionalProperties.containsKey(USE_CLASSES)) {
setUseClasses(convertPropertyToBooleanAndWriteBack(USE_CLASSES)); setUseClasses(convertPropertyToBooleanAndWriteBack(USE_CLASSES));
} }

View File

@ -2,6 +2,9 @@
{{#oneOf}} {{#oneOf}}
case type{{.}}({{.}}) case type{{.}}({{.}})
{{/oneOf}} {{/oneOf}}
{{#oneOfUnknownDefaultCase}}
case unknownDefaultOpenApi
{{/oneOfUnknownDefaultCase}}
public func encode(to encoder: Encoder) throws { public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer() var container = encoder.singleValueContainer()
@ -10,6 +13,10 @@
case .type{{.}}(let value): case .type{{.}}(let value):
try container.encode(value) try container.encode(value)
{{/oneOf}} {{/oneOf}}
{{#oneOfUnknownDefaultCase}}
case unknownDefaultOpenApi(let type):
try container.encodeNil()
{{/oneOfUnknownDefaultCase}}
} }
} }
@ -25,7 +32,12 @@
self = .type{{.}}(value) self = .type{{.}}(value)
{{/oneOf}} {{/oneOf}}
} else { } else {
{{#oneOfUnknownDefaultCase}}
self = .unknownDefaultOpenApi
{{/oneOfUnknownDefaultCase}}
{{^oneOfUnknownDefaultCase}}
throw DecodingError.typeMismatch(Self.Type.self, .init(codingPath: decoder.codingPath, debugDescription: "Unable to decode instance of {{classname}}")) throw DecodingError.typeMismatch(Self.Type.self, .init(codingPath: decoder.codingPath, debugDescription: "Unable to decode instance of {{classname}}"))
{{/oneOfUnknownDefaultCase}}
} }
} }
} }

View File

@ -104,6 +104,7 @@ public class Swift6ClientCodegenOptionsProvider implements OptionsProvider {
.put(Swift6ClientCodegen.MAP_FILE_BINARY_TO_DATA, "false") .put(Swift6ClientCodegen.MAP_FILE_BINARY_TO_DATA, "false")
.put(Swift6ClientCodegen.USE_CUSTOM_DATE_WITHOUT_TIME, "false") .put(Swift6ClientCodegen.USE_CUSTOM_DATE_WITHOUT_TIME, "false")
.put(Swift6ClientCodegen.VALIDATABLE, "true") .put(Swift6ClientCodegen.VALIDATABLE, "true")
.put(Swift6ClientCodegen.ONE_OF_UNKNOWN_DEFAULT_CASE, "false")
.put(Swift6ClientCodegen.USE_CLASSES, "false") .put(Swift6ClientCodegen.USE_CLASSES, "false")
.put(Swift6ClientCodegen.API_STATIC_METHOD, .put(Swift6ClientCodegen.API_STATIC_METHOD,
API_STATIC_METHOD_VALUE) API_STATIC_METHOD_VALUE)