@@ -15,9 +15,10 @@ extension OpenAPI {
1515 public struct Example : Equatable , CodableVendorExtendable {
1616 public let summary : String ?
1717 public let description : String ?
18+
1819 /// Represents the OpenAPI `externalValue` as a URL _or_
1920 /// the OpenAPI `value` as `AnyCodable`.
20- public let value : Either < URL , AnyCodable >
21+ public let value : Either < URL , AnyCodable > ?
2122
2223 /// Dictionary of vendor extensions.
2324 ///
@@ -29,7 +30,7 @@ extension OpenAPI {
2930 public init (
3031 summary: String ? = nil ,
3132 description: String ? = nil ,
32- value: Either < URL , AnyCodable > ,
33+ value: Either < URL , AnyCodable > ? = nil ,
3334 vendorExtensions: [ String : AnyCodable ] = [ : ]
3435 ) {
3536 self . summary = summary
@@ -50,7 +51,7 @@ extension Either where A == OpenAPI.Reference<OpenAPI.Example>, B == OpenAPI.Exa
5051 public static func example(
5152 summary: String ? = nil ,
5253 description: String ? = nil ,
53- value: Either < URL , AnyCodable > ,
54+ value: Either < URL , AnyCodable > ? = nil ,
5455 vendorExtensions: [ String : AnyCodable ] = [ : ]
5556 ) -> Self {
5657 return . b(
@@ -101,6 +102,8 @@ extension OpenAPI.Example: Encodable {
101102 try container. encode ( url. absoluteURL, forKey: . externalValue)
102103 case . b( let example) :
103104 try container. encode ( example, forKey: . value)
105+ case nil :
106+ break
104107 }
105108
106109 try encodeExtensions ( to: & container)
@@ -119,14 +122,17 @@ extension OpenAPI.Example: Decodable {
119122 )
120123 }
121124
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+ }
123132
124133 summary = try container. decodeIfPresent ( String . self, forKey: . summary)
125134 description = try container. decodeIfPresent ( String . self, forKey: . description)
126135
127- value = try externalValue. map ( Either . init)
128- ?? . init( container. decode ( AnyCodable . self, forKey: . value) )
129-
130136 vendorExtensions = try Self . extensions ( from: decoder)
131137 }
132138}
0 commit comments