Skip to content

Commit

Permalink
Add flag to skip property verification in ODataResource (#2812)
Browse files Browse the repository at this point in the history
* Add flag to skip property verificaiton in ODataResourceBase

* Add tests

* Add to PublicAPI
  • Loading branch information
habbes authored Jan 11, 2024
1 parent 67365d6 commit 85a5188
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/Microsoft.OData.Core/ODataResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,15 @@ public IEnumerable<ODataFunction> Functions
get { return this.MetadataBuilder.GetFunctions(); }
}

/// <summary>
/// Gets or sets whether to skip property verification. When set to false (default behaviour),
/// the properties collection will be verified to ensure it doesn't contain invalid values.
/// When set to true, it's the caller's responsibility to ensure the property values are valid
/// before setting the <see cref="Properties"/> property. This can be a useful optimization
/// in hot paths when you're sure property values are valid.
/// </summary>
public bool SkipPropertyVerification { get; set; }

/// <summary>Gets or sets the resource properties.</summary>
/// <returns>The resource properties.</returns>
/// <remarks>
Expand All @@ -190,7 +199,11 @@ public IEnumerable<ODataProperty> Properties

set
{
VerifyProperties(value);
if (!this.SkipPropertyVerification)
{
VerifyProperties(value);
}

this.properties = value;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Microsoft.OData.ODataResourceBase.SkipPropertyVerification.get -> bool
Microsoft.OData.ODataResourceBase.SkipPropertyVerification.set -> void
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Microsoft.OData.ODataResourceBase.SkipPropertyVerification.get -> bool
Microsoft.OData.ODataResourceBase.SkipPropertyVerification.set -> void
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Microsoft.OData.ODataResourceBase.SkipPropertyVerification.get -> bool
Microsoft.OData.ODataResourceBase.SkipPropertyVerification.set -> void
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Microsoft.OData.ODataResourceBase.SkipPropertyVerification.get -> bool
Microsoft.OData.ODataResourceBase.SkipPropertyVerification.set -> void
Original file line number Diff line number Diff line change
Expand Up @@ -316,5 +316,42 @@ public void ODataResourcePropertyWithCollectionODataResourceValueThrows()
var exception = Assert.Throws<ODataException>(test);
Assert.Equal(Strings.ODataResource_PropertyValueCannotBeODataResourceValue("CollectionProperty"), exception.Message);
}

[Fact]
public void WhenSkipPropertyVerificatonIsTrue_ODataResourcePropertyWithODataResourceValue_DoesNotThrow()
{
ODataResource resource = new ODataResource
{
TypeName = "NS.Resource",
SkipPropertyVerification = true
};

resource.Properties = new[]
{
new ODataProperty { Name = "ResourceProperty", Value = new ODataResourceValue() }
};
}

[Fact]
public void WhenSkipPropertyVerificatonIsTrue_ODataResourcePropertyWithCollectionODataResourceValue_DoesNotThrow()
{
ODataResource resource = new ODataResource
{
TypeName = "NS.Resource",
SkipPropertyVerification = true
};

resource.Properties = new[]
{
new ODataProperty
{
Name = "CollectionProperty",
Value = new ODataCollectionValue
{
Items = new [] { new ODataResourceValue() }
}
}
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4857,6 +4857,7 @@ public abstract class Microsoft.OData.ODataResourceBase : Microsoft.OData.ODataI
Microsoft.OData.ODataStreamReferenceValue MediaResource { public get; public set; }
System.Collections.Generic.IEnumerable`1[[Microsoft.OData.ODataProperty]] Properties { public get; public set; }
System.Uri ReadLink { public get; public set; }
bool SkipPropertyVerification { public get; public set; }
string TypeName { public get; public set; }

public void AddAction (Microsoft.OData.ODataAction action)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4857,6 +4857,7 @@ public abstract class Microsoft.OData.ODataResourceBase : Microsoft.OData.ODataI
Microsoft.OData.ODataStreamReferenceValue MediaResource { public get; public set; }
System.Collections.Generic.IEnumerable`1[[Microsoft.OData.ODataProperty]] Properties { public get; public set; }
System.Uri ReadLink { public get; public set; }
bool SkipPropertyVerification { public get; public set; }
string TypeName { public get; public set; }

public void AddAction (Microsoft.OData.ODataAction action)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4857,6 +4857,7 @@ public abstract class Microsoft.OData.ODataResourceBase : Microsoft.OData.ODataI
Microsoft.OData.ODataStreamReferenceValue MediaResource { public get; public set; }
System.Collections.Generic.IEnumerable`1[[Microsoft.OData.ODataProperty]] Properties { public get; public set; }
System.Uri ReadLink { public get; public set; }
bool SkipPropertyVerification { public get; public set; }
string TypeName { public get; public set; }

public void AddAction (Microsoft.OData.ODataAction action)
Expand Down

0 comments on commit 85a5188

Please sign in to comment.