Skip to content
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

Make GetPropertyCount public and fix its return value #106503

Merged
merged 7 commits into from
Aug 17, 2024

Conversation

etemi
Copy link
Contributor

@etemi etemi commented Aug 15, 2024

Should fix #104692

@WeihanLi was the first to submit a PR for this issue.
If there should be only one PR per issue feel free to close this as I created my PR later.

Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Aug 15, 2024
@WeihanLi
Copy link
Contributor

👍 thanks, I've closed my PR

Copy link
Member

@eiriktsarpalis eiriktsarpalis left a comment

Choose a reason for hiding this comment

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

The changes look good to me superficially. Paging @bartonjs who is probably more familiar with the code that is being changed.

@@ -68,7 +68,7 @@ public sealed partial class JsonDocument
// * 31 bits for token offset
// * Second int
// * Top bit is unassigned / always clear
// * 31 bits for the token length (always 1, effectively unassigned)
Copy link
Member

Choose a reason for hiding this comment

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

I think it might make sense to validate if this comment is consistent with the current implementation, just to make sure we're not accidentally regressing something.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FYI: For me it seemed that the comment was wrong because previously it wasn't always 1 for StartObject. Instead it was set to the same value as the third int (number of rows until the next value). See first and last line here:

database.SetLength(rowIndex, numberOfRowsForMembers);
int newRowIndex = database.Length;
database.Append(tokenType, tokenStart, reader.ValueSpan.Length);
database.SetNumberOfRows(rowIndex, numberOfRowsForMembers);

Copy link
Member

Choose a reason for hiding this comment

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

Looks like the comment was always wrong (at least, was wrong in the initial merge, maybe it was right when I wrote it during feature development).

Nothing noticed or cared that the value was wrong, because nothing read the value until DeepEquals was added... and I guess it didn't notice that it was wrong, because it was the "same wrong" when two things were equal.

Copy link
Member

@eiriktsarpalis eiriktsarpalis Aug 17, 2024

Choose a reason for hiding this comment

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

I'm trying to decide if this means that we should backport the change to 9. Is it possible for the original values (number of rows) to be equal when the number of properties are not equal? Even if that wouldn't impact functional correctness, it would prompt the comparer to recurse into a more expensive property-wise comparison for differently-sized objects.

Copy link
Contributor Author

@etemi etemi Aug 17, 2024

Choose a reason for hiding this comment

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

It is possible to have equal number of rows when the number of properties are not equal:

  • Example 1

    10 rows and 2 properties:

    { "a": [1,2,3,4], "b": 4 }

    10 rows but 3 properties:

    { "a": [1,2], "b": 3, "c": 4 }
  • Example 2

    7 rows and 1 property:

    { "a": [1,2,3] }

    7 rows and 3 properties:

    { "a": 1, "b": 2, "c": true }

But I would say it is very unlikely that two unequal values accidentally have the same number of rows.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, I think we should try to backport this.

if (prop.Value.ValueKind == JsonValueKind.Object)
{
CheckPropertyCountAgainstEnumerateObject(prop.Value);
}
Copy link
Member

Choose a reason for hiding this comment

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

Should there also be something like

if (prop.Value.ValueKind == JsonValueKind.Array) {
foreach item, check
}

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How did I miss that... I now also added checks for GetArrayLength (because the local variable is now reused/shared for object property count and array length here: ea276ea)

Also added checks for `GetArrayLength`
Copy link
Member

@eiriktsarpalis eiriktsarpalis left a comment

Choose a reason for hiding this comment

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

Thanks!

@eiriktsarpalis
Copy link
Member

/backport to release/9.0

Copy link
Contributor

Started backporting to release/9.0: https://github.com/dotnet/runtime/actions/runs/10434784757

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json community-contribution Indicates that the PR has been added by a community member new-api-needs-documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add JsonElement.GetPropertyCount()
4 participants