public void DeserializeAttributes_VariousDataTypes_CanDeserialize(string member, object value, bool expectError = false) { // Arrange var content = new Document { Data = new ResourceObject { Type = "testResource", Id = "1", Attributes = new Dictionary <string, object> { [member] = value } } }; string body = JsonConvert.SerializeObject(content); // Act Func <TestResource> action = () => (TestResource)_deserializer.Deserialize(body); // Assert if (expectError) { Assert.ThrowsAny <FormatException>(action); } else { TestResource resource = action(); PropertyInfo pi = ResourceGraph.GetResourceContext("testResource").Attributes.Single(attr => attr.PublicName == member).Property; object deserializedValue = pi.GetValue(resource); if (member == "intField") { Assert.Equal(1, deserializedValue); } else if (member == "nullableIntField" && value == null) { Assert.Null(deserializedValue); } else if (member == "nullableIntField" && (string)value == "1") { Assert.Equal(1, deserializedValue); } else if (member == "guidField") { Assert.Equal(deserializedValue, Guid.Parse("1a68be43-cc84-4924-a421-7f4d614b7781")); } else if (member == "dateTimeField") { Assert.Equal(deserializedValue, DateTime.Parse("9/11/2019 11:41:40 AM")); } else { Assert.Equal(value, deserializedValue); } } }
protected IFieldsToSerialize GetSerializableFields() { var mock = new Mock <IFieldsToSerialize>(); mock.Setup(m => m.GetAttributes(It.IsAny <Type>())).Returns <Type>(t => ResourceGraph.GetResourceContext(t).Attributes); mock.Setup(m => m.GetRelationships(It.IsAny <Type>())).Returns <Type>(t => ResourceGraph.GetResourceContext(t).Relationships); return(mock.Object); }
public LegacyFilterParseTests() { Options.EnableLegacyFilterNotation = true; CurrentRequest.PrimaryResource = ResourceGraph.GetResourceContext <Article>(); var resourceFactory = new ResourceFactory(new ServiceContainer()); _reader = new FilterQueryStringParameterReader(CurrentRequest, ResourceGraph, resourceFactory, Options); }
private List <RelationshipAttribute> GetIncludedRelationshipsChain(string chain) { var parsedChain = new List <RelationshipAttribute>(); var resourceContext = ResourceGraph.GetResourceContext <Article>(); var splitPath = chain.Split('.'); foreach (var requestedRelationship in splitPath) { var relationship = resourceContext.Relationships.Single(r => r.PublicName == requestedRelationship); parsedChain.Add(relationship); resourceContext = ResourceGraph.GetResourceContext(relationship.RightType); } return(parsedChain); }
public override FilterExpression OnApplyFilter(FilterExpression existingFilter) { // Use case: automatically exclude deleted resources for all requests. ResourceContext resourceContext = ResourceGraph.GetResourceContext <CallableResource>(); AttrAttribute isDeletedAttribute = resourceContext.Attributes.Single(attribute => attribute.Property.Name == nameof(CallableResource.IsDeleted)); var isNotDeleted = new ComparisonExpression(ComparisonOperator.Equals, new ResourceFieldChainExpression(isDeletedAttribute), new LiteralConstantExpression(bool.FalseString)); return(existingFilter == null ? (FilterExpression)isNotDeleted : new LogicalExpression(LogicalOperator.And, ArrayFactory.Create(isNotDeleted, existingFilter))); }
private ResourceFieldChainExpression GetRelationshipsInPath(string includePath) { ResourceContext resourceContext = ResourceGraph.GetResourceContext <TodoItem>(); var relationships = new List <RelationshipAttribute>(); foreach (string relationshipName in includePath.Split('.')) { RelationshipAttribute relationship = resourceContext.Relationships.Single(nextRelationship => nextRelationship.PublicName == relationshipName); relationships.Add(relationship); resourceContext = ResourceGraph.GetResourceContext(relationship.RightType); } return(new ResourceFieldChainExpression(relationships)); }
private IReadOnlyCollection <RelationshipAttribute> GetIncludedRelationshipsChain(string chain) { var parsedChain = new List <RelationshipAttribute>(); ResourceContext resourceContext = ResourceGraph.GetResourceContext <Article>(); string[] splitPath = chain.Split('.'); foreach (string requestedRelationship in splitPath) { RelationshipAttribute relationship = resourceContext.Relationships.Single(nextRelationship => nextRelationship.PublicName == requestedRelationship); parsedChain.Add(relationship); resourceContext = ResourceGraph.GetResourceContext(relationship.RightType); } return(parsedChain); }