|
| 1 | +// Copyright 2024 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import Foundation |
| 16 | + |
| 17 | +/// A `Schema` object allows the definition of input and output data types. |
| 18 | +/// |
| 19 | +/// These types can be objects, but also primitives and arrays. Represents a select subset of an |
| 20 | +/// [OpenAPI 3.0 schema object](https://spec.openapis.org/oas/v3.0.3#schema). |
| 21 | +public class Schema { |
| 22 | + /// The data type. |
| 23 | + let type: DataType |
| 24 | + |
| 25 | + /// The format of the data. |
| 26 | + let format: String? |
| 27 | + |
| 28 | + /// A brief description of the parameter. |
| 29 | + let description: String? |
| 30 | + |
| 31 | + /// Indicates if the value may be null. |
| 32 | + let nullable: Bool? |
| 33 | + |
| 34 | + /// Possible values of the element of type ``DataType/string`` with "enum" format. |
| 35 | + let enumValues: [String]? |
| 36 | + |
| 37 | + /// Schema of the elements of type ``DataType/array``. |
| 38 | + let items: Schema? |
| 39 | + |
| 40 | + /// Properties of type ``DataType/object``. |
| 41 | + let properties: [String: Schema]? |
| 42 | + |
| 43 | + /// Required properties of type ``DataType/object``. |
| 44 | + let requiredProperties: [String]? |
| 45 | + |
| 46 | + /// Constructs a new `Schema`. |
| 47 | + /// |
| 48 | + /// - Parameters: |
| 49 | + /// - type: The data type. |
| 50 | + /// - format: The format of the data; used only for primitive datatypes. |
| 51 | + /// Supported formats: |
| 52 | + /// - ``DataType/integer``: int32, int64 |
| 53 | + /// - ``DataType/number``: float, double |
| 54 | + /// - ``DataType/string``: enum |
| 55 | + /// - description: A brief description of the parameter; may be formatted as Markdown. |
| 56 | + /// - nullable: Indicates if the value may be null. |
| 57 | + /// - enumValues: Possible values of the element of type ``DataType/string`` with "enum" format. |
| 58 | + /// For example, an enum `Direction` may be defined as `["EAST", NORTH", "SOUTH", "WEST"]`. |
| 59 | + /// - items: Schema of the elements of type ``DataType/array``. |
| 60 | + /// - properties: Properties of type ``DataType/object``. |
| 61 | + /// - requiredProperties: Required properties of type ``DataType/object``. |
| 62 | + public init(type: DataType, format: String? = nil, description: String? = nil, |
| 63 | + nullable: Bool? = nil, |
| 64 | + enumValues: [String]? = nil, items: Schema? = nil, |
| 65 | + properties: [String: Schema]? = nil, |
| 66 | + requiredProperties: [String]? = nil) { |
| 67 | + self.type = type |
| 68 | + self.format = format |
| 69 | + self.description = description |
| 70 | + self.nullable = nullable |
| 71 | + self.enumValues = enumValues |
| 72 | + self.items = items |
| 73 | + self.properties = properties |
| 74 | + self.requiredProperties = requiredProperties |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/// A data type. |
| 79 | +/// |
| 80 | +/// Contains the set of OpenAPI [data types](https://spec.openapis.org/oas/v3.0.3#data-types). |
| 81 | +public enum DataType: String { |
| 82 | + /// A `String` type. |
| 83 | + case string = "STRING" |
| 84 | + |
| 85 | + /// A floating-point number type. |
| 86 | + case number = "NUMBER" |
| 87 | + |
| 88 | + /// An integer type. |
| 89 | + case integer = "INTEGER" |
| 90 | + |
| 91 | + /// A boolean type. |
| 92 | + case boolean = "BOOLEAN" |
| 93 | + |
| 94 | + /// An array type. |
| 95 | + case array = "ARRAY" |
| 96 | + |
| 97 | + /// An object type. |
| 98 | + case object = "OBJECT" |
| 99 | +} |
| 100 | + |
| 101 | +// MARK: - Codable Conformance |
| 102 | + |
| 103 | +extension Schema: Encodable { |
| 104 | + enum CodingKeys: String, CodingKey { |
| 105 | + case type |
| 106 | + case format |
| 107 | + case description |
| 108 | + case nullable |
| 109 | + case enumValues = "enum" |
| 110 | + case items |
| 111 | + case properties |
| 112 | + case requiredProperties = "required" |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +extension DataType: Encodable {} |
0 commit comments