Skip to content
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion bin/configs/swift5-default.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
generatorName: swift5
outputDir: samples/client/petstore/swift5/default
inputSpec: modules/openapi-generator/src/test/resources/2_0/swift/petstore-with-fake-endpoints-models-for-testing.yaml
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml
templateDir: modules/openapi-generator/src/main/resources/swift5
generateAliasAsModel: true
additionalProperties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ public Swift5ClientCodegen() {
typeMapping.put("UUID", "UUID");
typeMapping.put("URI", "String");
typeMapping.put("decimal", "Decimal");
typeMapping.put("object", "Any");
typeMapping.put("AnyType", "Any");
typeMapping.put("object", "AnyCodable");
typeMapping.put("AnyType", "AnyCodable");

importMapping = new HashMap<>();

Expand Down Expand Up @@ -342,7 +342,15 @@ protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel,
final Schema additionalProperties = getAdditionalProperties(schema);

if (additionalProperties != null) {
codegenModel.additionalPropertiesType = getSchemaType(additionalProperties);
Schema inner = null;
if (ModelUtils.isArraySchema(schema)) {
Copy link
Contributor

@4brunu 4brunu Apr 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aymanbagabas could you please explain what this change does? (the code block, not the line itself)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was getting the wrong inner type for additionalProperties when the schema is either an array or a map.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for explaining the change 🙂

ArraySchema ap = (ArraySchema) schema;
inner = ap.getItems();
} else if (ModelUtils.isMapSchema(schema)) {
inner = getAdditionalProperties(schema);
}

codegenModel.additionalPropertiesType = inner != null ? getTypeDeclaration(inner) : getSchemaType(additionalProperties);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{#useAlamofire}}github "Alamofire/Alamofire" ~> 4.9.1{{/useAlamofire}}{{#usePromiseKit}}
github "mxcl/PromiseKit" ~> 6.13.1{{/usePromiseKit}}{{#useRxSwift}}
github "ReactiveX/RxSwift" ~> 5.1.1{{/useRxSwift}}
github "Flight-School/AnyCodable" ~> 0.4.0
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
// https://openapi-generator.tech
//

import Foundation{{#usePromiseKit}}
import Foundation
import AnyCodable{{#usePromiseKit}}
import PromiseKit{{/usePromiseKit}}

extension Bool: JSONEncodable {
Expand Down Expand Up @@ -53,6 +54,12 @@ extension Array: JSONEncodable {
}
}

extension Set: JSONEncodable {
func encodeToJSON() -> Any {
return Array(self).encodeToJSON()
}
}

extension Dictionary: JSONEncodable {
func encodeToJSON() -> Any {
var dictionary = [AnyHashable: Any]()
Expand Down Expand Up @@ -193,3 +200,44 @@ extension RequestBuilder {
return deferred.promise
}
}{{/usePromiseKit}}

extension AnyCodable: Hashable {
public func hash(into hasher: inout Hasher) {
switch value {
case let value as Bool:
hasher.combine(value)
case let value as Int:
hasher.combine(value)
case let value as Int8:
hasher.combine(value)
case let value as Int16:
hasher.combine(value)
case let value as Int32:
hasher.combine(value)
case let value as Int64:
hasher.combine(value)
case let value as UInt:
hasher.combine(value)
case let value as UInt8:
hasher.combine(value)
case let value as UInt16:
hasher.combine(value)
case let value as UInt32:
hasher.combine(value)
case let value as UInt64:
hasher.combine(value)
case let value as Float:
hasher.combine(value)
case let value as Double:
hasher.combine(value)
case let value as String:
hasher.combine(value)
case let value as [String: AnyCodable]:
hasher.combine(value)
case let value as [AnyCodable]:
hasher.combine(value)
default:
hasher.combine(0)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.0
// swift-tools-version:5.1

import PackageDescription

Expand All @@ -19,6 +19,7 @@ let package = Package(
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/Flight-School/AnyCodable", .exact("0.4.0")),
{{#useAlamofire}}
.package(url: "https://github.com/Alamofire/Alamofire.git", from: "4.9.1"),
{{/useAlamofire}}
Expand All @@ -34,7 +35,7 @@ let package = Package(
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "{{projectName}}",
dependencies: [{{#useAlamofire}}"Alamofire", {{/useAlamofire}}{{#usePromiseKit}}"PromiseKit", {{/usePromiseKit}}{{#useRxSwift}}"RxSwift"{{/useRxSwift}}],
dependencies: ["AnyCodable", {{#useAlamofire}}"Alamofire", {{/useAlamofire}}{{#usePromiseKit}}"PromiseKit", {{/usePromiseKit}}{{#useRxSwift}}"RxSwift"{{/useRxSwift}}],
path: "{{#swiftPackagePath}}{{swiftPackagePath}}{{/swiftPackagePath}}{{^swiftPackagePath}}{{#useSPMFileStructure}}Sources/{{projectName}}{{/useSPMFileStructure}}{{^useSPMFileStructure}}{{projectName}}/Classes{{/useSPMFileStructure}}{{/swiftPackagePath}}"
),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ Pod::Spec.new do |s|
{{#useAlamofire}}
s.dependency 'Alamofire', '~> 4.9.1'
{{/useAlamofire}}
s.dependency 'AnyCodable-FlightSchool', '~> 0.4.0'
end
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ targets:
- carthage: Alamofire{{/useAlamofire}}{{#useRxSwift}}
- carthage: RxSwift{{/useRxSwift}}{{#usePromiseKit}}
- carthage: PromiseKit{{/usePromiseKit}}
- carthage: AnyCodable
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,13 @@ extension {{projectName}}API {
let parameters: [String: Any]? = nil
{{/hasFormParams}}
{{/bodyParam}}{{#hasQueryParams}}
var url = URLComponents(string: URLString)
url?.queryItems = APIHelper.mapValuesToQueryItems([{{^queryParams}}:{{/queryParams}}
var urlComponents = URLComponents(string: URLString)
urlComponents?.queryItems = APIHelper.mapValuesToQueryItems([{{^queryParams}}:{{/queryParams}}
{{#queryParams}}
{{> _param}},
{{/queryParams}}
]){{/hasQueryParams}}{{^hasQueryParams}}
let url = URLComponents(string: URLString){{/hasQueryParams}}
let urlComponents = URLComponents(string: URLString){{/hasQueryParams}}

let nillableHeaders: [String: Any?] = [{{^headerParams}}{{^hasFormParams}}
:{{/hasFormParams}}{{/headerParams}}{{#hasFormParams}}
Expand All @@ -266,7 +266,7 @@ extension {{projectName}}API {

let requestBuilder: RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>.Type = {{projectName}}API.requestBuilderFactory.{{#returnType}}getBuilder(){{/returnType}}{{^returnType}}getNonDecodableBuilder(){{/returnType}}

return requestBuilder.init(method: "{{httpMethod}}", URLString: (url?.string ?? URLString), parameters: parameters, headers: headerParameters)
return requestBuilder.init(method: "{{httpMethod}}", URLString: (urlComponents?.string ?? URLString), parameters: parameters, headers: headerParameters)
}

{{/operation}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import Foundation
import AnyCodable
{{#description}}

/** {{description}} */{{/description}}{{#isDeprecated}}
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/swift5/alamofireLibrary/Cartfile
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
github "Alamofire/Alamofire" ~> 4.9.1
github "Flight-School/AnyCodable" ~> 0.4.0
5 changes: 3 additions & 2 deletions samples/client/petstore/swift5/alamofireLibrary/Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.0
// swift-tools-version:5.1

import PackageDescription

Expand All @@ -19,14 +19,15 @@ let package = Package(
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/Flight-School/AnyCodable", .exact("0.4.0")),
.package(url: "https://github.com/Alamofire/Alamofire.git", from: "4.9.1"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "PetstoreClient",
dependencies: ["Alamofire", ],
dependencies: ["AnyCodable", "Alamofire", ],
path: "PetstoreClient/Classes"
),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ Pod::Spec.new do |s|
s.summary = 'PetstoreClient'
s.source_files = 'PetstoreClient/Classes/**/*.swift'
s.dependency 'Alamofire', '~> 4.9.1'
s.dependency 'AnyCodable-FlightSchool', '~> 0.4.0'
end
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ open class AnotherFakeAPI {
let URLString = PetstoreClientAPI.basePath + path
let parameters = JSONEncodingHelper.encodingParameters(forEncodableObject: body)

let url = URLComponents(string: URLString)
let urlComponents = URLComponents(string: URLString)

let nillableHeaders: [String: Any?] = [
:
Expand All @@ -48,7 +48,7 @@ open class AnotherFakeAPI {

let requestBuilder: RequestBuilder<Client>.Type = PetstoreClientAPI.requestBuilderFactory.getBuilder()

return requestBuilder.init(method: "PATCH", URLString: (url?.string ?? URLString), parameters: parameters, headers: headerParameters)
return requestBuilder.init(method: "PATCH", URLString: (urlComponents?.string ?? URLString), parameters: parameters, headers: headerParameters)
}

}
Loading