diff --git a/src/Microsoft.Restier.Publishers.OData/Extensions.cs b/src/Microsoft.Restier.Publishers.OData/Extensions.cs index 231650f3..39fc409e 100644 --- a/src/Microsoft.Restier.Publishers.OData/Extensions.cs +++ b/src/Microsoft.Restier.Publishers.OData/Extensions.cs @@ -28,9 +28,11 @@ internal static class Extensions private static PropertyInfo etagConcurrencyPropertiesProperty = typeof(ETag).GetProperty( PropertyNameOfConcurrencyProperties, BindingFlags.NonPublic | BindingFlags.Instance); + // TODO GithubIssue#485 considering move to API class DI instance private static ConcurrentDictionary concurrencyCheckFlags = new ConcurrentDictionary(); + // TODO GithubIssue#485 considering move to API class DI instance private static ConcurrentDictionary> typePropertiesAttributes = new ConcurrentDictionary>(); @@ -80,7 +82,8 @@ public static IReadOnlyDictionary CreatePropertyDictionary( PropertyAttributes attributes; if (propertiesAttributes != null && propertiesAttributes.TryGetValue(propertyName, out attributes)) { - if ((isCreation && attributes.IgnoreForCreation) || (!isCreation && attributes.IgnoreForUpdate)) + if ((isCreation && (attributes & PropertyAttributes.IgnoreForCreation) != PropertyAttributes.None) + || (!isCreation && (attributes & PropertyAttributes.IgnoreForUpdate) != PropertyAttributes.None)) { // Will not get the properties for update or creation continue; @@ -116,7 +119,7 @@ public static IDictionary RetrievePropertiesAttribut foreach (var property in edmType.DeclaredProperties) { var annotations = model.FindVocabularyAnnotations(property); - PropertyAttributes attributes = null; + var attributes = PropertyAttributes.None; foreach (var annotation in annotations) { var valueAnnotation = annotation as EdmAnnotation; @@ -125,34 +128,22 @@ public static IDictionary RetrievePropertiesAttribut continue; } - if (valueAnnotation.Term.Namespace == CoreVocabularyModel.ImmutableTerm.Namespace - && valueAnnotation.Term.Name == CoreVocabularyModel.ImmutableTerm.Name) + if (valueAnnotation.Term.IsSameTerm(CoreVocabularyModel.ImmutableTerm)) { var value = valueAnnotation.Value as EdmBooleanConstant; if (value != null && value.Value) { - if (attributes == null) - { - attributes = new PropertyAttributes(); - } - - attributes.IgnoreForUpdate = true; + attributes |= PropertyAttributes.IgnoreForUpdate; } } - if (valueAnnotation.Term.Namespace == CoreVocabularyModel.ComputedTerm.Namespace - && valueAnnotation.Term.Name == CoreVocabularyModel.ComputedTerm.Name) + if (valueAnnotation.Term.IsSameTerm(CoreVocabularyModel.ComputedTerm)) { var value = valueAnnotation.Value as EdmBooleanConstant; if (value != null && value.Value) { - if (attributes == null) - { - attributes = new PropertyAttributes(); - } - - attributes.IgnoreForUpdate = true; - attributes.IgnoreForCreation = true; + attributes |= PropertyAttributes.IgnoreForUpdate; + attributes |= PropertyAttributes.IgnoreForCreation; } } @@ -161,7 +152,7 @@ public static IDictionary RetrievePropertiesAttribut } // Add property attributes to the dictionary - if (attributes != null) + if (attributes != PropertyAttributes.None) { if (propertiesAttributes == null) { @@ -241,5 +232,15 @@ public static IEdmTypeReference GetTypeReference(this Type type, IEdmModel model return type.GetPrimitiveTypeReference(); } + + public static bool IsSameTerm(this IEdmTerm sourceTerm, IEdmTerm targetTerm) + { + if (sourceTerm.Namespace == targetTerm.Namespace && sourceTerm.Name == targetTerm.Name) + { + return true; + } + + return false; + } } } diff --git a/src/Microsoft.Restier.Publishers.OData/Model/PropertyAttributes.cs b/src/Microsoft.Restier.Publishers.OData/Model/PropertyAttributes.cs index bfc14f0f..5358b153 100644 --- a/src/Microsoft.Restier.Publishers.OData/Model/PropertyAttributes.cs +++ b/src/Microsoft.Restier.Publishers.OData/Model/PropertyAttributes.cs @@ -1,28 +1,36 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. +using System; + namespace Microsoft.Restier.Publishers.OData { - internal class PropertyAttributes + [Flags] + internal enum PropertyAttributes { + /// + /// No flag is set for the property + /// + None = 0x0, + /// /// Gets or sets a value indicating whether the property should be ignored during update /// - public bool IgnoreForUpdate { get; set; } + IgnoreForUpdate = 0x1, /// /// Gets or sets a value indicating whether the property should be ignored during creation /// - public bool IgnoreForCreation { get; set; } + IgnoreForCreation = 0x2, /// /// Gets or sets a value indicating whether there is permission to read the property /// - public bool NoReadPermission { get; set; } + NoReadPermission = 0x4, /// /// Gets or sets a value indicating whether there is permission to write the property /// - public bool NoWritePermission { get; set; } + NoWritePermission = 0x8 } } diff --git a/test/ODataEndToEnd/Microsoft.OData.Service.Library/DataStoreManager/DefaultDataStoreManager.cs b/test/ODataEndToEnd/Microsoft.OData.Service.Library/DataStoreManager/DefaultDataStoreManager.cs index a4e50dc1..cf4a0118 100644 --- a/test/ODataEndToEnd/Microsoft.OData.Service.Library/DataStoreManager/DefaultDataStoreManager.cs +++ b/test/ODataEndToEnd/Microsoft.OData.Service.Library/DataStoreManager/DefaultDataStoreManager.cs @@ -110,9 +110,9 @@ private void ResouceTimeoutHandler(object source, EventArgs e) private class DataStoreUnit { - public TKey DatastoreKey { get; } + public TKey DatastoreKey { get; private set; } - public TDataStoreType DataStore { get; } + public TDataStoreType DataStore { get; private set; } public DateTime DataStoreLastUsedDateTime { get; private set; }