Skip to content

Commit af9f7d4

Browse files
committed
Added 'DocumentMeta' to IJsonApiContext
1 parent 09a1323 commit af9f7d4

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public object Deserialize(string requestBody)
2929
try
3030
{
3131
var document = JsonConvert.DeserializeObject<Document>(requestBody);
32+
_jsonApiContext.DocumentMeta = document.Meta;
3233
var entity = DocumentToObject(document.Data);
3334
return entity;
3435
}
@@ -222,4 +223,4 @@ private object SetHasManyRelationship(object entity,
222223
return entity;
223224
}
224225
}
225-
}
226+
}

test/UnitTests/Serialization/JsonApiDeSerializerTests.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using JsonApiDotNetCore.Builders;
44
using JsonApiDotNetCore.Configuration;
@@ -271,6 +271,54 @@ public void Can_Deserialize_Independent_Side_Of_One_To_One_Relationship_With_Rel
271271
Assert.Equal(property, result.Property);
272272
}
273273

274+
[Fact]
275+
public void Sets_The_DocumentMeta_Property_In_JsonApiContext()
276+
{
277+
// arrange
278+
var contextGraphBuilder = new ContextGraphBuilder();
279+
contextGraphBuilder.AddResource<Independent>("independents");
280+
contextGraphBuilder.AddResource<Dependent>("dependents");
281+
var contextGraph = contextGraphBuilder.Build();
282+
283+
var jsonApiContextMock = new Mock<IJsonApiContext>();
284+
jsonApiContextMock.SetupAllProperties();
285+
jsonApiContextMock.Setup(m => m.ContextGraph).Returns(contextGraph);
286+
jsonApiContextMock.Setup(m => m.AttributesToUpdate).Returns(new Dictionary<AttrAttribute, object>());
287+
288+
var jsonApiOptions = new JsonApiOptions();
289+
jsonApiContextMock.Setup(m => m.Options).Returns(jsonApiOptions);
290+
291+
var genericProcessorFactoryMock = new Mock<IGenericProcessorFactory>();
292+
293+
var deserializer = new JsonApiDeSerializer(jsonApiContextMock.Object, genericProcessorFactoryMock.Object);
294+
295+
var property = Guid.NewGuid().ToString();
296+
297+
var content = new Document
298+
{
299+
Meta = new Dictionary<string, object>() { {"foo", "bar"}},
300+
Data = new DocumentData
301+
{
302+
Type = "independents",
303+
Id = "1",
304+
Attributes = new Dictionary<string, object> { { "property", property }
305+
},
306+
// a common case for this is deserialization in unit tests
307+
Relationships = new Dictionary<string, RelationshipData> { { "dependent", new RelationshipData { } }
308+
}
309+
}
310+
};
311+
312+
var contentString = JsonConvert.SerializeObject(content);
313+
314+
// act
315+
var result = deserializer.Deserialize<Independent>(contentString);
316+
317+
// assert
318+
jsonApiContextMock.VerifySet(mock => mock.DocumentMeta = content.Meta);
319+
}
320+
321+
274322
private class TestResource : Identifiable {
275323
[Attr("complex-member")]
276324
public ComplexType ComplexMember { get; set; }

0 commit comments

Comments
 (0)