-
Notifications
You must be signed in to change notification settings - Fork 349
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
Fixes #3068: Throws ResourceTypeAnnotationNotFirst error when payload… #3069
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 |
---|---|---|
|
@@ -81,6 +81,64 @@ internal BufferingJsonReader(IJsonReader innerReader, string inStreamErrorProper | |
this.currentBufferedNode = null; | ||
} | ||
|
||
internal bool TryGetValueFromBuffered(string propertyName, bool removeIfExist, out object value) | ||
{ | ||
bool found = false; | ||
BufferedNode currentNode = this.bufferedNodesHead; | ||
if (this.bufferedNodesHead != null) | ||
{ | ||
do | ||
{ | ||
if (currentNode.NodeType == JsonNodeType.StartObject || | ||
currentNode.NodeType == JsonNodeType.StartArray) | ||
{ | ||
break; | ||
} | ||
|
||
if (currentNode.NodeType == JsonNodeType.Property && | ||
currentNode.Value.ToString() == propertyName && | ||
currentNode.Next != currentNode) // must have another node | ||
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 check whether there's another node after this? 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. Just to see if I understand, property and value are stored in separate node, so if we find the expected property node, we want to make sure there's a value node after it. Is that correct? 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. That's correct. Property and value are two node types and they are stored in separate node. |
||
{ | ||
found = true; | ||
break; | ||
} | ||
|
||
currentNode = currentNode.Next; | ||
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. can 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. If we have the 'bufferedNodesHead', the 'Next' is not null because if it's empty, 'bufferedNodesHead.Next == bufferedNodesHead'. |
||
} | ||
while (currentNode != this.bufferedNodesHead); | ||
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. Was wondering how 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 checked the logics in 'BefferingJsonReader' and that's the way to check the end of link. |
||
} | ||
|
||
value = null; | ||
if (!found) | ||
{ | ||
return false; | ||
} | ||
|
||
BufferedNode valueNode = currentNode.Next; | ||
|
||
if (removeIfExist) | ||
{ | ||
currentNode.Previous.Next = valueNode.Next; | ||
valueNode.Next.Previous = currentNode.Previous; | ||
|
||
if (this.bufferedNodesHead == currentNode) | ||
{ | ||
this.bufferedNodesHead = valueNode.Next; | ||
} | ||
|
||
if (this.isBuffering) | ||
{ | ||
if (this.currentBufferedNode == currentNode || this.currentBufferedNode == valueNode) | ||
{ | ||
this.currentBufferedNode = this.bufferedNodesHead; | ||
} | ||
} | ||
} | ||
|
||
value = valueNode.Value; | ||
return true; | ||
} | ||
|
||
/// <summary> | ||
/// The type of the last node read. | ||
/// </summary> | ||
|
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.
This could have some pretty severe performance ramifications.
If a payload doesn't have the value that we try to get, we will read the entire payload into memory. For very large, or streamed, responses, this could overflow memory.
This kinda defeats the whole point of ordering, which is that we can process the response in a streaming fashion.
The previous implementation assumes that type, if present, is the first thing we read. So we read the first node and, if it's type, we store that, and if it is not type, we assume there is no type and continue reading the payload as normal.
I would have expected we simply expand that logic. That is, we read the first node. If it is one of our "metadata" nodes (context, removed, type, id), then we get the value and repeat for the next node. As soon as we find a node that is not one of our metadata nodes, we break and start reading the payload as normal.