Skip to content
Open
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
66 changes: 66 additions & 0 deletions docs/generators/swift5.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,53 +113,119 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>#line</li>
<li>#selector</li>
<li>#sourceLocation</li>
<li>APIHelper</li>
<li>AlamofireDecodableRequestBuilder</li>
<li>AlamofireRequestBuilder</li>
<li>AlamofireRequestBuilderFactory</li>
<li>Any</li>
<li>AnyHashable</li>
<li>AnyObject</li>
<li>AnyResponseSerializer</li>
<li>Array</li>
<li>ArrayRule</li>
<li>ArrayValidationErrorKind</li>
<li>Bool</li>
<li>COLUMN</li>
<li>Calendar</li>
<li>CaseIterableDefaultsLast</li>
<li>Character</li>
<li>Class</li>
<li>ClosedRange</li>
<li>Codable</li>
<li>CodableHelper</li>
<li>Configuration</li>
<li>CountableClosedRange</li>
<li>CountableRange</li>
<li>Data</li>
<li>DateFormatter</li>
<li>Decodable</li>
<li>DecodableRequestBuilderError</li>
<li>Dictionary</li>
<li>DispatchQueue</li>
<li>Double</li>
<li>DownloadException</li>
<li>Encodable</li>
<li>Error</li>
<li>ErrorResponse</li>
<li>FILE</li>
<li>FUNCTION</li>
<li>FileManager</li>
<li>Float</li>
<li>Float32</li>
<li>Float64</li>
<li>Float80</li>
<li>HTTPMethod</li>
<li>HTTPURLResponse</li>
<li>Int</li>
<li>Int16</li>
<li>Int32</li>
<li>Int64</li>
<li>Int8</li>
<li>JSONDataEncoding</li>
<li>JSONDecoder</li>
<li>JSONEncodable</li>
<li>JSONEncoder</li>
<li>JSONEncodingHelper</li>
<li>KeyedDecodingContainerProtocol</li>
<li>KeyedEncodingContainerProtocol</li>
<li>LINE</li>
<li>Locale</li>
<li>NSCoder</li>
<li>NSDecimalNumber</li>
<li>NSNumber</li>
<li>NSObject</li>
<li>NSRecursiveLock</li>
<li>NSRegularExpression</li>
<li>NSString</li>
<li>NullEncodable</li>
<li>NumericRule</li>
<li>NumericValidationErrorKind</li>
<li>OpenISO8601DateFormatter</li>
<li>OptionSet</li>
<li>Optional</li>
<li>ParameterEncoding</li>
<li>Progress</li>
<li>Protocol</li>
<li>Range</li>
<li>RequestBuilder</li>
<li>RequestBuilderFactory</li>
<li>RequestTask</li>
<li>Response</li>
<li>Self</li>
<li>Set</li>
<li>StaticString</li>
<li>String</li>
<li>StringRule</li>
<li>StringValidationErrorKind</li>
<li>SynchronizedDictionary</li>
<li>TimeZone</li>
<li>Type</li>
<li>UInt</li>
<li>UInt16</li>
<li>UInt32</li>
<li>UInt64</li>
<li>UInt8</li>
<li>URL</li>
<li>URLAuthenticationChallenge</li>
<li>URLComponents</li>
<li>URLCredential</li>
<li>URLQueryItem</li>
<li>URLRequest</li>
<li>URLResponse</li>
<li>URLSession</li>
<li>URLSessionConfiguration</li>
<li>URLSessionDataTask</li>
<li>URLSessionDataTaskProtocol</li>
<li>URLSessionDecodableRequestBuilder</li>
<li>URLSessionProtocol</li>
<li>URLSessionRequestBuilder</li>
<li>URLSessionRequestBuilderFactory</li>
<li>URLSessionTask</li>
<li>URLSessionTaskDelegate</li>
<li>Unicode</li>
<li>UnknownCaseCheckable</li>
<li>ValidationError</li>
<li>Validator</li>
<li>Void</li>
<li>_</li>
<li>as</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,22 @@ public Swift5ClientCodegen() {

reservedWords = new HashSet<>(
Arrays.asList(
// name used by swift client
// Types declared by the generated code itself (support files and
// per-library implementations). A model with one of these names
// would be an invalid redeclaration of the client's own type.
"ErrorResponse", "Response",
"APIHelper", "AlamofireDecodableRequestBuilder", "AlamofireRequestBuilder",
"AlamofireRequestBuilderFactory", "AnyResponseSerializer", "ArrayRule",
"ArrayValidationErrorKind", "CaseIterableDefaultsLast", "CodableHelper",
"Configuration", "DecodableRequestBuilderError", "DownloadException",
"HTTPMethod", "JSONDataEncoding", "JSONEncodable", "JSONEncodingHelper",
"NullEncodable", "NumericRule", "NumericValidationErrorKind",
"OpenISO8601DateFormatter", "ParameterEncoding", "RequestBuilder",
"RequestBuilderFactory", "RequestTask", "StringRule",
"StringValidationErrorKind", "SynchronizedDictionary", "UnknownCaseCheckable",
"URLSessionDataTaskProtocol", "URLSessionDecodableRequestBuilder",
"URLSessionProtocol", "URLSessionRequestBuilder",
"URLSessionRequestBuilderFactory", "ValidationError", "Validator",

// Swift keywords. This list is taken from here:
// https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html#//apple_ref/doc/uid/TP40014097-CH30-ID410
Expand Down Expand Up @@ -251,8 +265,20 @@ public Swift5ClientCodegen() {
// Collections
"Array", "Dictionary", "Set", "OptionSet", "CountableRange", "CountableClosedRange",

// The following are commonly-used Foundation types
// The following are commonly-used Foundation (and stdlib) types that
// the generated support files reference unqualified: a model with one
// of these names would shadow the real type inside the generated
// module and break the client's own code.
"URL", "Data", "Codable", "Encodable", "Decodable",
"AnyHashable", "Calendar", "DateFormatter", "DispatchQueue", "FileManager",
"HTTPURLResponse", "JSONDecoder", "JSONEncoder",
"KeyedDecodingContainerProtocol", "KeyedEncodingContainerProtocol",
"Locale", "NSCoder", "NSDecimalNumber", "NSNumber", "NSObject",
"NSRecursiveLock", "NSRegularExpression", "NSString", "Progress",
"TimeZone", "URLAuthenticationChallenge", "URLComponents", "URLCredential",
"URLQueryItem", "URLRequest", "URLResponse", "URLSession",
"URLSessionConfiguration", "URLSessionDataTask", "URLSessionTask",
"URLSessionTaskDelegate",

// The following are other words we want to reserve
"Void", "AnyObject", "Class", "dynamicType", "COLUMN", "FILE", "FUNCTION", "LINE"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,31 @@ public void dateWithoutTimeTest() {
Assert.assertEquals(op.bodyParam.dataType, "OpenAPIDateWithoutTime");
}

@Test(description = "model names colliding with types declared by the generated client are renamed", enabled = true)
public void reservedTypeNamesDeclaredByClientTest() {
final DefaultCodegen codegen = new Swift5ClientCodegen();

// Names declared by the generated support files (Validation.swift, Models.swift, ...):
// a model with such a name would be an invalid redeclaration of the client's own type.
Assert.assertEquals(codegen.toModelName("ValidationError"), "ModelValidationError");
Assert.assertEquals(codegen.toModelName("Validator"), "ModelValidator");
Assert.assertEquals(codegen.toModelName("Configuration"), "ModelConfiguration");
Assert.assertEquals(codegen.toModelName("RequestBuilder"), "ModelRequestBuilder");
}

@Test(description = "model names shadowing Foundation types used by the generated client are renamed", enabled = true)
public void reservedFoundationTypeNamesTest() {
final DefaultCodegen codegen = new Swift5ClientCodegen();

// Foundation types the generated support files reference unqualified
// (e.g. OpenISO8601DateFormatter.swift assigns `formatter.locale = Locale(...)`):
// a model with such a name would shadow the Foundation type inside the module.
Assert.assertEquals(codegen.toModelName("Locale"), "ModelLocale");
Assert.assertEquals(codegen.toModelName("DateFormatter"), "ModelDateFormatter");
Assert.assertEquals(codegen.toModelName("TimeZone"), "ModelTimeZone");
Assert.assertEquals(codegen.toModelName("URLSession"), "ModelURLSession");
}

@Test(description = "type from languageSpecificPrimitives should not be prefixed", enabled = true)
public void prefixExceptionTest() {
final DefaultCodegen codegen = new Swift5ClientCodegen();
Expand Down