From a385b01167326b72f68d92715e1a2411e0c50690 Mon Sep 17 00:00:00 2001 From: Bart Koelman Date: Mon, 6 Jan 2020 13:02:37 +0100 Subject: [PATCH 1/2] Removed AttrAttribute.InternalAttributeName --- benchmarks/Query/QueryParser_Benchmarks.cs | 2 +- .../Builders/ResourceGraphBuilder.cs | 4 +-- .../Extensions/IQueryableExtensions.cs | 16 +++++----- .../Internal/Query/BaseQueryContext.cs | 4 +-- .../Models/Annotation/AttrAttribute.cs | 31 +++---------------- .../Common/ResourceObjectBuilder.cs | 6 ++-- .../Data/EntityRepositoryTests.cs | 2 +- .../Models/ResourceDefinitionTests.cs | 6 ++-- .../SparseFieldsServiceTests.cs | 17 +++++----- 9 files changed, 31 insertions(+), 57 deletions(-) diff --git a/benchmarks/Query/QueryParser_Benchmarks.cs b/benchmarks/Query/QueryParser_Benchmarks.cs index 68c08e8e79..242df86b18 100644 --- a/benchmarks/Query/QueryParser_Benchmarks.cs +++ b/benchmarks/Query/QueryParser_Benchmarks.cs @@ -25,7 +25,7 @@ public QueryParser_Benchmarks() { var requestMock = new Mock(); requestMock.Setup(m => m.GetRequestResource()).Returns(new ResourceContext { Attributes = new List { - new AttrAttribute(ATTRIBUTE, ATTRIBUTE) + new AttrAttribute(ATTRIBUTE) } }); var options = new JsonApiOptions(); diff --git a/src/JsonApiDotNetCore/Builders/ResourceGraphBuilder.cs b/src/JsonApiDotNetCore/Builders/ResourceGraphBuilder.cs index 8b0ad0e348..414be55944 100644 --- a/src/JsonApiDotNetCore/Builders/ResourceGraphBuilder.cs +++ b/src/JsonApiDotNetCore/Builders/ResourceGraphBuilder.cs @@ -99,8 +99,7 @@ protected virtual List GetAttributes(Type entityType) var idAttr = new AttrAttribute() { PublicAttributeName = _formatter.FormatPropertyName(prop), - PropertyInfo = prop, - InternalAttributeName = prop.Name + PropertyInfo = prop }; attributes.Add(idAttr); continue; @@ -111,7 +110,6 @@ protected virtual List GetAttributes(Type entityType) continue; attribute.PublicAttributeName = attribute.PublicAttributeName ?? _formatter.FormatPropertyName(prop); - attribute.InternalAttributeName = prop.Name; attribute.PropertyInfo = prop; attributes.Add(attribute); diff --git a/src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs b/src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs index 49f6dfc326..2f66d34837 100644 --- a/src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs +++ b/src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs @@ -65,7 +65,7 @@ public static IQueryable Filter(this IQueryable sourc } public static IQueryable Select(this IQueryable source, IEnumerable columns) - => CallGenericSelectMethod(source, columns.Select(attr => attr.InternalAttributeName).ToList()); + => CallGenericSelectMethod(source, columns.Select(attr => attr.PropertyInfo.Name).ToList()); public static IOrderedQueryable Sort(this IQueryable source, SortQueryContext sortQuery) { @@ -191,7 +191,7 @@ private static Expression GetFilterExpressionLambda(Expression left, Expression private static IQueryable CallGenericWhereContainsMethod(IQueryable source, FilterQueryContext filter) { var concreteType = typeof(TSource); - var property = concreteType.GetProperty(filter.Attribute.InternalAttributeName); + var property = concreteType.GetProperty(filter.Attribute.PropertyInfo.Name); try { @@ -201,10 +201,10 @@ private static IQueryable CallGenericWhereContainsMethod(IQuer if (filter.IsAttributeOfRelationship) { var relation = Expression.PropertyOrField(entity, filter.Relationship.InternalRelationshipName); - member = Expression.Property(relation, filter.Attribute.InternalAttributeName); + member = Expression.Property(relation, filter.Attribute.PropertyInfo.Name); } else - member = Expression.Property(entity, filter.Attribute.InternalAttributeName); + member = Expression.Property(entity, filter.Attribute.PropertyInfo.Name); var method = ContainsMethod.MakeGenericMethod(member.Type); var obj = TypeHelper.ConvertListType(propertyValues, member.Type); @@ -259,9 +259,9 @@ private static IQueryable CallGenericWhereMethod(IQueryable CallGenericWhereMethod(IQueryable InternalAttributeName; - - - /// - /// Do not use this overload in your applications. - /// Provides a method for instantiating instances of `AttrAttribute` and specifying - /// the internal property name. - /// The primary intent for this was to enable certain types of unit tests to be possible. - /// This overload will be deprecated and removed in future releases and an alternative - /// for unit tests will be provided. - /// - public AttrAttribute(string publicName, string internalName, bool isImmutable = false) - { - PublicAttributeName = publicName; - InternalAttributeName = internalName; - IsImmutable = isImmutable; - } + public string ExposedInternalMemberName => PropertyInfo.Name; /// /// How this attribute is exposed through the API /// - public string PublicAttributeName { get; internal set;} - - /// - /// The internal property name this attribute belongs to. - /// - public string InternalAttributeName { get; internal set; } + public string PublicAttributeName { get; internal set; } /// /// Prevents PATCH requests from updating the value. @@ -84,7 +63,7 @@ public AttrAttribute(string publicName, string internalName, bool isImmutable = /// /// The member property info /// - internal PropertyInfo PropertyInfo { get; set; } + public PropertyInfo PropertyInfo { get; set; } /// /// Get the value of the attribute for the given object. @@ -119,7 +98,7 @@ public void SetValue(object entity, object newValue) private PropertyInfo GetResourceProperty(object resource) { // There are some scenarios, especially ones where users are using a different - // data model than view model, where they may use a repository implmentation + // data model than view model, where they may use a repository implementation // that does not match the deserialized type. For now, we will continue to support // this use case. var targetType = resource.GetType(); @@ -127,7 +106,7 @@ private PropertyInfo GetResourceProperty(object resource) { var propertyInfo = resource .GetType() - .GetProperty(InternalAttributeName); + .GetProperty(PropertyInfo.Name); return propertyInfo; diff --git a/src/JsonApiDotNetCore/Serialization/Common/ResourceObjectBuilder.cs b/src/JsonApiDotNetCore/Serialization/Common/ResourceObjectBuilder.cs index 6e0dde1fb0..784f2902a6 100644 --- a/src/JsonApiDotNetCore/Serialization/Common/ResourceObjectBuilder.cs +++ b/src/JsonApiDotNetCore/Serialization/Common/ResourceObjectBuilder.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections; using System.Collections.Generic; using System.Linq; @@ -31,7 +31,7 @@ public ResourceObject Build(IIdentifiable entity, IEnumerable att var ro = new ResourceObject { Type = resourceContext.ResourceName, Id = entity.StringId.NullIfEmpty() }; // populating the top-level "attribute" member of a resource object. never include "id" as an attribute - if (attributes != null && (attributes = attributes.Where(attr => attr.InternalAttributeName != _identifiablePropertyName)).Any()) + if (attributes != null && (attributes = attributes.Where(attr => attr.PropertyInfo.Name != _identifiablePropertyName)).Any()) ProcessAttributes(entity, attributes, ro); // populating the top-level "relationship" member of a resource object. @@ -145,4 +145,4 @@ private void ProcessAttributes(IIdentifiable entity, IEnumerable } } } -} \ No newline at end of file +} diff --git a/test/IntegrationTests/Data/EntityRepositoryTests.cs b/test/IntegrationTests/Data/EntityRepositoryTests.cs index 39f414bf39..8e45c13c30 100644 --- a/test/IntegrationTests/Data/EntityRepositoryTests.cs +++ b/test/IntegrationTests/Data/EntityRepositoryTests.cs @@ -33,7 +33,7 @@ public async Task UpdateAsync_AttributesUpdated_ShouldHaveSpecificallyThoseAttri arrangeContext.Add(todoItemUpdates); arrangeContext.SaveChanges(); - var descAttr = new AttrAttribute("description", "Description") + var descAttr = new AttrAttribute("description") { PropertyInfo = typeof(TodoItem).GetProperty(nameof(TodoItem.Description)) }; diff --git a/test/UnitTests/Models/ResourceDefinitionTests.cs b/test/UnitTests/Models/ResourceDefinitionTests.cs index 28e42dfbb5..d498042b34 100644 --- a/test/UnitTests/Models/ResourceDefinitionTests.cs +++ b/test/UnitTests/Models/ResourceDefinitionTests.cs @@ -29,7 +29,7 @@ public void Request_Filter_Uses_Member_Expression() var attrs = resource.GetAllowedAttributes(); // Assert - Assert.DoesNotContain(attrs, a => a.InternalAttributeName == nameof(Model.AlwaysExcluded)); + Assert.DoesNotContain(attrs, a => a.PropertyInfo.Name == nameof(Model.AlwaysExcluded)); } [Fact] @@ -42,8 +42,8 @@ public void Request_Filter_Uses_NewExpression() var attrs = resource.GetAllowedAttributes(); // Assert - Assert.DoesNotContain(attrs, a => a.InternalAttributeName == nameof(Model.AlwaysExcluded)); - Assert.DoesNotContain(attrs, a => a.InternalAttributeName == nameof(Model.Password)); + Assert.DoesNotContain(attrs, a => a.PropertyInfo.Name == nameof(Model.AlwaysExcluded)); + Assert.DoesNotContain(attrs, a => a.PropertyInfo.Name == nameof(Model.Password)); } } diff --git a/test/UnitTests/QueryParameters/SparseFieldsServiceTests.cs b/test/UnitTests/QueryParameters/SparseFieldsServiceTests.cs index 75ccf847c9..6b0df9e779 100644 --- a/test/UnitTests/QueryParameters/SparseFieldsServiceTests.cs +++ b/test/UnitTests/QueryParameters/SparseFieldsServiceTests.cs @@ -34,11 +34,10 @@ public void Parse_ValidSelection_CanParse() // Arrange const string type = "articles"; const string attrName = "someField"; - const string internalAttrName = "SomeField"; - var attribute = new AttrAttribute(attrName) { InternalAttributeName = internalAttrName }; - var idAttribute = new AttrAttribute("id") { InternalAttributeName = "Id" }; + var attribute = new AttrAttribute(attrName); + var idAttribute = new AttrAttribute("id"); - var query = new KeyValuePair($"fields", new StringValues(attrName)); + var query = new KeyValuePair("fields", new StringValues(attrName)); var resourceContext = new ResourceContext { @@ -64,9 +63,8 @@ public void Parse_TypeNameAsNavigation_Throws400ErrorWithRelationshipsOnlyMessag // Arrange const string type = "articles"; const string attrName = "someField"; - const string internalAttrName = "SomeField"; - var attribute = new AttrAttribute(attrName) { InternalAttributeName = internalAttrName }; - var idAttribute = new AttrAttribute("id") { InternalAttributeName = "Id" }; + var attribute = new AttrAttribute(attrName); + var idAttribute = new AttrAttribute("id"); var query = new KeyValuePair($"fields[{type}]", new StringValues(attrName)); @@ -90,9 +88,8 @@ public void Parse_DeeplyNestedSelection_Throws400ErrorWithDeeplyNestedMessage() const string type = "articles"; const string relationship = "author.employer"; const string attrName = "someField"; - const string internalAttrName = "SomeField"; - var attribute = new AttrAttribute(attrName) { InternalAttributeName = internalAttrName }; - var idAttribute = new AttrAttribute("id") { InternalAttributeName = "Id" }; + var attribute = new AttrAttribute(attrName); + var idAttribute = new AttrAttribute("id"); var query = new KeyValuePair($"fields[{relationship}]", new StringValues(attrName)); From e90fece558de4fa91d4a36a996266fadc2aa838d Mon Sep 17 00:00:00 2001 From: Bart Koelman Date: Mon, 6 Jan 2020 13:39:56 +0100 Subject: [PATCH 2/2] Removed unused IRelationshipField --- .../Models/Annotation/IRelationshipField.cs | 6 ------ .../Models/Annotation/RelationshipAttribute.cs | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) delete mode 100644 src/JsonApiDotNetCore/Models/Annotation/IRelationshipField.cs diff --git a/src/JsonApiDotNetCore/Models/Annotation/IRelationshipField.cs b/src/JsonApiDotNetCore/Models/Annotation/IRelationshipField.cs deleted file mode 100644 index a20d5fd00d..0000000000 --- a/src/JsonApiDotNetCore/Models/Annotation/IRelationshipField.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace JsonApiDotNetCore.Models -{ - public interface IRelationshipField - { - } -} \ No newline at end of file diff --git a/src/JsonApiDotNetCore/Models/Annotation/RelationshipAttribute.cs b/src/JsonApiDotNetCore/Models/Annotation/RelationshipAttribute.cs index 0e92e5ea84..4eb8d2bbfc 100644 --- a/src/JsonApiDotNetCore/Models/Annotation/RelationshipAttribute.cs +++ b/src/JsonApiDotNetCore/Models/Annotation/RelationshipAttribute.cs @@ -5,7 +5,7 @@ namespace JsonApiDotNetCore.Models { - public abstract class RelationshipAttribute : Attribute, IResourceField, IRelationshipField + public abstract class RelationshipAttribute : Attribute, IResourceField { protected RelationshipAttribute(string publicName, Link relationshipLinks, bool canInclude, string mappedBy) {