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

[swift] fix typos, minor code format enhancement #20387

Merged
merged 3 commits into from
Dec 31, 2024
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
2 changes: 1 addition & 1 deletion docs/generators/swift5.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|useCustomDateWithoutTime|Uses a custom type to decode and encode dates without time information to support OpenAPIs date format (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|
|validatable|Make validation rules and validator for model properies (default: true)| |true|
|validatable|Make validation rules and validator for model properties (default: true)| |true|

## IMPORT MAPPING

Expand Down
2 changes: 1 addition & 1 deletion docs/generators/swift6.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|useCustomDateWithoutTime|Uses a custom type to decode and encode dates without time information to support OpenAPIs date format (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: true).| |null|
|validatable|Make validation rules and validator for model properies (default: true)| |true|
|validatable|Make validation rules and validator for model properties (default: true)| |true|

## IMPORT MAPPING

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,43 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig
protected static final String RESPONSE_LIBRARY_COMBINE = "Combine";
protected static final String RESPONSE_LIBRARY_ASYNC_AWAIT = "AsyncAwait";
protected static final String[] RESPONSE_LIBRARIES = {RESPONSE_LIBRARY_PROMISE_KIT, RESPONSE_LIBRARY_RX_SWIFT, RESPONSE_LIBRARY_RESULT, RESPONSE_LIBRARY_COMBINE, RESPONSE_LIBRARY_ASYNC_AWAIT};
@Setter protected String projectName = "OpenAPIClient";
@Setter protected boolean nonPublicApi = false;
@Setter protected boolean objcCompatible = false;
@Setter protected boolean readonlyProperties = false;
@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;
@Setter protected boolean hashableModels = true;
@Setter protected boolean identifiableModels = true;
@Setter protected boolean useJsonEncodable = true;
@Getter @Setter
@Setter
protected String projectName = "OpenAPIClient";
@Setter
protected boolean nonPublicApi = false;
@Setter
protected boolean objcCompatible = false;
@Setter
protected boolean readonlyProperties = false;
@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;
@Setter
protected boolean hashableModels = true;
@Setter
protected boolean identifiableModels = true;
@Setter
protected boolean useJsonEncodable = true;
@Getter
@Setter
protected boolean mapFileBinaryToData = false;
@Setter protected boolean useCustomDateWithoutTime = false;
@Setter protected boolean validatable = true;
@Setter protected String[] responseAs = new String[0];
@Setter
protected boolean useCustomDateWithoutTime = false;
@Setter
protected boolean validatable = true;
@Setter
protected String[] responseAs = new String[0];
protected String sourceFolder = swiftPackagePath;
protected HashSet objcReservedWords;
protected String apiDocPath = "docs/";
Expand Down Expand Up @@ -322,7 +340,7 @@ public Swift5ClientCodegen() {
.defaultValue(Boolean.FALSE.toString()));

cliOptions.add(new CliOption(VALIDATABLE,
"Make validation rules and validator for model properies (default: true)")
"Make validation rules and validator for model properties (default: true)")
.defaultValue(Boolean.TRUE.toString()));

supportedLibraries.put(LIBRARY_URLSESSION, "[DEFAULT] HTTP client: URLSession");
Expand Down Expand Up @@ -1189,7 +1207,7 @@ public void postProcessFile(File file, String fileType) {
}
// only process files with swift extension
if ("swift".equals(FilenameUtils.getExtension(file.toString()))) {
this.executePostProcessor(new String[] {swiftPostProcessFile, file.toString()});
this.executePostProcessor(new String[]{swiftPostProcessFile, file.toString()});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,48 @@ public class Swift6ClientCodegen extends DefaultCodegen implements CodegenConfig
protected static final String RESPONSE_LIBRARY_COMBINE = "Combine";
protected static final String RESPONSE_LIBRARY_ASYNC_AWAIT = "AsyncAwait";
protected static final String RESPONSE_LIBRARY_OBJC_BLOCK = "ObjcBlock";
protected static final String[] RESPONSE_LIBRARIES = { RESPONSE_LIBRARY_ASYNC_AWAIT, RESPONSE_LIBRARY_COMBINE, RESPONSE_LIBRARY_RESULT, RESPONSE_LIBRARY_RX_SWIFT, RESPONSE_LIBRARY_OBJC_BLOCK, RESPONSE_LIBRARY_PROMISE_KIT };
@Setter protected String projectName = "OpenAPIClient";
@Setter protected boolean nonPublicApi = false;
@Setter protected boolean objcCompatible = false;
@Setter protected boolean readonlyProperties = false;
@Setter protected boolean swiftUseApiNamespace = false;
@Setter protected boolean useSPMFileStructure = true;
@Setter protected String swiftPackagePath = "Sources" + File.separator + projectName;
@Setter protected boolean oneOfUnknownDefaultCase = false;
@Setter protected boolean useClasses = false;
@Setter protected boolean useBacktickEscapes = false;
@Setter protected boolean generateModelAdditionalProperties = true;
@Setter protected boolean hashableModels = true;
@Setter protected boolean identifiableModels = true;
@Setter protected boolean useJsonEncodable = true;
@Getter @Setter protected boolean mapFileBinaryToData = false;
@Setter protected boolean useCustomDateWithoutTime = false;
@Setter protected boolean validatable = true;
@Setter protected boolean apiStaticMethod = true;
@Setter protected boolean combineDeferred = true;
@Setter protected String[] responseAs = { RESPONSE_LIBRARY_ASYNC_AWAIT };
protected static final String[] RESPONSE_LIBRARIES = {RESPONSE_LIBRARY_ASYNC_AWAIT, RESPONSE_LIBRARY_COMBINE, RESPONSE_LIBRARY_RESULT, RESPONSE_LIBRARY_RX_SWIFT, RESPONSE_LIBRARY_OBJC_BLOCK, RESPONSE_LIBRARY_PROMISE_KIT};
@Setter
protected String projectName = "OpenAPIClient";
@Setter
protected boolean nonPublicApi = false;
@Setter
protected boolean objcCompatible = false;
@Setter
protected boolean readonlyProperties = false;
@Setter
protected boolean swiftUseApiNamespace = false;
@Setter
protected boolean useSPMFileStructure = true;
@Setter
protected String swiftPackagePath = "Sources" + File.separator + projectName;
@Setter
protected boolean oneOfUnknownDefaultCase = false;
@Setter
protected boolean useClasses = false;
@Setter
protected boolean useBacktickEscapes = false;
@Setter
protected boolean generateModelAdditionalProperties = true;
@Setter
protected boolean hashableModels = true;
@Setter
protected boolean identifiableModels = true;
@Setter
protected boolean useJsonEncodable = true;
@Getter
@Setter
protected boolean mapFileBinaryToData = false;
@Setter
protected boolean useCustomDateWithoutTime = false;
@Setter
protected boolean validatable = true;
@Setter
protected boolean apiStaticMethod = true;
@Setter
protected boolean combineDeferred = true;
@Setter
protected String[] responseAs = {RESPONSE_LIBRARY_ASYNC_AWAIT};
protected String sourceFolder = swiftPackagePath;
protected HashSet objcReservedWords;
protected String apiDocPath = "docs/";
Expand Down Expand Up @@ -326,7 +347,7 @@ public Swift6ClientCodegen() {
.defaultValue(Boolean.FALSE.toString()));

cliOptions.add(new CliOption(VALIDATABLE,
"Make validation rules and validator for model properies (default: true)")
"Make validation rules and validator for model properties (default: true)")
.defaultValue(Boolean.TRUE.toString()));

cliOptions.add(new CliOption(API_STATIC_METHOD,
Expand Down Expand Up @@ -1222,7 +1243,7 @@ public void postProcessFile(File file, String fileType) {
}
// only process files with swift extension
if ("swift".equals(FilenameUtils.getExtension(file.toString()))) {
this.executePostProcessor(new String[] {swiftPostProcessFile, file.toString()});
this.executePostProcessor(new String[]{swiftPostProcessFile, file.toString()});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -746,8 +746,8 @@ protected void addVendorExtensions(CodegenParameter cp, CodegenOperation operati
CodegenModel baseModel = modelMaps.get(cp.items.dataType);
boolean isBaseTypeEnum = cp.items.isEnum || cp.isEnum || (baseModel != null && baseModel.isEnum);
cp.vendorExtensions.put("x-swift-is-base-type-enum", isBaseTypeEnum);
boolean isBaseTypeUdid = cp.items.isUuid || cp.isUuid;
cp.vendorExtensions.put("x-swift-is-base-type-udid", isBaseTypeUdid);
boolean isBaseTypeUuid = cp.items.isUuid || cp.isUuid;
cp.vendorExtensions.put("x-swift-is-base-type-uuid", isBaseTypeUuid);

boolean useEncoder = !isBaseTypeEnum && !cp.items.isString || (baseModel != null && !baseModel.isString);
cp.vendorExtensions.put("x-swift-use-encoder", useEncoder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ open class {{classname}} {
/// - defaultResponse: {{.}}{{/defaultResponse}}
{{#authMethods}}
/// - {{#isBasicBasic}}BASIC{{/isBasicBasic}}{{#isOAuth}}OAuth{{/isOAuth}}{{#isApiKey}}API Key{{/isApiKey}}:
/// - type: {{type}}{{#keyParamName}} {{keyParamName}} {{#isKeyInQuery}}(QUERY){{/isKeyInQuery}}{{#isKeyInHeaer}}(HEADER){{/isKeyInHeaer}}{{/keyParamName}}
/// - type: {{type}}{{#keyParamName}} {{keyParamName}} {{#isKeyInQuery}}(QUERY){{/isKeyInQuery}}{{#isKeyInHeader}}(HEADER){{/isKeyInHeader}}{{/keyParamName}}
/// - name: {{name}}
{{/authMethods}}
{{#hasResponseHeaders}}
Expand Down Expand Up @@ -212,4 +212,4 @@ open class {{classname}} {
}
{{/operation}}
}
{{/operations}}
{{/operations}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{#isDateTime}}OpenISO8601DateFormatter.shared.string(from: {{paramName}}){{/isDateTime}}{{#vendorExtensions.x-swift-use-encoder}}String(data: try self.encoder.encode({{paramName}}), encoding: .utf8) ?? ""{{/vendorExtensions.x-swift-use-encoder}}{{^vendorExtensions.x-swift-use-encoder}}{{#isArray}}{{paramName}}{{#vendorExtensions.x-swift-is-base-type-udid}}.map { $0.uuidString }{{/vendorExtensions.x-swift-is-base-type-udid}}{{#vendorExtensions.x-swift-is-base-type-enum}}.map { $0.rawValue }{{/vendorExtensions.x-swift-is-base-type-enum}}.joined(separator: ","){{/isArray}}{{^isArray}}{{#vendorExtensions.x-swift-is-enum-type}}{{paramName}}.rawValue{{/vendorExtensions.x-swift-is-enum-type}}{{^isEnum}}{{#isString}}{{#isUuid}}{{paramName}}.uuidString{{/isUuid}}{{^isUuid}}{{paramName}}{{/isUuid}}{{/isString}}{{#isInteger}}"\({{paramName}})"{{/isInteger}}{{#isDouble}}"\({{paramName}})"{{/isDouble}}{{#isFloat}}"\({{paramName}})"{{/isFloat}}{{#isNumber}}"\({{paramName}})"{{/isNumber}}{{#isLong}}"\({{paramName}})"{{/isLong}}{{#isBoolean}}{{paramName}} ? "true" : "false"{{/isBoolean}}{{/isEnum}}{{/isArray}}{{/vendorExtensions.x-swift-use-encoder}}
{{#isDateTime}}OpenISO8601DateFormatter.shared.string(from: {{paramName}}){{/isDateTime}}{{#vendorExtensions.x-swift-use-encoder}}String(data: try self.encoder.encode({{paramName}}), encoding: .utf8) ?? ""{{/vendorExtensions.x-swift-use-encoder}}{{^vendorExtensions.x-swift-use-encoder}}{{#isArray}}{{paramName}}{{#vendorExtensions.x-swift-is-base-type-uuid}}.map { $0.uuidString }{{/vendorExtensions.x-swift-is-base-type-uuid}}{{#vendorExtensions.x-swift-is-base-type-enum}}.map { $0.rawValue }{{/vendorExtensions.x-swift-is-base-type-enum}}.joined(separator: ","){{/isArray}}{{^isArray}}{{#vendorExtensions.x-swift-is-enum-type}}{{paramName}}.rawValue{{/vendorExtensions.x-swift-is-enum-type}}{{^isEnum}}{{#isString}}{{#isUuid}}{{paramName}}.uuidString{{/isUuid}}{{^isUuid}}{{paramName}}{{/isUuid}}{{/isString}}{{#isInteger}}"\({{paramName}})"{{/isInteger}}{{#isDouble}}"\({{paramName}})"{{/isDouble}}{{#isFloat}}"\({{paramName}})"{{/isFloat}}{{#isNumber}}"\({{paramName}})"{{/isNumber}}{{#isLong}}"\({{paramName}})"{{/isLong}}{{#isBoolean}}{{paramName}} ? "true" : "false"{{/isBoolean}}{{/isEnum}}{{/isArray}}{{/vendorExtensions.x-swift-use-encoder}}
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ import Foundation
/// - Throws: `ValidationError<NumericValidationErrorKind>` if the numeric is invalid against the rule.
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func validate<T: Comparable & BinaryInteger>(_ numeric: T, against rule: NumericRule<T>) throws -> T {
var error = ValidationError<NumericValidationErrorKind>(kinds: [])
if let minium = rule.minimum {
if !rule.exclusiveMinimum, minium > numeric {
if let minimum = rule.minimum {
if !rule.exclusiveMinimum, minimum > numeric {
error.kinds.insert(.minimum)
}
if rule.exclusiveMinimum, minium >= numeric {
if rule.exclusiveMinimum, minimum >= numeric {
error.kinds.insert(.minimum)
}
}
Expand Down Expand Up @@ -109,11 +109,11 @@ import Foundation
/// - Throws: `ValidationError<NumericValidationErrorKind>` if the numeric is invalid against the rule.
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func validate<T: Comparable & FloatingPoint>(_ numeric: T, against rule: NumericRule<T>) throws -> T {
var error = ValidationError<NumericValidationErrorKind>(kinds: [])
if let minium = rule.minimum {
if !rule.exclusiveMinimum, minium > numeric {
if let minimum = rule.minimum {
if !rule.exclusiveMinimum, minimum > numeric {
error.kinds.insert(.minimum)
}
if rule.exclusiveMinimum, minium >= numeric {
if rule.exclusiveMinimum, minimum >= numeric {
error.kinds.insert(.minimum)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ extension NumericRule: Sendable where T: Sendable {}
/// - Throws: `ValidationError<NumericValidationErrorKind>` if the numeric is invalid against the rule.
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func validate<T: Comparable & BinaryInteger>(_ numeric: T, against rule: NumericRule<T>) throws -> T {
var error = ValidationError<NumericValidationErrorKind>(kinds: [])
if let minium = rule.minimum {
if !rule.exclusiveMinimum, minium > numeric {
if let minimum = rule.minimum {
if !rule.exclusiveMinimum, minimum > numeric {
error.kinds.insert(.minimum)
}
if rule.exclusiveMinimum, minium >= numeric {
if rule.exclusiveMinimum, minimum >= numeric {
error.kinds.insert(.minimum)
}
}
Expand Down Expand Up @@ -110,11 +110,11 @@ extension NumericRule: Sendable where T: Sendable {}
/// - Throws: `ValidationError<NumericValidationErrorKind>` if the numeric is invalid against the rule.
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func validate<T: Comparable & FloatingPoint>(_ numeric: T, against rule: NumericRule<T>) throws -> T {
var error = ValidationError<NumericValidationErrorKind>(kinds: [])
if let minium = rule.minimum {
if !rule.exclusiveMinimum, minium > numeric {
if let minimum = rule.minimum {
if !rule.exclusiveMinimum, minimum > numeric {
error.kinds.insert(.minimum)
}
if rule.exclusiveMinimum, minium >= numeric {
if rule.exclusiveMinimum, minimum >= numeric {
error.kinds.insert(.minimum)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ open class PetAPI {
/// - GET /pet/{petId}
/// - Returns a single pet
/// - API Key:
/// - type: apiKey api_key
/// - type: apiKey api_key (HEADER)
/// - name: api_key
/// - parameter petId: (path) ID of pet to return
/// - returns: AnyPublisher<Pet, Error>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ open class StoreAPI {
/// - GET /store/inventory
/// - Returns a map of status codes to quantities
/// - API Key:
/// - type: apiKey api_key
/// - type: apiKey api_key (HEADER)
/// - name: api_key
/// - returns: AnyPublisher<[String: Int], Error>
open func getInventory() -> AnyPublisher<[String: Int], Error> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ open class UserAPI {
/// - POST /user
/// - This can only be done by the logged in user.
/// - API Key:
/// - type: apiKey api_key
/// - type: apiKey api_key (HEADER)
/// - name: api_key
/// - parameter user: (body) Created user object
/// - returns: AnyPublisher<Void, Error>
Expand Down Expand Up @@ -68,7 +68,7 @@ open class UserAPI {
/// - POST /user/createWithArray
/// -
/// - API Key:
/// - type: apiKey api_key
/// - type: apiKey api_key (HEADER)
/// - name: api_key
/// - parameter user: (body) List of user object
/// - returns: AnyPublisher<Void, Error>
Expand Down Expand Up @@ -104,7 +104,7 @@ open class UserAPI {
/// - POST /user/createWithList
/// -
/// - API Key:
/// - type: apiKey api_key
/// - type: apiKey api_key (HEADER)
/// - name: api_key
/// - parameter user: (body) List of user object
/// - returns: AnyPublisher<Void, Error>
Expand Down Expand Up @@ -155,7 +155,7 @@ open class UserAPI {
/// - DELETE /user/{username}
/// - This can only be done by the logged in user.
/// - API Key:
/// - type: apiKey api_key
/// - type: apiKey api_key (HEADER)
/// - name: api_key
/// - parameter username: (path) The name that needs to be deleted
/// - returns: AnyPublisher<Void, Error>
Expand Down Expand Up @@ -309,7 +309,7 @@ open class UserAPI {
/// - GET /user/logout
/// -
/// - API Key:
/// - type: apiKey api_key
/// - type: apiKey api_key (HEADER)
/// - name: api_key
/// - returns: AnyPublisher<Void, Error>
open func logoutUser() -> AnyPublisher<Void, Error> {
Expand Down Expand Up @@ -357,7 +357,7 @@ open class UserAPI {
/// - PUT /user/{username}
/// - This can only be done by the logged in user.
/// - API Key:
/// - type: apiKey api_key
/// - type: apiKey api_key (HEADER)
/// - name: api_key
/// - parameter username: (path) name that need to be deleted
/// - parameter user: (body) Updated user object
Expand Down
Loading
Loading