From ae0350280f27dabc1a7aa6c485515ab02561de4b Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Wed, 26 May 2021 05:02:28 -0400 Subject: [PATCH] [swift5] Map file and binary to Data (#9419) * [swift5] Map file and binary to Data Fixes: https://github.com/OpenAPITools/openapi-generator/issues/9308 * Update docs --- docs/generators/swift5.md | 1 + docs/generators/tiny-cpp.md | 2 +- .../languages/Swift5ClientCodegen.java | 24 ++++++++++++++++++- .../options/Swift5OptionsProvider.java | 1 + 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/docs/generators/swift5.md b/docs/generators/swift5.md index 47f0318e839..480eda2ebfd 100644 --- a/docs/generators/swift5.md +++ b/docs/generators/swift5.md @@ -17,6 +17,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl |legacyDiscriminatorBehavior|Set to false for generators with better support for discriminators. (Python, Java, Go, PowerShell, C#have this enabled by default).|
**true**
The mapping in the discriminator includes descendent schemas that allOf inherit from self and the discriminator mapping schemas in the OAS document.
**false**
The mapping in the discriminator includes any descendent schemas that allOf inherit from self, any oneOf schemas, any anyOf schemas, any x-discriminator-values, and the discriminator mapping schemas in the OAS document AND Codegen validates that oneOf and anyOf schemas contain the required discriminator and throws an error if the discriminator is missing.
|true| |lenientTypeCast|Accept and cast values for simple types (string->bool, string->int, int->string)| |false| |library|Library template (sub-template) to use|
**urlsession**
[DEFAULT] HTTP client: URLSession
**alamofire**
HTTP client: Alamofire
|urlsession| +|mapFileBinaryToData|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| |podAuthors|Authors used for Podspec| |null| diff --git a/docs/generators/tiny-cpp.md b/docs/generators/tiny-cpp.md index cd3877236ec..f7f6a0385ca 100644 --- a/docs/generators/tiny-cpp.md +++ b/docs/generators/tiny-cpp.md @@ -165,7 +165,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl |Maps|✗|ToolingExtension |CollectionFormat|✓|OAS2 |CollectionFormatMulti|✓|OAS2 -|Enum|✓|OAS2,OAS3 +|Enum|✗|OAS2,OAS3 |ArrayOfEnum|✓|ToolingExtension |ArrayOfModel|✓|ToolingExtension |ArrayOfCollectionOfPrimitives|✓|ToolingExtension 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 4766e2a9c91..e84cd331369 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 @@ -66,6 +66,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 MAP_FILE_BINARY_TO_DATA = "mapFileBinaryToData"; protected static final String LIBRARY_ALAMOFIRE = "alamofire"; protected static final String LIBRARY_URLSESSION = "urlsession"; protected static final String RESPONSE_LIBRARY_PROMISE_KIT = "PromiseKit"; @@ -84,6 +85,7 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig protected boolean useBacktickEscapes = false; protected boolean generateModelAdditionalProperties = true; protected boolean hashableModels = true; + protected boolean mapFileBinaryToData = false; protected String[] responseAs = new String[0]; protected String sourceFolder = swiftPackagePath; protected HashSet objcReservedWords; @@ -120,7 +122,6 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig "Bool", "Void", "String", - "URL", "Data", "Date", "Character", @@ -286,6 +287,10 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig "Make hashable models (default: true)") .defaultValue(Boolean.TRUE.toString())); + cliOptions.add(new CliOption(MAP_FILE_BINARY_TO_DATA, + "Map File and Binary to Data (default: false)") + .defaultValue(Boolean.FALSE.toString())); + supportedLibraries.put(LIBRARY_URLSESSION, "[DEFAULT] HTTP client: URLSession"); supportedLibraries.put(LIBRARY_ALAMOFIRE, "HTTP client: Alamofire"); @@ -470,6 +475,15 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig } additionalProperties.put(HASHABLE_MODELS, hashableModels); + if (additionalProperties.containsKey(MAP_FILE_BINARY_TO_DATA)) { + setMapFileBinaryToData(convertPropertyToBooleanAndWriteBack(MAP_FILE_BINARY_TO_DATA)); + } + additionalProperties.put(MAP_FILE_BINARY_TO_DATA, mapFileBinaryToData); + if (mapFileBinaryToData) { + typeMapping.put("file", "Data"); + typeMapping.put("binary", "Data"); + } + setLenientTypeCast(convertPropertyToBooleanAndWriteBack(LENIENT_TYPE_CAST)); // make api and model doc path available in mustache template @@ -547,6 +561,14 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig } + public boolean isMapFileBinaryToData() { + return mapFileBinaryToData; + } + + public void setMapFileBinaryToData(boolean mapFileBinaryToData) { + this.mapFileBinaryToData = mapFileBinaryToData; + } + @Override protected boolean isReservedWord(String word) { return word != null && reservedWords.contains(word); //don't lowercase as super does 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 979da734eac..6e5f48ca377 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 @@ -95,6 +95,7 @@ public class Swift5OptionsProvider implements OptionsProvider { .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.MAP_FILE_BINARY_TO_DATA, "false") .build(); }