Skip to content

Commit 04541a2

Browse files
dxrdxrxuzhg
authored andcommitted
DeltaOfT out of sync with WebAPI: missing UpdatableProperties
1 parent 97f9cde commit 04541a2

File tree

6 files changed

+184
-57
lines changed

6 files changed

+184
-57
lines changed

src/Microsoft.AspNetCore.OData/Deltas/DeltaOfT.cs

+21-18
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ namespace Microsoft.AspNetCore.OData.Deltas
2525
public class Delta<T> : Delta, IDelta, ITypedDelta where T : class
2626
{
2727
// cache property accessors for this type and all its derived types.
28-
private static ConcurrentDictionary<Type, Dictionary<string, PropertyAccessor<T>>> _propertyCache
28+
private static readonly ConcurrentDictionary<Type, Dictionary<string, PropertyAccessor<T>>> _propertyCache
2929
= new ConcurrentDictionary<Type, Dictionary<string, PropertyAccessor<T>>>();
3030

3131
private Dictionary<string, PropertyAccessor<T>> _allProperties;
32-
private HashSet<string> _updatableProperties;
32+
private List<string> _updatableProperties;
3333

3434
private HashSet<string> _changedProperties;
3535

@@ -39,7 +39,7 @@ private static ConcurrentDictionary<Type, Dictionary<string, PropertyAccessor<T>
3939
private T _instance;
4040
private Type _structuredType;
4141

42-
private PropertyInfo _dynamicDictionaryPropertyinfo;
42+
private readonly PropertyInfo _dynamicDictionaryPropertyinfo;
4343
private HashSet<string> _changedDynamicProperties;
4444
private IDictionary<string, object> _dynamicDictionaryCache;
4545

@@ -98,16 +98,19 @@ public Delta(Type structuralType, IEnumerable<string> updatableProperties,
9898

9999
/// <inheritdoc/>
100100
public virtual Type StructuredType
101-
{
102-
get
103-
{
104-
return _structuredType;
105-
}
106-
}
101+
=> _structuredType;
107102

108103
/// <inheritdoc/>
109104
public virtual Type ExpectedClrType => typeof(T);
110105

106+
/// <summary>
107+
/// The list of property names that can be updated.
108+
/// </summary>
109+
/// <remarks>When the list is modified, any modified properties that were removed from the list are no longer
110+
/// considered to be changed.</remarks>
111+
public IList<string> UpdatableProperties
112+
=> _updatableProperties;
113+
111114
/// <inheritdoc/>
112115
public override void Clear()
113116
{
@@ -172,7 +175,7 @@ public override bool TryGetPropertyValue(string name, out object value)
172175
}
173176
}
174177

175-
if (this._deltaNestedResources.ContainsKey(name))
178+
if (_deltaNestedResources.ContainsKey(name))
176179
{
177180
// If this is a nested resource, get the value from the dictionary of nested resources.
178181
object deltaNestedResource = _deltaNestedResources[name];
@@ -259,7 +262,7 @@ public T GetInstance()
259262
/// </summary>
260263
public override IEnumerable<string> GetChangedPropertyNames()
261264
{
262-
return _changedProperties.Concat(_deltaNestedResources.Keys);
265+
return _changedProperties.Intersect(_updatableProperties).Concat(_deltaNestedResources.Keys);
263266
}
264267

265268
/// <summary>
@@ -269,7 +272,8 @@ public override IEnumerable<string> GetChangedPropertyNames()
269272
/// </summary>
270273
public override IEnumerable<string> GetUnchangedPropertyNames()
271274
{
272-
return _updatableProperties.Except(GetChangedPropertyNames());
275+
// UpdatableProperties could include arbitrary strings, filter by _allProperties
276+
return _updatableProperties.Intersect(_allProperties.Keys).Except(GetChangedPropertyNames());
273277
}
274278

275279
/// <summary>
@@ -295,7 +299,7 @@ public void CopyChangedValues(T original)
295299

296300
// For regular non-structural properties at current level.
297301
PropertyAccessor<T>[] propertiesToCopy =
298-
this._changedProperties.Select(s => _allProperties[s]).ToArray();
302+
_changedProperties.Intersect(_updatableProperties).Select(s => _allProperties[s]).ToArray();
299303
foreach (PropertyAccessor<T> propertyToCopy in propertiesToCopy)
300304
{
301305
propertyToCopy.Copy(_instance, original);
@@ -498,12 +502,11 @@ private void InitializeProperties(IEnumerable<string> updatableProperties)
498502

499503
if (updatableProperties != null)
500504
{
501-
_updatableProperties = new HashSet<string>(updatableProperties);
502-
_updatableProperties.IntersectWith(_allProperties.Keys);
505+
_updatableProperties = updatableProperties.Intersect(_allProperties.Keys).ToList();
503506
}
504507
else
505508
{
506-
_updatableProperties = new HashSet<string>(_allProperties.Keys);
509+
_updatableProperties = new List<string>(_allProperties.Keys);
507510
}
508511

509512
if (_dynamicDictionaryPropertyinfo != null)
@@ -618,7 +621,7 @@ private bool TrySetPropertyValueInternal(string name, object value)
618621
throw Error.ArgumentNull("name");
619622
}
620623

621-
if (!_updatableProperties.Contains(name))
624+
if (!(_allProperties.ContainsKey(name) && _updatableProperties.Contains(name)))
622625
{
623626
return false;
624627
}
@@ -648,7 +651,7 @@ private bool TrySetNestedResourceInternal(string name, object deltaNestedResourc
648651
throw Error.ArgumentNull("name");
649652
}
650653

651-
if (!_updatableProperties.Contains(name))
654+
if (!(_allProperties.ContainsKey(name) && _updatableProperties.Contains(name)))
652655
{
653656
return false;
654657
}

src/Microsoft.AspNetCore.OData/Microsoft.AspNetCore.OData.xml

+7
Original file line numberDiff line numberDiff line change
@@ -1615,6 +1615,13 @@
16151615
<member name="P:Microsoft.AspNetCore.OData.Deltas.Delta`1.ExpectedClrType">
16161616
<inheritdoc/>
16171617
</member>
1618+
<member name="P:Microsoft.AspNetCore.OData.Deltas.Delta`1.UpdatableProperties">
1619+
<summary>
1620+
The list of property names that can be updated.
1621+
</summary>
1622+
<remarks>When the list is modified, any modified properties that were removed from the list are no longer
1623+
considered to be changed.</remarks>
1624+
</member>
16181625
<member name="M:Microsoft.AspNetCore.OData.Deltas.Delta`1.Clear">
16191626
<inheritdoc/>
16201627
</member>

src/Microsoft.AspNetCore.OData/PublicAPI.Unshipped.txt

+1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ Microsoft.AspNetCore.OData.Deltas.Delta<T>.Delta(System.Type structuralType, Sys
180180
Microsoft.AspNetCore.OData.Deltas.Delta<T>.GetInstance() -> T
181181
Microsoft.AspNetCore.OData.Deltas.Delta<T>.Patch(T original) -> void
182182
Microsoft.AspNetCore.OData.Deltas.Delta<T>.Put(T original) -> void
183+
Microsoft.AspNetCore.OData.Deltas.Delta<T>.UpdatableProperties.get -> System.Collections.Generic.IList<string>
183184
Microsoft.AspNetCore.OData.Deltas.DeltaDeletedResource<T>
184185
Microsoft.AspNetCore.OData.Deltas.DeltaDeletedResource<T>.DeltaDeletedResource() -> void
185186
Microsoft.AspNetCore.OData.Deltas.DeltaDeletedResource<T>.DeltaDeletedResource(System.Type structuralType) -> void

0 commit comments

Comments
 (0)