Skip to content

Commit 6f23199

Browse files
authored
Fix schema null default example serialization (#40)
- fix schema null default example serialization
1 parent 969e223 commit 6f23199

10 files changed

Lines changed: 106 additions & 26 deletions

.github/workflows/testing.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ on:
77

88
jobs:
99

10+
api_breakage:
11+
name: Check API breakage
12+
uses: BinaryBirds/github-workflows/.github/workflows/api_breakage.yml@main
13+
1014
swiftlang_checks:
1115
name: Swiftlang Checks
12-
uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main
16+
uses: swiftlang/github-workflows/.github/workflows/soundness.yml@0.0.7
1317
with:
1418
license_header_check_project_name: "project"
1519
format_check_enabled : true

README.md

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
The FeatherOpenAPI library makes it easy to define OpenAPI specifications using Swift in a type-safe way.
44

5-
[
6-
![Release: 1.0.0-beta.5](https://img.shields.io/badge/Release-1%2E0%2E0--beta%2E5-F05138)
7-
](
8-
https://github.com/feather-framework/feather-openapi/releases/tag/1.0.0-beta.5
9-
)
5+
[![Release: 1.0.0-beta.6](https://img.shields.io/badge/Release-1%2E0%2E0--beta%2E6-F05138)](https://github.com/feather-framework/feather-openapi/releases/tag/1.0.0-beta.6)
106

117
## Features
128

@@ -34,7 +30,7 @@ The FeatherOpenAPI library makes it easy to define OpenAPI specifications using
3430
Use Swift Package Manager; add the dependency to your `Package.swift` file:
3531

3632
```swift
37-
.package(url: "https://github.com/feather-framework/feather-openapi", exact: "1.0.0-beta.5"),
33+
.package(url: "https://github.com/feather-framework/feather-openapi", exact: "1.0.0-beta.6"),
3834
```
3935

4036
Then add `FeatherOpenAPI` to your target dependencies:
@@ -45,11 +41,7 @@ Then add `FeatherOpenAPI` to your target dependencies:
4541

4642
## Usage
4743

48-
[
49-
![DocC API documentation](https://img.shields.io/badge/DocC-API_documentation-F05138)
50-
](
51-
https://feather-framework.github.io/feather-openapi/
52-
)
44+
[![DocC API documentation](https://img.shields.io/badge/DocC-API_documentation-F05138)](https://feather-framework.github.io/feather-openapi/)
5345

5446
API documentation is available at the following link.
5547

Sources/FeatherOpenAPI/Schema/BoolSchemaRepresentable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ extension BoolSchemaRepresentable {
3535
discriminator: nil,
3636
externalDocs: nil,
3737
allowedValues: nil,
38-
defaultValue: .init(defaultValue),
39-
example: .init(example)
38+
defaultValue: defaultValue.map { .init($0) },
39+
example: example.map { .init($0) }
4040
)
4141
}
4242
}

Sources/FeatherOpenAPI/Schema/DoubleSchemaRepresentable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ extension DoubleSchemaRepresentable {
4040
maximum: nil,
4141
minimum: nil,
4242
allowedValues: allowedValues?.map { .init($0) },
43-
defaultValue: .init(defaultValue),
44-
example: .init(example)
43+
defaultValue: defaultValue.map { .init($0) },
44+
example: example.map { .init($0) }
4545
)
4646
}
4747
}

Sources/FeatherOpenAPI/Schema/FloatSchemaRepresentable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ extension FloatSchemaRepresentable {
4040
maximum: nil,
4141
minimum: nil,
4242
allowedValues: allowedValues?.map { .init($0) },
43-
defaultValue: .init(defaultValue),
44-
example: .init(example)
43+
defaultValue: defaultValue.map { .init($0) },
44+
example: example.map { .init($0) }
4545
)
4646
}
4747
}

Sources/FeatherOpenAPI/Schema/Int32SchemaRepresentable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ extension Int32SchemaRepresentable {
4040
maximum: nil,
4141
minimum: nil,
4242
allowedValues: allowedValues?.map { .init($0) },
43-
defaultValue: .init(defaultValue),
44-
example: .init(example)
43+
defaultValue: defaultValue.map { .init($0) },
44+
example: example.map { .init($0) }
4545
)
4646
}
4747
}

Sources/FeatherOpenAPI/Schema/Int64SchemaRepresentable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ extension Int64SchemaRepresentable {
4040
maximum: nil,
4141
minimum: nil,
4242
allowedValues: allowedValues?.map { .init($0) },
43-
defaultValue: .init(defaultValue),
44-
example: .init(example)
43+
defaultValue: defaultValue.map { .init($0) },
44+
example: example.map { .init($0) }
4545
)
4646
}
4747
}

Sources/FeatherOpenAPI/Schema/IntSchemaRepresentable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ extension IntSchemaRepresentable {
4040
maximum: nil,
4141
minimum: nil,
4242
allowedValues: allowedValues?.map { .init($0) },
43-
defaultValue: .init(defaultValue),
44-
example: .init(example)
43+
defaultValue: defaultValue.map { .init($0) },
44+
example: example.map { .init($0) }
4545
)
4646
}
4747
}

Sources/FeatherOpenAPI/Schema/StringSchemaRepresentable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ extension StringSchemaRepresentable {
4040
maxLength: nil,
4141
pattern: nil,
4242
allowedValues: allowedValues?.map { .init($0) },
43-
defaultValue: .init(defaultValue),
44-
example: .init(example)
43+
defaultValue: defaultValue.map { .init($0) },
44+
example: example.map { .init($0) }
4545
)
4646
}
4747
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)