|
| 1 | +import OpenAPIKit30 |
| 2 | +import Testing |
| 3 | +import Yams |
| 4 | + |
| 5 | +@testable import FeatherOpenAPI |
| 6 | + |
| 7 | +@Suite |
| 8 | +struct SchemaSerializationTestSuite { |
| 9 | + |
| 10 | + @Test |
| 11 | + func omittedStringDefaultAndExampleDoNotSerializeAsNull() throws { |
| 12 | + let yaml = try YAMLEncoder() |
| 13 | + .encode(OmittedStringSchema().openAPISchema()) |
| 14 | + #expect(yaml.contains("default: null") == false) |
| 15 | + #expect(yaml.contains("example: null") == false) |
| 16 | + } |
| 17 | + |
| 18 | + @Test |
| 19 | + func omittedIntDefaultAndExampleDoNotSerializeAsNull() throws { |
| 20 | + let yaml = try YAMLEncoder().encode(OmittedIntSchema().openAPISchema()) |
| 21 | + #expect(yaml.contains("default: null") == false) |
| 22 | + #expect(yaml.contains("example: null") == false) |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + func omittedInt32DefaultAndExampleDoNotSerializeAsNull() throws { |
| 27 | + let yaml = try YAMLEncoder() |
| 28 | + .encode(OmittedInt32Schema().openAPISchema()) |
| 29 | + #expect(yaml.contains("default: null") == false) |
| 30 | + #expect(yaml.contains("example: null") == false) |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + func omittedInt64DefaultAndExampleDoNotSerializeAsNull() throws { |
| 35 | + let yaml = try YAMLEncoder() |
| 36 | + .encode(OmittedInt64Schema().openAPISchema()) |
| 37 | + #expect(yaml.contains("default: null") == false) |
| 38 | + #expect(yaml.contains("example: null") == false) |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + func omittedFloatDefaultAndExampleDoNotSerializeAsNull() throws { |
| 43 | + let yaml = try YAMLEncoder() |
| 44 | + .encode(OmittedFloatSchema().openAPISchema()) |
| 45 | + #expect(yaml.contains("default: null") == false) |
| 46 | + #expect(yaml.contains("example: null") == false) |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + func omittedDoubleDefaultAndExampleDoNotSerializeAsNull() throws { |
| 51 | + let yaml = try YAMLEncoder() |
| 52 | + .encode(OmittedDoubleSchema().openAPISchema()) |
| 53 | + #expect(yaml.contains("default: null") == false) |
| 54 | + #expect(yaml.contains("example: null") == false) |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + func omittedBoolDefaultAndExampleDoNotSerializeAsNull() throws { |
| 59 | + let yaml = try YAMLEncoder().encode(OmittedBoolSchema().openAPISchema()) |
| 60 | + #expect(yaml.contains("default: null") == false) |
| 61 | + #expect(yaml.contains("example: null") == false) |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + func explicitDefaultAndExampleStillSerialize() throws { |
| 66 | + let yaml = try YAMLEncoder() |
| 67 | + .encode(ExplicitStringSchema().openAPISchema()) |
| 68 | + #expect(yaml.contains("default: abc")) |
| 69 | + #expect(yaml.contains("example: xyz")) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +private struct OmittedStringSchema: StringSchemaRepresentable {} |
| 74 | +private struct OmittedIntSchema: IntSchemaRepresentable {} |
| 75 | +private struct OmittedInt32Schema: Int32SchemaRepresentable {} |
| 76 | +private struct OmittedInt64Schema: Int64SchemaRepresentable {} |
| 77 | +private struct OmittedFloatSchema: FloatSchemaRepresentable {} |
| 78 | +private struct OmittedDoubleSchema: DoubleSchemaRepresentable {} |
| 79 | +private struct OmittedBoolSchema: BoolSchemaRepresentable {} |
| 80 | + |
| 81 | +private struct ExplicitStringSchema: StringSchemaRepresentable { |
| 82 | + var defaultValue: String? { "abc" } |
| 83 | + var example: String? { "xyz" } |
| 84 | +} |
0 commit comments