Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Consume comments changes#35934

Merged
ahsonkhan merged 8 commits into
dotnet:masterfrom
WinCPP:extcom
May 17, 2019
Merged

Consume comments changes#35934
ahsonkhan merged 8 commits into
dotnet:masterfrom
WinCPP:extcom

Conversation

@WinCPP
Copy link
Copy Markdown

@WinCPP WinCPP commented Mar 11, 2019

Changes to ValueSpan / ValueSequence to exclude comment delimiters as per point 2 below,


Would it make sense to strip the comment delimiters (\\, /* and */) and the line separators (\r, \r\n and \n) during parsing where the ValueSpan or ValueSequence for the JsonTokenType.Comment is prepared?

Yes, it would.

In that case, should I do the changes in PR #33393 or should I do them in this PR?

Let's stage it in smaller, clearer chunks, where the change is isolated. So, in this order:

  1. Utf8JsonReader.cs - Add support for single line comments ending on \r, \r\n #33393 - Add support for single line comments ending on \r, \r\n
  2. Open up a new PR - Change ValueSpan/ValueSequence to exclude the comment delimiters, effectively undoing parts of 4a2aa63 from Add JsonUtf8Reader (for ReadOnlySpan) along with unit tests #33216
  3. This PR (Implement GetComment API #35705) to expose new GetComment API

What do you think of this approach?

Query: IIUC this line in JsonReaderHelper.CountNewLines needs to handle \r on its own as a line separator, and also ensure that \r\n are handled properly... ?

Unless this is blocking some of the work above, I would file a new issue for this and investigate/fix separately. Some questions we likely want to resolve first: What does Json.NET return for line number when comments end with just \r (for Windows vs Unix)? Do we double count line number \r\n or treat it as a single line increment?

cc @JamesNK

Originally posted by @ahsonkhan in #35705

@WinCPP WinCPP changed the title WIP: Consume comments changes Consume comments changes Mar 11, 2019
@ahsonkhan ahsonkhan added this to the 3.0 milestone Mar 11, 2019
Comment thread src/System.Text.Json/tests/Utf8JsonReaderTests.MultiSegment.cs Outdated
Comment thread src/System.Text.Json/tests/Utf8JsonReaderTests.MultiSegment.cs Outdated
Comment thread src/System.Text.Json/tests/Utf8JsonReaderTests.MultiSegment.cs
Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs Outdated
Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs Outdated
Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs
Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs
Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs Outdated
Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs
Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs
@WinCPP
Copy link
Copy Markdown
Author

WinCPP commented Mar 16, 2019

@ahsonkhan I have checked in the fixes for review comments. Please check when you get a moment. Thanks!

@WinCPP
Copy link
Copy Markdown
Author

WinCPP commented Mar 28, 2019

@ahsonkhan appreciate your review comments on this one! Thanks!

Copy link
Copy Markdown

@ahsonkhan ahsonkhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some feedback and questions. Apologies for the delayed review.

Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs Outdated
Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs Outdated
Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs Outdated
Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs
Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs Outdated
Comment thread src/System.Text.Json/tests/Utf8JsonReaderTests.MultiSegment.cs
Comment thread src/System.Text.Json/tests/Utf8JsonReaderTests.MultiSegment.cs Outdated
@WinCPP WinCPP force-pushed the extcom branch 2 times, most recently from b2bdff6 to aa6b47f Compare April 8, 2019 23:18
@WinCPP
Copy link
Copy Markdown
Author

WinCPP commented Apr 11, 2019

@ahsonkhan I have uploaded the entirely reworked logic. Please review referencing the original logic before this PR.

@WinCPP
Copy link
Copy Markdown
Author

WinCPP commented Apr 18, 2019

@ahsonkhan the code is ready for review with all the changes. Could you please review it. It also has fix for the existing issue that I mentioned in here.

Copy link
Copy Markdown

@ahsonkhan ahsonkhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more feedback/questions. Apologies for the late response.

Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs Outdated
Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs Outdated
Comment thread src/System.Text.Json/tests/Utf8JsonReaderTests.MultiSegment.cs Outdated
Comment thread src/System.Text.Json/tests/Utf8JsonReaderTests.MultiSegment.cs Outdated
Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs Outdated
Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs Outdated
@WinCPP
Copy link
Copy Markdown
Author

WinCPP commented May 4, 2019

@ahsonkhan I have fixed the latest review comment as well and resolved the code conflict. Please check.

Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs Outdated
Copy link
Copy Markdown

@ahsonkhan ahsonkhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise, LGTM.

@ahsonkhan ahsonkhan requested a review from bartonjs May 6, 2019 03:33
Comment thread src/System.Text.Json/src/System/Text/Json/ThrowHelper.cs Outdated
Comment thread src/System.Text.Json/tests/Utf8JsonReaderTests.MultiSegment.cs Outdated
@ahsonkhan
Copy link
Copy Markdown

I have fixed the latest review comment as well and resolved the code conflict. Please check.

@WinCPP, thanks for your patience and effort. Much appreciated.

We are almost at the finish line :)

I just have a nit and a leftover question around consistency when throwing particular exception messages.

@WinCPP
Copy link
Copy Markdown
Author

WinCPP commented May 16, 2019 via email

Copy link
Copy Markdown

@ahsonkhan ahsonkhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning "end of data" exception message for incorrect character within a comment is not correct.

Comment thread src/System.Text.Json/src/Resources/Strings.resx Outdated
Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs Outdated
Comment thread src/System.Text.Json/src/System/Text/Json/ThrowHelper.cs Outdated
Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs Outdated
else
{
ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.UnexpectedEndOfDataWithinStartOfComment);
ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.UnexpectedEndOfDataWhileReadingComment);
Copy link
Copy Markdown

@ahsonkhan ahsonkhan May 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use InvalidCharacterAtStartOfComment here. UnexpectedEndOfDataWhileReadingComment doesn't make sense for this case.

OR, better yet: since this is "Skip" and not "Consume", keep ExpectedStartOfValueNotFound here.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Skip in Multi-segment does the right thing. Single-segment should do the same:

ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedStartOfValueNotFound, JsonConstants.Slash);

ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedStartOfValueNotFound, JsonConstants.Slash);

ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedStartOfValueNotFound, marker);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value sent is JsonConstants.Slash but that could be an * as well, which we don't know yet because it is not there. Essentially, it could be / or * and we don't have placeholders for two characters in that exception. This is where all my thought process to have all those separate exception messages started.

So was the original exception that just emitted a / correct?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At line 2179 and 2186, the buffer length is already zero. We don't know the intention yet whether it was to follow with a / or * for single / multi line comment respectively. That is why I considered ExpectedStartOfComment as a wrong message. Because it expects a / (only). Why not *? We can't say anything if there is nothing!

At 2206, I did get confused because the exception resource name says ExpectedStartOfValueNotFound where as the actual message is <value> is invalid start of a value.. Perhaps, the exception resource name should be UnexpectedStartOfValue to align with the contents of exception message.

Copy link
Copy Markdown

@ahsonkhan ahsonkhan May 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the confusion. Maybe I can try to help explain my perspective.

The value sent is JsonConstants.Slash but that could be an * as well, which we don't know yet because it is not there.

The reason why value sent is JsonConstants.Slash is because before calling Skip(), we already saw a slash (that's what got us into the Skip method). See:

if (marker == JsonConstants.Slash)
{
if (SkipComment())

It doesn't matter if * follows next or /. Skip mode tries to ignore comments all together. So, the exception user sees is, "we saw / and then either we hit end of data or something invalid". / is not a valid start of value which is what we would expect (think of skip as if comments don't exist at all in JSON). That's why we have the ExpectedStartOfValueNotFound message.

That is why I considered ExpectedStartOfComment as a wrong message.

For "Skip" mode, I agree, we shouldn't return ExpectedStartOfComment. I am suggesting we return ExpectedStartOfValueNotFound. I don't think it makes sense for the user to see a comment related message when the user said "skip" them.

Let's say you have invalid JSON like this: {"property": /}. I expect an exception saying / is invalid start of a value. We should throw that regardless of whether the user was in "skip" or "disallow".

So was the original exception that just emitted a / correct?

In my opinion, yes.

Perhaps, the exception resource name should be UnexpectedStartOfValue to align with the contents of exception message.

Maybe, but I don't feel strongly about it. If you think it makes it clearer, I would be ok with renaming it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahsonkhan I get the idea now. Thank you for the clarification. Didn't realize the subtlety in two usages of one exception message.

I have submitted the changes. Hope I have finally got them as wanted. Thanks for patience.

@ahsonkhan
Copy link
Copy Markdown

ahsonkhan commented May 17, 2019

@WinCPP, here are all the cases we need to be consistent on.

Behavior of single-segment Skip/Consume should match the behavior of multi-segment Skip/Consume.

For invalid or incomplete data, Skip should throw ExpectedStartOfValueNotFound in all cases.

Consume should throw UnexpectedEndOfDataWhileReadingComment anywhere we hit end of data while reading. It should throw InvalidCharacterAtStartOfComment for invalid data.

To summarize:

  1. Single-segment
    a) Skip

    • End of Data - ExpectedStartOfValueNotFound
      • <value>'{0}' is an invalid start of a value.</value>
    • Invalid - ExpectedStartOfValueNotFound
      • <value>'{0}' is an invalid start of a value.</value>

    b) Consume

    • End of Data - UnexpectedEndOfDataWhileReadingComment
      • <value>Unexpected end of data while reading a comment.</value>
    • Invalid - InvalidCharacterAtStartOfComment
      • <value>'{0}' is invalid after '/' at the beginning of the comment. Expected either '/' or '*'.</value>
  2. Multi-segment
    a) Skip

    • End of Data - ExpectedStartOfValueNotFound
      • <value>'{0}' is an invalid start of a value.</value>
    • Invalid - ExpectedStartOfValueNotFound
      • <value>'{0}' is an invalid start of a value.</value>

    b) Consume

    • End of Data - UnexpectedEndOfDataWhileReadingComment
      • <value>Unexpected end of data while reading a comment.</value>
    • Invalid - InvalidCharacterAtStartOfComment
      • <value>'{0}' is invalid after '/' at the beginning of the comment. Expected either '/' or '*'.</value>

Comment thread src/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs Outdated
@ahsonkhan ahsonkhan merged commit d72d720 into dotnet:master May 17, 2019
@ahsonkhan
Copy link
Copy Markdown

Let's stage it in smaller, clearer chunks, where the change is isolated

  1. This PR (Implement GetComment API #35705) to expose new GetComment API

@WinCPP - thanks for the fix. Looks like the next step is #35705.

@WinCPP WinCPP deleted the extcom branch September 9, 2019 03:38
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
* Consume comments changes

* Fixed issues in branch splitting

* Use sequence positions. Elaborate comments.

* Reworked algos, review comment fixes

* Fixed review comments

* minor comment fix

* Standardize exception messages

* Clean up exception messages


Commit migrated from dotnet/corefx@d72d720
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants