public CompoundDocument Transform(object objectGraph, Context context) { CompoundDocument result = objectGraph as CompoundDocument; if (result != null) { return(result); } Type innerObjectType = TransformationHelper.GetObjectType(objectGraph); TransformationHelper.VerifyTypeSupport(innerObjectType); TransformationHelper.AssureAllMappingsRegistered(innerObjectType, context.Configuration); result = new CompoundDocument { Meta = TransformationHelper.GetMetadata(objectGraph) }; var resource = TransformationHelper.UnwrapResourceObject(objectGraph); var resourceMapping = context.Configuration.GetMapping(innerObjectType); var resourceList = TransformationHelper.UnifyObjectsToList(resource); var representationList = resourceList.Select(o => TransformationHelper.CreateResourceRepresentation(o, resourceMapping, context)); var primaryResource = TransformationHelper.ChooseProperResourceRepresentation(resource, representationList); result.Data = primaryResource; if (resourceMapping.Relationships.Any()) { result.Included = TransformationHelper.CreateIncludedRepresentations(resourceList, resourceMapping, context); } return(result); }
public void AppendIncludedRepresentationRecursive_RecursesWholeTree() { // Arrange var source = new PostBuilder() .WithAuthor(PostBuilder.Asimov) .WithComment(1, "Comment One") .WithComment(2, "Comment Two") .Build(); var sourceList = new List<object>() { source }; var config = TestModelConfigurationBuilder.BuilderForEverything.Build(); var mapping = config.GetMapping(typeof(Post)); var context = new Context( new Uri("http://dummy:4242/posts"), new string[] { "authors.comments" }); var transformationHelper = new TransformationHelper(config, new FakeLinkBuilder()); // Act var result = transformationHelper.CreateIncludedRepresentations(sourceList, mapping, context); // Assert Assert.NotNull(result.Single(x => x.Id == "1" && x.Type == "comments")); Assert.NotNull(result.Single(x => x.Id == "2" && x.Type == "comments")); Assert.NotNull(result.Single(x => x.Id == "1" && x.Type == "authors")); Assert.False(result.Any(x => x.Type == "posts")); }
public JsonApiTransformer( JsonSerializer serializer, IConfiguration configuration, TransformationHelper transformationHelper) { this.serializer = serializer; this.configuration = configuration; this.transformationHelper = transformationHelper; }
public void Apply(HttpConfiguration configuration) { var serializer = GetJsonSerializer(); var helper = new TransformationHelper(); var transformer = new JsonApiTransformer { Serializer = serializer, TransformationHelper = helper }; var filter = new JsonApiActionFilter(transformer, this); configuration.Filters.Add(filter); var formatter = new JsonApiFormatter(this, serializer); configuration.Formatters.Add(formatter); }
public void AppendIncludedRepresentationRecursive_RecursesWholeTree_No_Duplicates() { // Arrange var duplicateAuthor = PostBuilder.Asimov; var firstSource = new PostBuilder() .WithAuthor(duplicateAuthor) .Build(); var secondSource = new PostBuilder() .WithAuthor(duplicateAuthor) .Build(); var sourceList = new List<object>() { firstSource, secondSource }; var config = TestModelConfigurationBuilder.BuilderForEverything.Build(); var mapping = config.GetMapping(typeof(Post)); var context = new Context( new Uri("http://dummy:4242/posts"), new string[] { "authors.comments" }); var transformationHelper = new TransformationHelper(config, new FakeLinkBuilder()); // Act var result = transformationHelper.CreateIncludedRepresentations(sourceList, mapping, context); // Assert Assert.Equal(1, result.Count(x => x.Type == "authors" && x.Id == PostBuilder.Asimov.Id.ToString())); }
public void GIVEN_NoIncludedItems_WHEN_Get_THEN_IncludedItemsAreNull() { // Arrange var source = new PostBuilder() .Build(); var sourceList = new List<object>() { source }; var config = TestModelConfigurationBuilder.BuilderForEverything.Build(); var mapping = config.GetMapping(typeof(Post)); var context = new Context(new Uri("http://dummy:4242/posts")); var transformationHelper = new TransformationHelper(config, new FakeLinkBuilder()); // Act var result = transformationHelper.CreateIncludedRepresentations(sourceList, mapping, context); // Assert Assert.Null(result); }
public JsonApiTransformer Build() { var serializer = JsonSerializerBuilder.Build(); var transformationHelper = new TransformationHelper(config, linkBuilder); return new JsonApiTransformer(serializer, config, transformationHelper); }
public IDelta TransformBack(UpdateDocument updateDocument, Type type, Context context) { var mapping = context.Configuration.GetMapping(type); var openGeneric = typeof(Delta <>); var closedGenericType = openGeneric.MakeGenericType(type); var delta = Activator.CreateInstance(closedGenericType) as IDelta; if (delta == null) { return(null); } var resourceKey = mapping.ResourceType; if (!updateDocument.Data.ContainsKey(resourceKey)) { return(delta); } var resource = updateDocument.Data[resourceKey] as JObject; if (resource == null) { return(delta); } // Scan the data for which properties are only set foreach (var propertySetter in mapping.PropertySettersExpressions) { JToken value; resource.TryGetValue(propertySetter.Key, StringComparison.CurrentCultureIgnoreCase, out value); if (value == null) { continue; } // Set only the properties that are present var methodCallExpression = propertySetter.Value.Body as MethodCallExpression; if (methodCallExpression != null) { Type returnType = methodCallExpression.Arguments[0].Type; var resultValue = TransformationHelper.GetValue(value, returnType); string key = propertySetter.Key.TrimStart('_'); delta.ObjectPropertyValues.Add(key, resultValue); } } JToken linksToken; resource.TryGetValue("links", StringComparison.CurrentCultureIgnoreCase, out linksToken); JObject links = linksToken as JObject; if (links != null) { foreach (var link in mapping.Relationships) { JToken value; links.TryGetValue(link.RelationshipName, StringComparison.CurrentCultureIgnoreCase, out value); if (value == null) { continue; } if (link.IsCollection) { var property = link.RelatedCollectionProperty; if (property != null) { var resultValue = TransformationHelper.GetCollection(value, link); string key = link.RelationshipName.TrimStart('_'); delta.ObjectPropertyValues.Add(key, resultValue); } } else { delta.ObjectPropertyValues.Add(link.ParentResourceNavigationPropertyName, TransformationHelper.GetValue(value, link.ParentResourceNavigationPropertyType)); } } } return(delta); }
public CompoundDocument Transform(Exception error) { return(TransformationHelper.HandleException(error)); }