diff --git a/docs/generators/swift5.md b/docs/generators/swift5.md index be06da4bf09d..92d3265b1bc0 100644 --- a/docs/generators/swift5.md +++ b/docs/generators/swift5.md @@ -53,6 +53,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl |swiftUseApiNamespace|Flag to make all the API classes inner-class of {{projectName}}API| |null| |useBacktickEscapes|Escape reserved words using backticks (default: false)| |false| |useClasses|Use final classes for models instead of structs (default: false)| |false| +|useJsonEncodable|Make models conform to JSONEncodable protocol (default: true)| |true| |useSPMFileStructure|Use SPM file structure and set the source path to Sources/{{projectName}} (default: false).| |null| ## IMPORT MAPPING diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java index 08afea2fa608..4c0b8326a452 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java @@ -70,6 +70,7 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig public static final String USE_BACKTICK_ESCAPES = "useBacktickEscapes"; public static final String GENERATE_MODEL_ADDITIONAL_PROPERTIES = "generateModelAdditionalProperties"; public static final String HASHABLE_MODELS = "hashableModels"; + public static final String USE_JSON_ENCODABLE = "useJsonEncodable"; public static final String MAP_FILE_BINARY_TO_DATA = "mapFileBinaryToData"; protected static final String LIBRARY_ALAMOFIRE = "alamofire"; protected static final String LIBRARY_URLSESSION = "urlsession"; @@ -93,6 +94,7 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig protected boolean useBacktickEscapes = false; protected boolean generateModelAdditionalProperties = true; protected boolean hashableModels = true; + protected boolean useJsonEncodable = true; protected boolean mapFileBinaryToData = false; protected String[] responseAs = new String[0]; protected String sourceFolder = swiftPackagePath; @@ -299,6 +301,10 @@ public Swift5ClientCodegen() { "Make hashable models (default: true)") .defaultValue(Boolean.TRUE.toString())); + cliOptions.add(new CliOption(USE_JSON_ENCODABLE, + "Make models conform to JSONEncodable protocol (default: true)") + .defaultValue(Boolean.TRUE.toString())); + cliOptions.add(new CliOption(MAP_FILE_BINARY_TO_DATA, "[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)") .defaultValue(Boolean.FALSE.toString())); @@ -497,6 +503,11 @@ public void processOpts() { } additionalProperties.put(HASHABLE_MODELS, hashableModels); + if (additionalProperties.containsKey(USE_JSON_ENCODABLE)) { + setUseJsonEncodable(convertPropertyToBooleanAndWriteBack(USE_JSON_ENCODABLE)); + } + additionalProperties.put(USE_JSON_ENCODABLE, useJsonEncodable); + if (additionalProperties.containsKey(MAP_FILE_BINARY_TO_DATA)) { setMapFileBinaryToData(convertPropertyToBooleanAndWriteBack(MAP_FILE_BINARY_TO_DATA)); } @@ -953,6 +964,10 @@ public void setHashableModels(boolean hashableModels) { this.hashableModels = hashableModels; } + public void setUseJsonEncodable(boolean useJsonEncodable) { + this.useJsonEncodable = useJsonEncodable; + } + @Override public String toEnumValue(String value, String datatype) { // for string, array of string @@ -995,7 +1010,7 @@ public String toEnumVarName(String name, String datatype) { if (getSymbolName(name) != null) { return camelize(WordUtils.capitalizeFully(getSymbolName(name).toUpperCase(Locale.ROOT)), true); } - + // Camelize only when we have a structure defined below Boolean camelized = false; if (name.matches("[A-Z][a-z0-9]+[a-zA-Z0-9]*")) { diff --git a/modules/openapi-generator/src/main/resources/swift5/modelObject.mustache b/modules/openapi-generator/src/main/resources/swift5/modelObject.mustache index a82cc1e89537..b1c23e19b916 100644 --- a/modules/openapi-generator/src/main/resources/swift5/modelObject.mustache +++ b/modules/openapi-generator/src/main/resources/swift5/modelObject.mustache @@ -1,5 +1,5 @@ -{{^objcCompatible}}{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} {{#useClasses}}final class{{/useClasses}}{{^useClasses}}struct{{/useClasses}} {{{classname}}}: {{#useVapor}}Content{{/useVapor}}{{^useVapor}}Codable, JSONEncodable{{/useVapor}}{{#vendorExtensions.x-swift-hashable}}, Hashable{{/vendorExtensions.x-swift-hashable}} { -{{/objcCompatible}}{{#objcCompatible}}@objc {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} class {{classname}}: NSObject, Codable, JSONEncodable { +{{^objcCompatible}}{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} {{#useClasses}}final class{{/useClasses}}{{^useClasses}}struct{{/useClasses}} {{{classname}}}: {{#useVapor}}Content{{/useVapor}}{{^useVapor}}Codable{{#useJsonEncodable}}, JSONEncodable{{/useJsonEncodable}}{{/useVapor}}{{#vendorExtensions.x-swift-hashable}}, Hashable{{/vendorExtensions.x-swift-hashable}} { +{{/objcCompatible}}{{#objcCompatible}}@objc {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} class {{classname}}: NSObject, Codable{{#useJsonEncodable}}, JSONEncodable{{/useJsonEncodable}} { {{/objcCompatible}} {{#allVars}} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/Swift5OptionsProvider.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/Swift5OptionsProvider.java index 003e5b4b5b7d..b41f76f9a993 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/Swift5OptionsProvider.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/Swift5OptionsProvider.java @@ -50,6 +50,7 @@ public class Swift5OptionsProvider implements OptionsProvider { public static final String USE_BACKTICKS_ESCAPES_VALUE = "false"; public static final String GENERATE_MODEL_ADDITIONAL_PROPERTIES_VALUE = "true"; public static final String HASHABLE_MODELS_VALUE = "true"; + public static final String USE_JSON_ENCODABLE_VALUE = "true"; public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false"; public static final String PREPEND_FORM_OR_BODY_PARAMETERS_VALUE = "true"; public static final String LIBRARY_VALUE = "alamofire"; @@ -98,6 +99,7 @@ public Map createOptions() { .put(Swift5ClientCodegen.SWIFT_PACKAGE_PATH, SWIFT_PACKAGE_PATH_VALUE) .put(Swift5ClientCodegen.GENERATE_MODEL_ADDITIONAL_PROPERTIES, GENERATE_MODEL_ADDITIONAL_PROPERTIES_VALUE) .put(Swift5ClientCodegen.HASHABLE_MODELS, HASHABLE_MODELS_VALUE) + .put(Swift5ClientCodegen.USE_JSON_ENCODABLE, USE_JSON_ENCODABLE_VALUE) .put(Swift5ClientCodegen.MAP_FILE_BINARY_TO_DATA, "false") .put(Swift5ClientCodegen.USE_CLASSES, "false") .put(CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE, ENUM_UNKNOWN_DEFAULT_CASE_VALUE)