Skip to content

Commit

Permalink
Merge pull request #3 from eneko/master
Browse files Browse the repository at this point in the history
Add support for OpenAPI DataTypes
  • Loading branch information
mczachurski authored Feb 3, 2019
2 parents db3140b + 3f43667 commit 5363087
Show file tree
Hide file tree
Showing 9 changed files with 390 additions and 25 deletions.
5 changes: 4 additions & 1 deletion Sources/Swiftgger/APIModel/APIParameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,23 @@ public class APIParameter {
var required: Bool = false
var deprecated: Bool = false
var allowEmptyValue: Bool = false
var dataType: APIDataType = APIDataType.string

public init(
name: String,
parameterLocation: APILocation = APILocation.path,
description: String? = nil,
required: Bool = false,
deprecated: Bool = false,
allowEmptyValue: Bool = false
allowEmptyValue: Bool = false,
dataType: APIDataType = APIDataType.string
) {
self.name = name
self.parameterLocation = parameterLocation
self.description = description
self.required = required
self.deprecated = deprecated
self.allowEmptyValue = allowEmptyValue
self.dataType = dataType
}
}
3 changes: 2 additions & 1 deletion Sources/Swiftgger/Builder/OpenAPIParametersBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class OpenAPIParametersBuilder {
description: apiParameter.description,
required: apiParameter.required,
deprecated: apiParameter.deprecated,
allowEmptyValue: apiParameter.allowEmptyValue
allowEmptyValue: apiParameter.allowEmptyValue,
schema: OpenAPISchema(type: apiParameter.dataType.type, format: apiParameter.dataType.format)
)

openApiParameters.append(openAPIParameter)
Expand Down
33 changes: 29 additions & 4 deletions Sources/Swiftgger/Builder/OpenAPISchemasBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,40 @@ class OpenAPISchemasBuilder {

var array: [(name: String, type: OpenAPIObjectProperty)] = []
for property in properties {
let someType = type(of: unwrap(property.value))
let typeName = String(describing: someType)
let example = String(describing: unwrap(property.value))
array.append((name: property.label!, type: OpenAPIObjectProperty(type: typeName.lowercased(), example: example)))
let unwrapped = unwrap(property.value)
let dataType = makeAPIDataType(fromSwiftValue: unwrapped)
let example = String(describing: unwrapped)
let objectProperty = OpenAPIObjectProperty(type: dataType.type, format: dataType.format, example: example)
array.append((name: property.label ?? "", type: objectProperty))
}

return array
}

/// Infer OpenAPI Data Type from Swift value type
/// (nested types or collections not supported at the moment)
///
/// - Parameter value: Swift property value to analyze
/// - Returns: Most appropriate OpenAPI Data Type
private func makeAPIDataType(fromSwiftValue value: Any) -> APIDataType {
switch value {
case is Int32:
return .int32
case is Int:
return .int64
case is Float:
return .float
case is Double:
return .double
case is Bool:
return .boolean
case is Date:
return .dateTime
default:
return .string
}
}

private func getRequiredProperties(properties: Mirror.Children) -> [String] {
var array: [String] = []

Expand Down
26 changes: 26 additions & 0 deletions Sources/Swiftgger/Common/APIDataType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// APIDataType.swift
// Swiftgger
//
// Created by Eneko Alonso on 2/2/19.
//

import Foundation

/// OpenAPI Data Types as specified in https://swagger.io/specification/#dataTypes
public struct APIDataType {
let type: String
let format: String?

public static let int32 = APIDataType(type: "integer", format: "int32")
public static let int64 = APIDataType(type: "integer", format: "int64")
public static let float = APIDataType(type: "number", format: "float")
public static let double = APIDataType(type: "number", format: "double")
public static let string = APIDataType(type: "string", format: nil)
public static let byte = APIDataType(type: "string", format: "byte")
public static let binary = APIDataType(type: "string", format: "binary")
public static let boolean = APIDataType(type: "boolean", format: nil)
public static let date = APIDataType(type: "string", format: "date")
public static let dateTime = APIDataType(type: "string", format: "date-time")
public static let password = APIDataType(type: "string", format: "password")
}
4 changes: 3 additions & 1 deletion Sources/Swiftgger/OpenAPIModel/OpenAPIObjectProperty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import Foundation
public class OpenAPIObjectProperty: Encodable {

public private(set) var type: String
public private(set) var format: String?
public private(set) var example: String?

init(type: String, example: String?) {
init(type: String, format: String?, example: String?) {
self.type = type
self.format = format
self.example = example
}
}
6 changes: 5 additions & 1 deletion Sources/Swiftgger/OpenAPIModel/OpenAPIParameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class OpenAPIParameter: Encodable {
public private(set) var required: Bool = false
public private(set) var deprecated: Bool = false
public private(set) var allowEmptyValue: Bool = false
public private(set) var schema: OpenAPISchema?

init(ref: String) {
self.ref = ref
Expand All @@ -31,14 +32,16 @@ public class OpenAPIParameter: Encodable {
description: String? = nil,
required: Bool = false,
deprecated: Bool = false,
allowEmptyValue: Bool = false
allowEmptyValue: Bool = false,
schema: OpenAPISchema
) {
self.name = name
self.parameterLocation = parameterLocation
self.description = description
self.required = required
self.deprecated = deprecated
self.allowEmptyValue = allowEmptyValue
self.schema = schema
}

private enum CodingKeys: String, CodingKey {
Expand All @@ -49,5 +52,6 @@ public class OpenAPIParameter: Encodable {
case required
case deprecated
case allowEmptyValue
case schema
}
}
7 changes: 6 additions & 1 deletion Sources/Swiftgger/OpenAPIModel/OpenAPISchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class OpenAPISchema: Encodable {

public private(set) var ref: String?
public private(set) var type: String?
public private(set) var format: String?
public private(set) var items: OpenAPISchema?
public private(set) var required: [String]?
public private(set) var properties: [String: OpenAPIObjectProperty]?
Expand All @@ -20,8 +21,11 @@ public class OpenAPISchema: Encodable {
self.ref = ref
}

init(type: String? = nil, items: OpenAPISchema? = nil, required: [String]? = nil, properties: [(name: String, type: OpenAPIObjectProperty)]? = nil) {
init(type: String? = nil, format: String? = nil,
items: OpenAPISchema? = nil, required: [String]? = nil,
properties: [(name: String, type: OpenAPIObjectProperty)]? = nil) {
self.type = type
self.format = format
self.items = items
self.required = required

Expand All @@ -36,6 +40,7 @@ public class OpenAPISchema: Encodable {
private enum CodingKeys: String, CodingKey {
case ref = "$ref"
case type
case format
case items
case required
case properties
Expand Down
Loading

0 comments on commit 5363087

Please sign in to comment.