11using System ;
22using System . Collections . Generic ;
3+ using System . Linq ;
34using System . Threading . Tasks ;
45using JsonApiDotNetCore . Data ;
56using JsonApiDotNetCore . Internal ;
7+ using JsonApiDotNetCore . Models ;
68using JsonApiDotNetCore . Models . Operations ;
79using JsonApiDotNetCore . Models . Pointers ;
810using Microsoft . EntityFrameworkCore ;
@@ -61,7 +63,7 @@ private async Task ProcessOperation(Operation op, List<Operation> outputOps)
6163 {
6264 var operationsPointer = new OperationsPointer ( ) ;
6365
64- // ReplaceDataPointers(op.DataObject, outputOps);
66+ ReplaceDataPointers ( op . DataObject , outputOps ) ;
6567 // ReplaceRefPointers(op.Ref, outputOps);
6668
6769 var processor = GetOperationsProcessor ( op ) ;
@@ -71,6 +73,43 @@ private async Task ProcessOperation(Operation op, List<Operation> outputOps)
7173 outputOps . Add ( resultOp ) ;
7274 }
7375
76+ private void ReplaceDataPointers ( DocumentData data , List < Operation > outputOps )
77+ {
78+ if ( data == null ) return ;
79+
80+ bool HasLocalId ( ResourceIdentifierObject rio ) => string . IsNullOrEmpty ( rio . LocalId ) == false ;
81+ string GetIdFromLocalId ( string localId ) {
82+ var referencedOp = outputOps . FirstOrDefault ( o => o . DataObject . LocalId == localId ) ;
83+ if ( referencedOp == null ) throw new JsonApiException ( 400 , $ "Could not locate lid '{ localId } ' in document.") ;
84+ return referencedOp . DataObject . Id ;
85+ } ;
86+
87+ // are there any circumstances where the primary data would contain an lid?
88+ // if(HasLocalId(data))
89+ // {
90+ // data.Id = GetIdFromLocalId(data.LocalId);
91+ // }
92+
93+ if ( data . Relationships != null )
94+ {
95+ foreach ( var relationshipDictionary in data . Relationships )
96+ {
97+ if ( relationshipDictionary . Value . IsHasMany )
98+ {
99+ foreach ( var relationship in relationshipDictionary . Value . ManyData )
100+ if ( HasLocalId ( relationship ) )
101+ relationship . Id = GetIdFromLocalId ( relationship . LocalId ) ;
102+ }
103+ else
104+ {
105+ var relationship = relationshipDictionary . Value . SingleData ;
106+ if ( HasLocalId ( relationship ) )
107+ relationship . Id = GetIdFromLocalId ( relationship . LocalId ) ;
108+ }
109+ }
110+ }
111+ }
112+
74113 private IOpProcessor GetOperationsProcessor ( Operation op )
75114 {
76115 switch ( op . Op )
0 commit comments