|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftOpenAPIGenerator open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | +import XCTest |
| 15 | +import OpenAPIKit |
| 16 | +import Yams |
| 17 | +@testable import _OpenAPIGeneratorCore |
| 18 | + |
| 19 | +class Test_typeSubstitutions: Test_Core { |
| 20 | + |
| 21 | + func testSchemaString() throws { |
| 22 | + func _test( |
| 23 | + schema schemaString: String, |
| 24 | + expectedType: ExistingTypeDescription, |
| 25 | + file: StaticString = #file, |
| 26 | + line: UInt = #line |
| 27 | + ) throws { |
| 28 | + let typeName = TypeName(swiftKeyPath: ["Foo"]) |
| 29 | + |
| 30 | + let schema = try loadSchemaFromYAML(schemaString) |
| 31 | + let collector = AccumulatingDiagnosticCollector() |
| 32 | + let translator = makeTranslator(diagnostics: collector) |
| 33 | + let translated = try translator.translateSchema(typeName: typeName, schema: schema, overrides: .none) |
| 34 | + if translated.count != 1 { |
| 35 | + XCTFail("Expected only a single translated schema, got: \(translated.count)", file: file, line: line) |
| 36 | + return |
| 37 | + } |
| 38 | + XCTAssertTrue(translated.count == 1, "Should have one translated schema") |
| 39 | + guard case let .typealias(typeAliasDescription) = translated.first?.strippingTopComment else { |
| 40 | + XCTFail("Expected typealias description got", file: file, line: line) |
| 41 | + return |
| 42 | + } |
| 43 | + XCTAssertEqual(typeAliasDescription.name, "Foo", file: file, line: line) |
| 44 | + XCTAssertEqual(typeAliasDescription.existingType, expectedType, file: file, line: line) |
| 45 | + } |
| 46 | + try _test( |
| 47 | + schema: #""" |
| 48 | + type: string |
| 49 | + x-swift-open-api-replace-type: MyLibrary.MyCustomType |
| 50 | + """#, |
| 51 | + expectedType: .member(["MyLibrary", "MyCustomType"]) |
| 52 | + ) |
| 53 | + try _test( |
| 54 | + schema: """ |
| 55 | + type: array |
| 56 | + items: |
| 57 | + type: integer |
| 58 | + x-swift-open-api-replace-type: MyLibrary.MyCustomType |
| 59 | + """, |
| 60 | + expectedType: .member(["MyLibrary", "MyCustomType"]) |
| 61 | + ) |
| 62 | + try _test( |
| 63 | + schema: """ |
| 64 | + type: string |
| 65 | + x-swift-open-api-replace-type: MyLibrary.MyCustomType |
| 66 | + """, |
| 67 | + expectedType: .member(["MyLibrary", "MyCustomType"]) |
| 68 | + ) |
| 69 | + try _test( |
| 70 | + schema: """ |
| 71 | + type: array |
| 72 | + items: |
| 73 | + type: integer |
| 74 | + x-swift-open-api-replace-type: MyLibrary.MyCustomType |
| 75 | + """, |
| 76 | + expectedType: .array(.member(["MyLibrary", "MyCustomType"])) |
| 77 | + ) |
| 78 | + // TODO: Investigate if vendor-extensions are allowed in anyOf, allOf, oneOf |
| 79 | + try _test( |
| 80 | + schema: """ |
| 81 | + anyOf: |
| 82 | + - type: string |
| 83 | + - type: integer |
| 84 | + x-swift-open-api-replace-type: MyLibrary.MyCustomType |
| 85 | + """, |
| 86 | + expectedType: .member(["MyLibrary", "MyCustomType"]) |
| 87 | + ) |
| 88 | + try _test( |
| 89 | + schema: """ |
| 90 | + allOf: |
| 91 | + - type: object |
| 92 | + properties: |
| 93 | + foo: |
| 94 | + type: string |
| 95 | + - type: object |
| 96 | + properties: |
| 97 | + bar: |
| 98 | + type: string |
| 99 | + x-swift-open-api-replace-type: MyLibrary.MyCustomType |
| 100 | + """, |
| 101 | + expectedType: .member(["MyLibrary", "MyCustomType"]) |
| 102 | + ) |
| 103 | + try _test( |
| 104 | + schema: """ |
| 105 | + oneOf: |
| 106 | + - type: object |
| 107 | + properties: |
| 108 | + foo: |
| 109 | + type: string |
| 110 | + - type: object |
| 111 | + properties: |
| 112 | + bar: |
| 113 | + type: string |
| 114 | + x-swift-open-api-replace-type: MyLibrary.MyCustomType |
| 115 | + """, |
| 116 | + expectedType: .member(["MyLibrary", "MyCustomType"]) |
| 117 | + ) |
| 118 | + } |
| 119 | + func testSimpleInlinePropertiesReplacements() throws { |
| 120 | + func _testInlineProperty( |
| 121 | + schema schemaString: String, |
| 122 | + expectedType: ExistingTypeDescription, |
| 123 | + file: StaticString = #file, |
| 124 | + line: UInt = #line |
| 125 | + ) throws { |
| 126 | + let typeName = TypeName(swiftKeyPath: ["Foo"]) |
| 127 | + |
| 128 | + let propertySchema = try YAMLDecoder().decode(JSONSchema.self, from: schemaString).requiredSchemaObject() |
| 129 | + let schema = JSONSchema.object(properties: ["property": propertySchema]) |
| 130 | + let collector = AccumulatingDiagnosticCollector() |
| 131 | + let translator = makeTranslator(diagnostics: collector) |
| 132 | + let translated = try translator.translateSchema(typeName: typeName, schema: schema, overrides: .none) |
| 133 | + if translated.count != 1 { |
| 134 | + XCTFail("Expected only a single translated schema, got: \(translated.count)", file: file, line: line) |
| 135 | + return |
| 136 | + } |
| 137 | + guard case let .struct(structDescription) = translated.first?.strippingTopComment else { |
| 138 | + throw GenericError(message: "Expected struct") |
| 139 | + } |
| 140 | + let variables: [VariableDescription] = structDescription.members.compactMap { member in |
| 141 | + guard case let .variable(variableDescription) = member.strippingTopComment else { return nil } |
| 142 | + return variableDescription |
| 143 | + } |
| 144 | + if variables.count != 1 { |
| 145 | + XCTFail("Expected only a single variable, got: \(variables.count)", file: file, line: line) |
| 146 | + return |
| 147 | + } |
| 148 | + XCTAssertEqual(variables[0].type, expectedType, file: file, line: line) |
| 149 | + } |
| 150 | + try _testInlineProperty( |
| 151 | + schema: """ |
| 152 | + type: array |
| 153 | + items: |
| 154 | + type: integer |
| 155 | + x-swift-open-api-replace-type: MyLibrary.MyCustomType |
| 156 | + """, |
| 157 | + expectedType: .member(["MyLibrary", "MyCustomType"]) |
| 158 | + ) |
| 159 | + try _testInlineProperty( |
| 160 | + schema: """ |
| 161 | + type: string |
| 162 | + x-swift-open-api-replace-type: MyLibrary.MyCustomType |
| 163 | + """, |
| 164 | + expectedType: .member(["MyLibrary", "MyCustomType"]) |
| 165 | + ) |
| 166 | + try _testInlineProperty( |
| 167 | + schema: """ |
| 168 | + type: array |
| 169 | + items: |
| 170 | + type: integer |
| 171 | + x-swift-open-api-replace-type: MyLibrary.MyCustomType |
| 172 | + """, |
| 173 | + expectedType: .array(.member(["MyLibrary", "MyCustomType"])) |
| 174 | + ) |
| 175 | + // TODO: Investigate if vendor-extensions are allowed in anyOf, allOf, oneOf |
| 176 | + try _testInlineProperty( |
| 177 | + schema: """ |
| 178 | + anyOf: |
| 179 | + - type: string |
| 180 | + - type: integer |
| 181 | + x-swift-open-api-replace-type: MyLibrary.MyCustomType |
| 182 | + """, |
| 183 | + expectedType: .member(["MyLibrary", "MyCustomType"]) |
| 184 | + ) |
| 185 | + try _testInlineProperty( |
| 186 | + schema: """ |
| 187 | + allOf: |
| 188 | + - type: object |
| 189 | + properties: |
| 190 | + foo: |
| 191 | + type: string |
| 192 | + - type: object |
| 193 | + properties: |
| 194 | + bar: |
| 195 | + type: string |
| 196 | + x-swift-open-api-replace-type: MyLibrary.MyCustomType |
| 197 | + """, |
| 198 | + expectedType: .member(["MyLibrary", "MyCustomType"]) |
| 199 | + ) |
| 200 | + try _testInlineProperty( |
| 201 | + schema: """ |
| 202 | + oneOf: |
| 203 | + - type: object |
| 204 | + properties: |
| 205 | + foo: |
| 206 | + type: string |
| 207 | + - type: object |
| 208 | + properties: |
| 209 | + bar: |
| 210 | + type: string |
| 211 | + x-swift-open-api-replace-type: MyLibrary.MyCustomType |
| 212 | + """, |
| 213 | + expectedType: .member(["MyLibrary", "MyCustomType"]) |
| 214 | + ) |
| 215 | + } |
| 216 | +} |
0 commit comments