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

[swift5][client] Fix module and class with same name #10387

Merged
merged 2 commits into from Sep 14, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/configs/swift5-resultLibrary.yaml
Expand Up @@ -9,3 +9,4 @@ additionalProperties:
podSummary: PetstoreClient
projectName: PetstoreClient
podHomepage: https://github.com/openapitools/openapi-generator
removeMigrationProjectNameClass: true
1 change: 1 addition & 0 deletions docs/generators/swift5.md
Expand Up @@ -33,6 +33,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
|projectName|Project name in Xcode| |null|
|readonlyProperties|Make properties readonly (default: false)| |null|
|removeMigrationProjectNameClass|Make properties removeMigrationProjectNameClass (default: false)| |null|
|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|
Expand Down
Expand Up @@ -58,6 +58,7 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig
public static final String POD_SCREENSHOTS = "podScreenshots";
public static final String POD_DOCUMENTATION_URL = "podDocumentationURL";
public static final String READONLY_PROPERTIES = "readonlyProperties";
public static final String REMOVE_MIGRATION_PROJECT_NAME_CLASS = "removeMigrationProjectNameClass";
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";
Expand All @@ -81,6 +82,7 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig
protected boolean objcCompatible = false;
protected boolean lenientTypeCast = false;
protected boolean readonlyProperties = false;
protected boolean removeMigrationProjectNameClass = false;
protected boolean swiftUseApiNamespace = false;
protected boolean useSPMFileStructure = false;
protected String swiftPackagePath = "Classes" + File.separator + "OpenAPIs";
Expand Down Expand Up @@ -263,6 +265,8 @@ public Swift5ClientCodegen() {
"Documentation URL used for Podspec"));
cliOptions.add(new CliOption(READONLY_PROPERTIES, "Make properties "
+ "readonly (default: false)"));
cliOptions.add(new CliOption(REMOVE_MIGRATION_PROJECT_NAME_CLASS, "Make properties "
+ "removeMigrationProjectNameClass (default: false)"));
cliOptions.add(new CliOption(SWIFT_USE_API_NAMESPACE,
"Flag to make all the API classes inner-class "
+ "of {{projectName}}API"));
Expand Down Expand Up @@ -447,8 +451,14 @@ public void processOpts() {
}
additionalProperties.put(READONLY_PROPERTIES, readonlyProperties);

// Setup removeMigrationProjectNameClass option, which keeps or remove the projectName class
if (additionalProperties.containsKey(REMOVE_MIGRATION_PROJECT_NAME_CLASS)) {
setRemoveMigrationProjectNameClass(convertPropertyToBooleanAndWriteBack(REMOVE_MIGRATION_PROJECT_NAME_CLASS));
}
additionalProperties.put(REMOVE_MIGRATION_PROJECT_NAME_CLASS, removeMigrationProjectNameClass);

// Setup swiftUseApiNamespace option, which makes all the API
// classes inner-class of {{projectName}}
// classes inner-class of {{projectName}}API
if (additionalProperties.containsKey(SWIFT_USE_API_NAMESPACE)) {
setSwiftUseApiNamespace(convertPropertyToBooleanAndWriteBack(SWIFT_USE_API_NAMESPACE));
}
Expand Down Expand Up @@ -901,6 +911,10 @@ public void setReadonlyProperties(boolean readonlyProperties) {
this.readonlyProperties = readonlyProperties;
}

public void setRemoveMigrationProjectNameClass(boolean removeMigrationProjectNameClass) {
this.removeMigrationProjectNameClass = removeMigrationProjectNameClass;
}

public void setResponseAs(String[] responseAs) {
this.responseAs = responseAs;
}
Expand Down
22 changes: 13 additions & 9 deletions modules/openapi-generator/src/main/resources/swift5/APIs.mustache
Expand Up @@ -4,15 +4,19 @@
// https://openapi-generator.tech
//

import Foundation
{{#useVapor}}
import Foundation{{#useVapor}}
import Vapor
{{/useVapor}}
{{/useVapor}}{{^removeMigrationProjectNameClass}}

@available(*, deprecated, renamed: "{{projectName}}")
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} typealias {{projectName}}API = {{projectName}}
// We reverted the change of {{projectName}}API to {{projectName}} introduced in https://github.com/OpenAPITools/openapi-generator/pull/9624
// Because it was causing the following issue https://github.com/OpenAPITools/openapi-generator/issues/9953
// If you are affected by this issue, please consider removing the following two lines,
// By setting the option removeMigrationProjectNameClass to true in the generator
@available(*, deprecated, renamed: "{{projectName}}API")
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} typealias {{projectName}} = {{projectName}}API
{{/removeMigrationProjectNameClass}}

{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class {{projectName}} {
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class {{projectName}}API {
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var basePath = "{{{basePath}}}"
{{#useVapor}}
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var customHeaders: HTTPHeaders = [:]
Expand Down Expand Up @@ -44,7 +48,7 @@ import Vapor
self.parameters = parameters
self.headers = headers

addHeaders({{projectName}}.customHeaders)
addHeaders({{projectName}}API.customHeaders)
}

{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func addHeaders(_ aHeaders: [String: String]) {
Expand All @@ -53,7 +57,7 @@ import Vapor
}
}

{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func execute(_ apiResponseQueue: DispatchQueue = {{projectName}}.apiResponseQueue, _ completion: @escaping (_ result: Swift.Result<Response<T>, Error>) -> Void) { }
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func execute(_ apiResponseQueue: DispatchQueue = {{projectName}}API.apiResponseQueue, _ completion: @escaping (_ result: Swift.Result<Response<T>, Error>) -> Void) { }

{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} func addHeader(name: String, value: String) -> Self {
if !value.isEmpty {
Expand All @@ -63,7 +67,7 @@ import Vapor
}

{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func addCredential() -> Self {
credential = {{projectName}}.credential
credential = {{projectName}}API.credential
return self
}
}
Expand Down
Expand Up @@ -8,10 +8,10 @@ import Foundation{{#useVapor}}
import Vapor{{/useVapor}}

{{#swiftUseApiNamespace}}
@available(*, deprecated, renamed: "{{projectName}}.Configuration")
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} typealias Configuration = {{projectName}}.Configuration
@available(*, deprecated, renamed: "{{projectName}}API.Configuration")
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} typealias Configuration = {{projectName}}API.Configuration

extension {{projectName}} {
extension {{projectName}}API {
{{/swiftUseApiNamespace}}
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class Configuration {
{{#useVapor}}{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var apiClient: Vapor.Client? = nil
Expand Down