Skip to content

Commit

Permalink
[swift5] Add useSPMFileStructure (#9074)
Browse files Browse the repository at this point in the history
* [swift5] Add useSPMFileStructure

Signed-off-by: Ayman Bagabas <ayman.bagabas@gmail.com>

* [swift5] Add swiftPackagePath

Prioritize swiftPackagePath over useSPMFileStructure

* [swift5] Add cli options and update docs

* [swift5] Fix tests

* [swift5] Update XcodeGen source path

* [swift5] Update samples and docs

Add useSPMFileStructure to URLSession library
  • Loading branch information
aymanbagabas authored Apr 21, 2021
1 parent 139e9e4 commit 3894aa4
Show file tree
Hide file tree
Showing 125 changed files with 4,174 additions and 65 deletions.
1 change: 1 addition & 0 deletions bin/configs/swift5-urlsessionLibrary.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ additionalProperties:
podSummary: PetstoreClient
projectName: PetstoreClient
podHomepage: https://github.com/openapitools/openapi-generator
useSPMFileStructure: true
2 changes: 2 additions & 0 deletions docs/generators/swift5.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|responseAs|Optionally use libraries to manage response. Currently PromiseKit, RxSwift, Result, Combine are available.| |null|
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|swiftPackagePath|Set a custom source path instead of OpenAPIClient/Classes/OpenAPIs.| |null|
|swiftUseApiNamespace|Flag to make all the API classes inner-class of {{projectName}}API| |null|
|useSPMFileStructure|Use SPM file structure and set the source path to Sources/{{projectName}} (default: false).| |null|

## IMPORT MAPPING

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig
public static final String SWIFT_USE_API_NAMESPACE = "swiftUseApiNamespace";
public static final String DEFAULT_POD_AUTHORS = "OpenAPI Generator";
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";
protected static final String LIBRARY_ALAMOFIRE = "alamofire";
protected static final String LIBRARY_URLSESSION = "urlsession";
protected static final String RESPONSE_LIBRARY_PROMISE_KIT = "PromiseKit";
Expand All @@ -73,9 +75,11 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig
protected boolean objcCompatible = false;
protected boolean lenientTypeCast = false;
protected boolean readonlyProperties = false;
protected boolean swiftUseApiNamespace;
protected boolean swiftUseApiNamespace = false;
protected boolean useSPMFileStructure = false;
protected String swiftPackagePath = "Classes" + File.separator + "OpenAPIs";
protected String[] responseAs = new String[0];
protected String sourceFolder = "Classes" + File.separator + "OpenAPIs";
protected String sourceFolder = swiftPackagePath;
protected HashSet objcReservedWords;
protected String apiDocPath = "docs/";
protected String modelDocPath = "docs/";
Expand Down Expand Up @@ -261,6 +265,10 @@ public Swift5ClientCodegen() {
.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"
+ " and set the source path to Sources" + File.separator + "{{projectName}} (default: false)."));
cliOptions.add(new CliOption(SWIFT_PACKAGE_PATH, "Set a custom source path instead of "
+ projectName + File.separator + "Classes" + File.separator + "OpenAPIs" + "."));

supportedLibraries.put(LIBRARY_URLSESSION, "[DEFAULT] HTTP client: URLSession");
supportedLibraries.put(LIBRARY_ALAMOFIRE, "HTTP client: Alamofire");
Expand Down Expand Up @@ -414,6 +422,16 @@ public void processOpts() {
additionalProperties.put(POD_AUTHORS, DEFAULT_POD_AUTHORS);
}

if (additionalProperties.containsKey(USE_SPM_FILE_STRUCTURE)) {
setUseSPMFileStructure(convertPropertyToBooleanAndWriteBack(USE_SPM_FILE_STRUCTURE));
sourceFolder = "Sources" + File.separator + projectName;
}

if (additionalProperties.containsKey(SWIFT_PACKAGE_PATH) && ((String)additionalProperties.get(SWIFT_PACKAGE_PATH)).length() > 0) {
setSwiftPackagePath((String)additionalProperties.get(SWIFT_PACKAGE_PATH));
sourceFolder = swiftPackagePath;
}

setLenientTypeCast(convertPropertyToBooleanAndWriteBack(LENIENT_TYPE_CAST));

// make api and model doc path available in mustache template
Expand Down Expand Up @@ -806,6 +824,14 @@ public void setSwiftUseApiNamespace(boolean swiftUseApiNamespace) {
this.swiftUseApiNamespace = swiftUseApiNamespace;
}

public void setUseSPMFileStructure(boolean useSPMFileStructure) {
this.useSPMFileStructure = useSPMFileStructure;
}

public void setSwiftPackagePath(String swiftPackagePath) {
this.swiftPackagePath = swiftPackagePath;
}

@Override
public String toEnumValue(String value, String datatype) {
// for string, array of string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ let package = Package(
.target(
name: "{{projectName}}",
dependencies: [{{#useAlamofire}}"Alamofire", {{/useAlamofire}}{{#usePromiseKit}}"PromiseKit", {{/usePromiseKit}}{{#useRxSwift}}"RxSwift"{{/useRxSwift}}],
path: "{{projectName}}/Classes"
path: "{{#swiftPackagePath}}{{swiftPackagePath}}{{/swiftPackagePath}}{{^swiftPackagePath}}{{#useSPMFileStructure}}Sources/{{projectName}}{{/useSPMFileStructure}}{{^useSPMFileStructure}}{{projectName}}/Classes{{/useSPMFileStructure}}{{/swiftPackagePath}}"
),
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Pod::Spec.new do |s|
{{#podDocumentationURL}}
s.documentation_url = '{{podDocumentationURL}}'
{{/podDocumentationURL}}
s.source_files = '{{projectName}}/Classes/**/*.swift'
s.source_files = '{{#swiftPackagePath}}{{swiftPackagePath}}{{/swiftPackagePath}}{{^swiftPackagePath}}{{#useSPMFileStructure}}Sources/{{projectName}}{{/useSPMFileStructure}}{{^useSPMFileStructure}}{{projectName}}/Classes{{/useSPMFileStructure}}{{/swiftPackagePath}}/**/*.swift'
{{#usePromiseKit}}
s.dependency 'PromiseKit/CorePromise', '~> 6.13.1'
{{/usePromiseKit}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ targets:
type: framework
platform: iOS
deploymentTarget: "9.0"
sources: [{{projectName}}]
sources: [{{#swiftPackagePath}}{{swiftPackagePath}}{{/swiftPackagePath}}{{^swiftPackagePath}}{{#useSPMFileStructure}}Sources{{/useSPMFileStructure}}{{^useSPMFileStructure}}{{projectName}}{{/useSPMFileStructure}}{{/swiftPackagePath}}]
info:
path: ./Info.plist
version: {{#podVersion}}{{podVersion}}{{/podVersion}}{{^podVersion}}{{#apiInfo}}{{version}}{{/apiInfo}}{{^apiInfo}}}0.0.1{{/apiInfo}}{{/podVersion}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.languages.Swift5ClientCodegen;

import java.io.File;
import java.util.Map;

public class Swift5OptionsProvider implements OptionsProvider {
Expand Down Expand Up @@ -48,6 +49,8 @@ public class Swift5OptionsProvider implements OptionsProvider {
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";
public static final String USE_SPM_FILE_STRUCTURE_VALUE = "false";
public static final String SWIFT_PACKAGE_PATH_VALUE = "";

@Override
public String getLanguage() {
Expand Down Expand Up @@ -84,6 +87,8 @@ public Map<String, String> createOptions() {
.put(CodegenConstants.LIBRARY, LIBRARY_VALUE)
.put(CodegenConstants.LEGACY_DISCRIMINATOR_BEHAVIOR, "true")
.put(CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT, "true")
.put(Swift5ClientCodegen.USE_SPM_FILE_STRUCTURE, USE_SPM_FILE_STRUCTURE_VALUE)
.put(Swift5ClientCodegen.SWIFT_PACKAGE_PATH, SWIFT_PACKAGE_PATH_VALUE)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,64 @@
Cartfile
Package.swift
PetstoreClient.podspec
PetstoreClient/Classes/OpenAPIs/APIHelper.swift
PetstoreClient/Classes/OpenAPIs/APIs.swift
PetstoreClient/Classes/OpenAPIs/APIs/AnotherFakeAPI.swift
PetstoreClient/Classes/OpenAPIs/APIs/FakeAPI.swift
PetstoreClient/Classes/OpenAPIs/APIs/FakeClassnameTags123API.swift
PetstoreClient/Classes/OpenAPIs/APIs/PetAPI.swift
PetstoreClient/Classes/OpenAPIs/APIs/StoreAPI.swift
PetstoreClient/Classes/OpenAPIs/APIs/UserAPI.swift
PetstoreClient/Classes/OpenAPIs/CodableHelper.swift
PetstoreClient/Classes/OpenAPIs/Configuration.swift
PetstoreClient/Classes/OpenAPIs/Extensions.swift
PetstoreClient/Classes/OpenAPIs/JSONDataEncoding.swift
PetstoreClient/Classes/OpenAPIs/JSONEncodingHelper.swift
PetstoreClient/Classes/OpenAPIs/Models.swift
PetstoreClient/Classes/OpenAPIs/Models/AdditionalPropertiesClass.swift
PetstoreClient/Classes/OpenAPIs/Models/Animal.swift
PetstoreClient/Classes/OpenAPIs/Models/AnimalFarm.swift
PetstoreClient/Classes/OpenAPIs/Models/ApiResponse.swift
PetstoreClient/Classes/OpenAPIs/Models/ArrayOfArrayOfNumberOnly.swift
PetstoreClient/Classes/OpenAPIs/Models/ArrayOfNumberOnly.swift
PetstoreClient/Classes/OpenAPIs/Models/ArrayTest.swift
PetstoreClient/Classes/OpenAPIs/Models/Capitalization.swift
PetstoreClient/Classes/OpenAPIs/Models/Cat.swift
PetstoreClient/Classes/OpenAPIs/Models/CatAllOf.swift
PetstoreClient/Classes/OpenAPIs/Models/Category.swift
PetstoreClient/Classes/OpenAPIs/Models/ClassModel.swift
PetstoreClient/Classes/OpenAPIs/Models/Client.swift
PetstoreClient/Classes/OpenAPIs/Models/Dog.swift
PetstoreClient/Classes/OpenAPIs/Models/DogAllOf.swift
PetstoreClient/Classes/OpenAPIs/Models/EnumArrays.swift
PetstoreClient/Classes/OpenAPIs/Models/EnumClass.swift
PetstoreClient/Classes/OpenAPIs/Models/EnumTest.swift
PetstoreClient/Classes/OpenAPIs/Models/File.swift
PetstoreClient/Classes/OpenAPIs/Models/FileSchemaTestClass.swift
PetstoreClient/Classes/OpenAPIs/Models/FormatTest.swift
PetstoreClient/Classes/OpenAPIs/Models/HasOnlyReadOnly.swift
PetstoreClient/Classes/OpenAPIs/Models/List.swift
PetstoreClient/Classes/OpenAPIs/Models/MapTest.swift
PetstoreClient/Classes/OpenAPIs/Models/MixedPropertiesAndAdditionalPropertiesClass.swift
PetstoreClient/Classes/OpenAPIs/Models/Model200Response.swift
PetstoreClient/Classes/OpenAPIs/Models/Name.swift
PetstoreClient/Classes/OpenAPIs/Models/NumberOnly.swift
PetstoreClient/Classes/OpenAPIs/Models/Order.swift
PetstoreClient/Classes/OpenAPIs/Models/OuterComposite.swift
PetstoreClient/Classes/OpenAPIs/Models/OuterEnum.swift
PetstoreClient/Classes/OpenAPIs/Models/Pet.swift
PetstoreClient/Classes/OpenAPIs/Models/ReadOnlyFirst.swift
PetstoreClient/Classes/OpenAPIs/Models/Return.swift
PetstoreClient/Classes/OpenAPIs/Models/SpecialModelName.swift
PetstoreClient/Classes/OpenAPIs/Models/StringBooleanMap.swift
PetstoreClient/Classes/OpenAPIs/Models/Tag.swift
PetstoreClient/Classes/OpenAPIs/Models/TypeHolderDefault.swift
PetstoreClient/Classes/OpenAPIs/Models/TypeHolderExample.swift
PetstoreClient/Classes/OpenAPIs/Models/User.swift
PetstoreClient/Classes/OpenAPIs/OpenISO8601DateFormatter.swift
PetstoreClient/Classes/OpenAPIs/SynchronizedDictionary.swift
PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift
README.md
Sources/PetstoreClient/APIHelper.swift
Sources/PetstoreClient/APIs.swift
Sources/PetstoreClient/APIs/AnotherFakeAPI.swift
Sources/PetstoreClient/APIs/FakeAPI.swift
Sources/PetstoreClient/APIs/FakeClassnameTags123API.swift
Sources/PetstoreClient/APIs/PetAPI.swift
Sources/PetstoreClient/APIs/StoreAPI.swift
Sources/PetstoreClient/APIs/UserAPI.swift
Sources/PetstoreClient/CodableHelper.swift
Sources/PetstoreClient/Configuration.swift
Sources/PetstoreClient/Extensions.swift
Sources/PetstoreClient/JSONDataEncoding.swift
Sources/PetstoreClient/JSONEncodingHelper.swift
Sources/PetstoreClient/Models.swift
Sources/PetstoreClient/Models/AdditionalPropertiesClass.swift
Sources/PetstoreClient/Models/Animal.swift
Sources/PetstoreClient/Models/AnimalFarm.swift
Sources/PetstoreClient/Models/ApiResponse.swift
Sources/PetstoreClient/Models/ArrayOfArrayOfNumberOnly.swift
Sources/PetstoreClient/Models/ArrayOfNumberOnly.swift
Sources/PetstoreClient/Models/ArrayTest.swift
Sources/PetstoreClient/Models/Capitalization.swift
Sources/PetstoreClient/Models/Cat.swift
Sources/PetstoreClient/Models/CatAllOf.swift
Sources/PetstoreClient/Models/Category.swift
Sources/PetstoreClient/Models/ClassModel.swift
Sources/PetstoreClient/Models/Client.swift
Sources/PetstoreClient/Models/Dog.swift
Sources/PetstoreClient/Models/DogAllOf.swift
Sources/PetstoreClient/Models/EnumArrays.swift
Sources/PetstoreClient/Models/EnumClass.swift
Sources/PetstoreClient/Models/EnumTest.swift
Sources/PetstoreClient/Models/File.swift
Sources/PetstoreClient/Models/FileSchemaTestClass.swift
Sources/PetstoreClient/Models/FormatTest.swift
Sources/PetstoreClient/Models/HasOnlyReadOnly.swift
Sources/PetstoreClient/Models/List.swift
Sources/PetstoreClient/Models/MapTest.swift
Sources/PetstoreClient/Models/MixedPropertiesAndAdditionalPropertiesClass.swift
Sources/PetstoreClient/Models/Model200Response.swift
Sources/PetstoreClient/Models/Name.swift
Sources/PetstoreClient/Models/NumberOnly.swift
Sources/PetstoreClient/Models/Order.swift
Sources/PetstoreClient/Models/OuterComposite.swift
Sources/PetstoreClient/Models/OuterEnum.swift
Sources/PetstoreClient/Models/Pet.swift
Sources/PetstoreClient/Models/ReadOnlyFirst.swift
Sources/PetstoreClient/Models/Return.swift
Sources/PetstoreClient/Models/SpecialModelName.swift
Sources/PetstoreClient/Models/StringBooleanMap.swift
Sources/PetstoreClient/Models/Tag.swift
Sources/PetstoreClient/Models/TypeHolderDefault.swift
Sources/PetstoreClient/Models/TypeHolderExample.swift
Sources/PetstoreClient/Models/User.swift
Sources/PetstoreClient/OpenISO8601DateFormatter.swift
Sources/PetstoreClient/SynchronizedDictionary.swift
Sources/PetstoreClient/URLSessionImplementations.swift
docs/AdditionalPropertiesClass.md
docs/Animal.md
docs/AnimalFarm.md
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ let package = Package(
.target(
name: "PetstoreClient",
dependencies: [],
path: "PetstoreClient/Classes"
path: "Sources/PetstoreClient"
),
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ Pod::Spec.new do |s|
s.license = 'Proprietary'
s.homepage = 'https://github.com/openapitools/openapi-generator'
s.summary = 'PetstoreClient'
s.source_files = 'PetstoreClient/Classes/**/*.swift'
s.source_files = 'Sources/PetstoreClient/**/*.swift'
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// ClassModel.swift
//
// Generated by openapi-generator
// https://openapi-generator.tech
//

import Foundation

/** Model for testing model with \&quot;_class\&quot; property */
public struct ClassModel: Codable, Hashable {

public var &#x60;class&#x60;: String?

public init(&#x60;class&#x60;: String? = nil) {
self.&#x60;class&#x60; = &#x60;class&#x60;
}

public enum CodingKeys: String, CodingKey, CaseIterable {
case &#x60;class&#x60; = "_class"
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Model200Response.swift
//
// Generated by openapi-generator
// https://openapi-generator.tech
//

import Foundation

/** Model for testing model name starting with number */
public struct Model200Response: Codable, Hashable {

public var name: Int?
public var &#x60;class&#x60;: String?

public init(name: Int? = nil, &#x60;class&#x60;: String? = nil) {
self.name = name
self.&#x60;class&#x60; = &#x60;class&#x60;
}

public enum CodingKeys: String, CodingKey, CaseIterable {
case name
case &#x60;class&#x60; = "class"
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Return.swift
//
// Generated by openapi-generator
// https://openapi-generator.tech
//

import Foundation

/** Model for testing reserved words */
public struct Return: Codable, Hashable {

public var &#x60;return&#x60;: Int?

public init(&#x60;return&#x60;: Int? = nil) {
self.&#x60;return&#x60; = &#x60;return&#x60;
}

public enum CodingKeys: String, CodingKey, CaseIterable {
case &#x60;return&#x60; = "return"
}

}
Loading

0 comments on commit 3894aa4

Please sign in to comment.