@@ -15,9 +15,10 @@ extension OpenAPI {
15
15
public struct Example : Equatable , CodableVendorExtendable {
16
16
public let summary : String ?
17
17
public let description : String ?
18
+
18
19
/// Represents the OpenAPI `externalValue` as a URL _or_
19
20
/// the OpenAPI `value` as `AnyCodable`.
20
- public let value : Either < URL , AnyCodable >
21
+ public let value : Either < URL , AnyCodable > ?
21
22
22
23
/// Dictionary of vendor extensions.
23
24
///
@@ -29,7 +30,7 @@ extension OpenAPI {
29
30
public init (
30
31
summary: String ? = nil ,
31
32
description: String ? = nil ,
32
- value: Either < URL , AnyCodable > ,
33
+ value: Either < URL , AnyCodable > ? = nil ,
33
34
vendorExtensions: [ String : AnyCodable ] = [ : ]
34
35
) {
35
36
self . summary = summary
@@ -50,7 +51,7 @@ extension Either where A == OpenAPI.Reference<OpenAPI.Example>, B == OpenAPI.Exa
50
51
public static func example(
51
52
summary: String ? = nil ,
52
53
description: String ? = nil ,
53
- value: Either < URL , AnyCodable > ,
54
+ value: Either < URL , AnyCodable > ? = nil ,
54
55
vendorExtensions: [ String : AnyCodable ] = [ : ]
55
56
) -> Self {
56
57
return . b(
@@ -101,6 +102,8 @@ extension OpenAPI.Example: Encodable {
101
102
try container. encode ( url. absoluteURL, forKey: . externalValue)
102
103
case . b( let example) :
103
104
try container. encode ( example, forKey: . value)
105
+ case nil :
106
+ break
104
107
}
105
108
106
109
try encodeExtensions ( to: & container)
@@ -119,14 +122,17 @@ extension OpenAPI.Example: Decodable {
119
122
)
120
123
}
121
124
122
- let externalValue = try container. decodeURLAsStringIfPresent ( forKey: . externalValue)
125
+ if let externalValue = try container. decodeURLAsStringIfPresent ( forKey: . externalValue) {
126
+ value = . a( externalValue)
127
+ } else if let internalValue = try container. decodeIfPresent ( AnyCodable . self, forKey: . value) {
128
+ value = . b( internalValue)
129
+ } else {
130
+ value = nil
131
+ }
123
132
124
133
summary = try container. decodeIfPresent ( String . self, forKey: . summary)
125
134
description = try container. decodeIfPresent ( String . self, forKey: . description)
126
135
127
- value = try externalValue. map ( Either . init)
128
- ?? . init( container. decode ( AnyCodable . self, forKey: . value) )
129
-
130
136
vendorExtensions = try Self . extensions ( from: decoder)
131
137
}
132
138
}
0 commit comments