public void Transform_UpdateDocument_To_Delta_TwoFields() { // Arrange var updateDocument = new UpdateDocument { Data = new Dictionary <string, object> { { "posts", JObject.FromObject(new PostUpdateTwoFields() { Title = "Food", AuthorId = 0 }) } } }; var configuration = (new ConfigurationBuilder()) .Resource <Post>() .WithSimpleProperty(x => x.AuthorId) .WithSimpleProperty(x => x.Title); var context = new Context { Configuration = configuration.ConfigurationBuilder.Build() }; var sut = new JsonApiTransformer(); // Act var resultDelta = sut.TransformBack(updateDocument, typeof(Post), context); // Assert resultDelta.ObjectPropertyValues.ContainsKey("title").ShouldBeTrue(); resultDelta.ObjectPropertyValues.ContainsKey("authorId").ShouldBeTrue(); }
public void Transform_properties_with_reserverd_keyword() { var updateDocument = new UpdateDocument() { Data = new Dictionary <string, object>() { { "posts", JObject.Parse("{ \"_id\":123, \"title\": \"someTitle\" }") } } }; var configuration = (new ConfigurationBuilder()) .Resource <Post>() .WithSimpleProperty(x => x.AuthorId) .WithSimpleProperty(x => x.Id) .WithSimpleProperty(x => x.Title); var context = new Context { Configuration = configuration.ConfigurationBuilder.Build() }; var sut = new JsonApiTransformer(); // Act var resultDelta = sut.TransformBack(updateDocument, typeof(Post), context); // Assert resultDelta.ObjectPropertyValues.ContainsKey("id").ShouldBeTrue(); }
public void Transform_UpdateDocument_To_Delta_TwoFields() { // Arrange var updateDocument = new UpdateDocument { Data = new Dictionary <string, object>() { { "data", JObject.Parse("{ \"id\":123, \"type\":\"post\", \"attributes\" : { \"title\": \"someTitle\", \"authorId\" : \"1234\"}}") } } }; var configuration = (new ConfigurationBuilder()) .Resource <Post>() .WithSimpleProperty(x => x.AuthorId) .WithSimpleProperty(x => x.Title); var context = new Context(configuration.ConfigurationBuilder.Build(), new Uri("http://fakehost:1234", UriKind.Absolute)); var sut = new JsonApiTransformer(); // Act var resultDelta = sut.TransformBack(updateDocument, typeof(Post), context); // Assert Assert.True(resultDelta.ObjectPropertyValues.ContainsKey("title")); Assert.True(resultDelta.ObjectPropertyValues.ContainsKey("authorId")); }
public override Task <InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context) { using (var reader = new StreamReader(context.HttpContext.Request.Body)) { using (var jsonReader = new JsonTextReader(reader)) { var updateDocument = jsonSerializer.Deserialize(jsonReader, typeof(UpdateDocument)) as UpdateDocument; if (updateDocument != null) { var resultType = context.ModelType.GenericTypeArguments.Single(); var jsonApiContext = new Context(configuration, new Uri(context.HttpContext.Request.Host.Value, UriKind.Absolute)); var transformed = jsonApiTransformer.TransformBack(updateDocument, resultType, jsonApiContext); return(InputFormatterResult.SuccessAsync(transformed)); } throw new NotImplementedException("Throw a better error when the update document could not be deserialised, such as a bad request"); } } }