-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix subtle bug when deserializing property without value #1308
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -200,6 +200,10 @@ private static void ReadODataItem(ODataReader reader, Stack<ODataItemWrapper> it | |
resourceSetParentWrapper.Items.Add(new ODataPrimitiveWrapper((ODataPrimitiveValue)reader.Item)); | ||
break; | ||
|
||
case ODataReaderState.NestedProperty: | ||
// Property without value - do nothing | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do nothing? need more details There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @xuzhg What do you suggest we should do here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we 'd add at least:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why didn't we need a case for this state before? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @xuzhg @habbes The block for the In OData/odata.net#2786, we introduced support for reading property without value. Before that, we would throw an exception if we came across a property without value. When we changed that, the |
||
break; | ||
|
||
default: | ||
Contract.Assert(false, "We should never get here, it means the ODataReader reported a wrong state."); | ||
break; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1553,6 +1553,40 @@ public void ReadAsync_ThrowsOnUnknownEntityType() | |
typeof(Product), _readContext).Wait(), "The property 'Concurrency' does not exist on type 'ODataDemo.Product'. Make sure to only use property names that are defined by the type or mark the type as open type."); | ||
} | ||
|
||
[Fact] | ||
public async Task ReadAsync_ForPropertyWithoutValue() | ||
{ | ||
// Arrange | ||
string content = "{" + | ||
"\"ID\":0," + | ||
"\"Name\":\"Bread\"," + | ||
"\"Description\":\"Whole grain bread\"," + | ||
"\"PublishDate\":\"1997-07-01\"," + | ||
"\"[email protected]\":\"#Edm.DateTimeOffset\"," + // OData annotation - Property without value | ||
"\"[email protected]\":true," + // Custom annotation - Property without value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about the other type of properties with value? (Complex, enum) |
||
"\"Rating\":4," + | ||
"\"Price\":2.5" + | ||
"}"; | ||
|
||
ODataResourceDeserializer deserializer = new ODataResourceDeserializer(_deserializerProvider); | ||
|
||
// Act | ||
Product product = await deserializer.ReadAsync( | ||
GetODataMessageReader(content, _edmModel), | ||
typeof(Product), | ||
_readContext) as Product; | ||
|
||
// Assert | ||
Assert.Equal(0, product.ID); | ||
Assert.Equal(4, product.Rating); | ||
Assert.Equal(2.5m, product.Price); | ||
Assert.Equal(product.PublishDate, new Date(1997, 7, 1)); | ||
Assert.Equal("Bread", product.Name); | ||
Assert.Equal("Whole grain bread", product.Description); | ||
Assert.Null(product.ReleaseDate); | ||
Assert.Null(product.DiscontinuedDate); | ||
} | ||
|
||
private static ODataMessageReader GetODataMessageReader(IODataRequestMessage oDataRequestMessage, IEdmModel edmModel) | ||
{ | ||
return new ODataMessageReader(oDataRequestMessage, new ODataMessageReaderSettings(), edmModel); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why don't use 'OfType', so we don't need line 494 to 499.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was deliberate. I believe
OfType
would be more expensiveThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xuzhg Here's the code for a simple benchmark to compare the two:
And here are the results:
I think that LINQ overheads are responsible for the difference