Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[swift6] oneOf-default-case #19754

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/generators/swift6.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
{{#oneOf}}
case type{{.}}({{.}})
{{/oneOf}}
{{#oneOfUnknownDefaultCase}}
case unknownDefaultOpenApi
{{/oneOfUnknownDefaultCase}}

public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
Expand All @@ -10,6 +13,10 @@
case .type{{.}}(let value):
try container.encode(value)
{{/oneOf}}
{{#oneOfUnknownDefaultCase}}
case unknownDefaultOpenApi(let type):
try container.encodeNil()
{{/oneOfUnknownDefaultCase}}
}
}

Expand All @@ -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}}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public Map<String, String> 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)
Expand Down
Loading