Skip to content
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/swift5.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
Expand Down Expand Up @@ -292,6 +294,9 @@ public Swift5ClientCodegen() {
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 @@ -532,6 +537,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 unknownDefault
{{/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 unknownDefault(let type):
try container.encodeNil()
{{/oneOfUnknownDefaultCase}}
}
}

Expand All @@ -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}}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public Map<String, String> createOptions() {
.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();
Expand Down