@@ -133,23 +133,23 @@ public struct ToManyRelationship<Relatable: JSONAPI.Relatable, IdMetaType: JSONA
133133 }
134134 }
135135
136- public let metaIds : [ ID ]
136+ public let idsWithMeta : [ ID ]
137137
138138 public var ids : [ Relatable . ID ] {
139- metaIds . map ( \. id)
139+ idsWithMeta . map ( \. id)
140140 }
141141
142142 public let meta : MetaType
143143 public let links : LinksType
144144
145145 public init ( idsWithMetadata ids: [ ( Relatable . ID , IdMetaType ) ] , meta: MetaType , links: LinksType ) {
146- self . metaIds = ids. map { ID . init ( $0) }
146+ self . idsWithMeta = ids. map { ID . init ( $0) }
147147 self . meta = meta
148148 self . links = links
149149 }
150150
151151 public init < T: JSONAPIIdentifiable > ( pointers: [ ToOneRelationship < T , NoIdMetadata , NoMetadata , NoLinks > ] , meta: MetaType , links: LinksType ) where T. ID == Relatable . ID , IdMetaType == NoIdMetadata {
152- metaIds = pointers. map { . init( id: $0. id, meta: . none) }
152+ idsWithMeta = pointers. map { . init( id: $0. id, meta: . none) }
153153 self . meta = meta
154154 self . links = links
155155 }
@@ -169,7 +169,7 @@ public struct ToManyRelationship<Relatable: JSONAPI.Relatable, IdMetaType: JSONA
169169
170170extension ToManyRelationship where IdMetaType == NoIdMetadata {
171171 public init ( ids: [ Relatable . ID ] , meta: MetaType , links: LinksType ) {
172- self . metaIds = ids. map { . init( id: $0, meta: . none) }
172+ self . idsWithMeta = ids. map { . init( id: $0, meta: . none) }
173173 self . meta = meta
174174 self . links = links
175175 }
@@ -263,6 +263,9 @@ extension MetaRelationship: Codable {
263263 }
264264}
265265
266+ fileprivate protocol _Optional { }
267+ extension Optional : _Optional { }
268+
266269extension ToOneRelationship : Codable where Identifiable. ID: OptionalId {
267270 public init ( from decoder: Decoder ) throws {
268271 let container = try decoder. container ( keyedBy: ResourceLinkageCodingKeys . self)
@@ -295,7 +298,15 @@ extension ToOneRelationship: Codable where Identifiable.ID: OptionalId {
295298 )
296299 )
297300 }
298- guard let noIdMeta = NoIdMetadata ( ) as? IdMetaType else {
301+ // if we know we aren't getting any Resource Identifer Object at all
302+ // (which we do inside this block) then we better either be expecting
303+ // no Id Metadata or optional Id Metadata or else we will report an
304+ // error.
305+ if let noIdMeta = NoIdMetadata ( ) as? IdMetaType {
306+ idMeta = noIdMeta
307+ } else if let nilMeta = anyNil as? IdMetaType {
308+ idMeta = nilMeta
309+ } else {
299310 throw DecodingError . valueNotFound (
300311 Self . self,
301312 DecodingError . Context (
@@ -304,7 +315,6 @@ extension ToOneRelationship: Codable where Identifiable.ID: OptionalId {
304315 )
305316 )
306317 }
307- idMeta = noIdMeta
308318 id = val
309319 return
310320 }
@@ -425,7 +435,7 @@ extension ToManyRelationship: Codable {
425435
426436 newIds. append ( . init( id: Relatable . ID ( rawValue: id) , meta: idMeta) )
427437 }
428- metaIds = newIds
438+ idsWithMeta = newIds
429439 }
430440
431441 public func encode( to encoder: Encoder ) throws {
@@ -441,7 +451,7 @@ extension ToManyRelationship: Codable {
441451
442452 var identifiers = container. nestedUnkeyedContainer ( forKey: . data)
443453
444- for id in metaIds {
454+ for id in idsWithMeta {
445455 var identifier = identifiers. nestedContainer ( keyedBy: ResourceIdentifierCodingKeys . self)
446456
447457 try identifier. encode ( id. id. rawValue, forKey: . id)
0 commit comments