Example #1
0
        public async Task Can_Select_Sparse_Fieldsets()
        {
            // Arrange
            var fields = new List <string> {
                "Id", "Description", "CreatedDate", "AchievedDate"
            };
            var todoItem = new TodoItem
            {
                Description  = "description",
                Ordinal      = 1,
                CreatedDate  = DateTime.Now,
                AchievedDate = DateTime.Now.AddDays(2)
            };

            _dbContext.TodoItems.Add(todoItem);
            await _dbContext.SaveChangesAsync();

            var expectedSql = StringExtensions.Normalize($@"SELECT 't'.'Id', 't'.'Description', 't'.'CreatedDate', 't'.'AchievedDate'
                                FROM 'TodoItems' AS 't'
                                WHERE 't'.'Id' = {todoItem.Id}");

            // Act
            var query = _dbContext
                        .TodoItems
                        .Where(t => t.Id == todoItem.Id)
                        .Select(_resourceGraph.GetAttributes <TodoItem>(e => new { e.Id, e.Description, e.CreatedDate, e.AchievedDate }));

            var result = await query.FirstAsync();

            // Assert
            Assert.Equal(0, result.Ordinal);
            Assert.Equal(todoItem.Description, result.Description);
            Assert.Equal(todoItem.CreatedDate.ToString("G"), result.CreatedDate.ToString("G"));
            Assert.Equal(todoItem.AchievedDate.GetValueOrDefault().ToString("G"), result.AchievedDate.GetValueOrDefault().ToString("G"));
        }
        public async Task Can_Select_Sparse_Fieldsets()
        {
            // Arrange
            var todoItem = new TodoItem
            {
                Description  = "description",
                Ordinal      = 1,
                CreatedDate  = new DateTime(2002, 2, 2),
                AchievedDate = new DateTime(2002, 2, 4)
            };

            _dbContext.TodoItems.Add(todoItem);
            await _dbContext.SaveChangesAsync();

            var properties = _resourceGraph
                             .GetAttributes <TodoItem>(e => new { e.Id, e.Description, e.CreatedDate, e.AchievedDate })
                             .Select(x => x.PropertyInfo.Name);

            var resourceFactory = new DefaultResourceFactory(new ServiceContainer());

            // Act
            var query = _dbContext
                        .TodoItems
                        .Where(t => t.Id == todoItem.Id)
                        .Select(properties, resourceFactory);

            var result = await query.FirstAsync();

            // Assert
            Assert.Equal(0, result.Ordinal);
            Assert.Equal(todoItem.Description, result.Description);
            Assert.Equal(todoItem.CreatedDate.ToString("G"), result.CreatedDate.ToString("G"));
            Assert.Equal(todoItem.AchievedDate.GetValueOrDefault().ToString("G"), result.AchievedDate.GetValueOrDefault().ToString("G"));
        }
        public async Task Can_Select_Sparse_Fieldsets()
        {
            // Arrange
            var todoItem = new TodoItem
            {
                Description  = "description",
                Ordinal      = 1,
                CreatedDate  = DateTime.Now,
                AchievedDate = DateTime.Now.AddDays(2)
            };

            _dbContext.TodoItems.Add(todoItem);
            await _dbContext.SaveChangesAsync();

            // Act
            var query = _dbContext
                        .TodoItems
                        .Where(t => t.Id == todoItem.Id)
                        .Select(_resourceGraph.GetAttributes <TodoItem>(e => new { e.Id, e.Description, e.CreatedDate, e.AchievedDate }));

            var result = await query.FirstAsync();

            // Assert
            Assert.Equal(0, result.Ordinal);
            Assert.Equal(todoItem.Description, result.Description);
            Assert.Equal(todoItem.CreatedDate.ToString("G"), result.CreatedDate.ToString("G"));
            Assert.Equal(todoItem.AchievedDate.GetValueOrDefault().ToString("G"), result.AchievedDate.GetValueOrDefault().ToString("G"));
        }
        /// <summary>
        /// Helper method for parsing query parameters into attributes
        /// </summary>
        protected AttrAttribute GetAttribute(string queryParameterName, string target, RelationshipAttribute relationship = null)
        {
            var attribute = relationship != null
                ? _resourceGraph.GetAttributes(relationship.RightType).FirstOrDefault(a => a.Is(target))
                : _requestResource.Attributes.FirstOrDefault(attr => attr.Is(target));

            if (attribute == null)
            {
                throw new InvalidQueryStringParameterException(queryParameterName,
                                                               "The attribute requested in query string does not exist.",
                                                               $"The attribute '{target}' does not exist on resource '{_requestResource.ResourceName}'.");
            }

            return(attribute);
        }
        /// <summary>
        /// By default, the client serializer includes all attributes in the result, unless a list of allowed attributes was supplied using the
        /// <see cref="AttributesToSerialize" /> method. For any related resources, attributes are never exposed.
        /// </summary>
        private IReadOnlyCollection <AttrAttribute> GetAttributesToSerialize(IIdentifiable resource)
        {
            Type currentResourceType = resource.GetType();

            if (_currentTargetedResource != currentResourceType)
            {
                // We're dealing with a relationship that is being serialized, for which
                // we never want to include any attributes in the request body.
                return(new List <AttrAttribute>());
            }

            if (AttributesToSerialize == null)
            {
                return(_resourceGraph.GetAttributes(currentResourceType));
            }

            return(AttributesToSerialize);
        }
        /// <summary>
        /// By default, the client serializer includes all attributes in the result,
        /// unless a list of allowed attributes was supplied using the <see cref="AttributesToSerialize"/>
        /// method. For any related resources, attributes are never exposed.
        /// </summary>
        private List <AttrAttribute> GetAttributesToSerialize(IIdentifiable entity)
        {
            var currentResourceType = entity.GetType();

            if (_currentTargetedResource != currentResourceType)
            {
                // We're dealing with a relationship that is being serialized, for which
                // we never want to include any attributes in the payload.
                return(new List <AttrAttribute>());
            }

            if (AttributesToSerialize == null)
            {
                return(_resourceGraph.GetAttributes(currentResourceType));
            }

            return(AttributesToSerialize.ToList());
        }
Example #7
0
        /// <summary>
        /// Helper method for parsing query parameters into attributes
        /// </summary>
        protected AttrAttribute GetAttribute(string target, RelationshipAttribute relationship = null)
        {
            AttrAttribute attribute;

            if (relationship != null)
            {
                attribute = _resourceGraph.GetAttributes(relationship.RightType).FirstOrDefault(a => a.Is(target));
            }
            else
            {
                attribute = _requestResource.Attributes.FirstOrDefault(attr => attr.Is(target));
            }

            if (attribute == null)
            {
                throw new JsonApiException(400, $"'{target}' is not a valid attribute.");
            }

            return(attribute);
        }
Example #8
0
        public static SparseFieldSetExpression Excluding <TResource>(this SparseFieldSetExpression sparseFieldSet,
                                                                     Expression <Func <TResource, dynamic> > attributeSelector, IResourceGraph resourceGraph)
            where TResource : class, IIdentifiable
        {
            if (attributeSelector == null)
            {
                throw new ArgumentNullException(nameof(attributeSelector));
            }

            if (resourceGraph == null)
            {
                throw new ArgumentNullException(nameof(resourceGraph));
            }

            foreach (var attribute in resourceGraph.GetAttributes(attributeSelector))
            {
                sparseFieldSet = ExcludeAttribute(sparseFieldSet, attribute);
            }

            return(sparseFieldSet);
        }
Example #9
0
        /// <inheritdoc/>
        public List <AttrAttribute> GetAllowedAttributes(Type type, RelationshipAttribute relationship = null)
        {   // get the list of all exposed attributes for the given type.
            var allowed = _resourceGraph.GetAttributes(type);

            var resourceDefinition = _provider.Get(type);

            if (resourceDefinition != null)
            {
                // The set of allowed attributes to be exposed was defined on the resource definition
                allowed = allowed.Intersect(resourceDefinition.GetAllowedAttributes()).ToList();
            }

            var sparseFieldsSelection = _sparseFieldsService.Get(relationship);

            if (sparseFieldsSelection != null && sparseFieldsSelection.Any())
            {
                // from the allowed attributes, select the ones flagged by sparse field selection.
                allowed = allowed.Intersect(sparseFieldsSelection).ToList();
            }

            return(allowed);
        }
 private HashSet <AttrAttribute> GetViewableAttributes(Type resourceType)
 {
     return(_resourceGraph.GetAttributes(resourceType)
            .Where(attr => attr.Capabilities.HasFlag(AttrCapabilities.AllowView))
            .ToHashSet());
 }