Skip to content
This repository was archived by the owner on Mar 4, 2026. It is now read-only.

fix: Update the depth validation used when writing documents#1815

Merged
MarkDuckworth merged 3 commits intomainfrom
markduckworth/fix-object-depth-validation
Jan 25, 2023
Merged

fix: Update the depth validation used when writing documents#1815
MarkDuckworth merged 3 commits intomainfrom
markduckworth/fix-object-depth-validation

Conversation

@MarkDuckworth
Copy link
Copy Markdown
Contributor

@MarkDuckworth MarkDuckworth commented Jan 25, 2023

BEGIN_COMMIT_OVERRIDE
fix: Update the depth validation used when writing documents, so that it matches the validation of the Firestore backend.
END_COMMIT_OVERRIDE

… it matches the validation of the Firestore backend.
@MarkDuckworth MarkDuckworth requested review from a team January 25, 2023 16:14
@product-auto-label product-auto-label Bot added size: l Pull request size is large. api: firestore Issues related to the googleapis/nodejs-firestore API. labels Jan 25, 2023
@MarkDuckworth
Copy link
Copy Markdown
Contributor Author

The behavior in question: is path "foo.bar.baz" 3 levels deep or 2? Admin SDK says it's 3 but DBE says 2.

The answer is not important since the backend already has customer data that is "21" levels deep. This PR modifies the depth validation in this SDK to compute the depth the same way.

Comment thread dev/test/serializer.ts Outdated
Copy link
Copy Markdown
Contributor

@dconeybe dconeybe left a comment

Choose a reason for hiding this comment

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

One minor comment, but LGTM otherwise.

@product-auto-label product-auto-label Bot added size: m Pull request size is medium. and removed size: l Pull request size is large. labels Jan 25, 2023
Comment thread dev/src/serializer.ts
inArray?: boolean
): void {
if (path && path.size > MAX_DEPTH) {
if (path && path.size - 1 > MAX_DEPTH) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

this may be the simplest workaround for the issue but may not be the best fix.

IMO, L348 is the one that's problematic, it adds the path of a top-level map property to the accumulating path, creating the off-by-one error. "foo" in {"foo": {"bar": "baz"}} have depth 0 but this implementation currently calculates it as depth 1.

Currently, path serves two purposes:

  1. to be used in the fieldPathMessage (as part of error message)
  2. to be used for counting the depth

I think we should separate this responsibility out.

Separately, I think validateUserInput may be exposing too wide of an API. Currently, it can be used to validate an array property or validate a Firestore value as a whole. The ambiguity really comes from this. This also lead to incorrect usage, e.g., in

/*path=*/ undefined,
, the path should include the array index in order for the depth calculation to be correct.

I don't have a strong opinion on this though. This change is technically relaxing the requirement on the client side. If anything, we always have the backend side as fallback so the change as is may be fine.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback.

Regarding L348 being problematic. If a path of ['foo', 'bar'] is considered to have a depth of 1 and ['foo'] is considered to have a depth of 0, then yes we could remove the top level map property or we could compute the depth as path.size - 1. It's the same IMO and allows the re-use of path.

I agree with your statement "I think validateUserInput may be exposing too wide of an API", but in consideration of our other discussion of removing all of this validation. I'm going to avoid any refactor for now.

@MarkDuckworth MarkDuckworth merged commit 789d9eb into main Jan 25, 2023
@MarkDuckworth MarkDuckworth deleted the markduckworth/fix-object-depth-validation branch January 25, 2023 20:46
@MarkDuckworth MarkDuckworth added the release-please:force-run To run release-please label Feb 16, 2023
@release-please release-please Bot removed the release-please:force-run To run release-please label Feb 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

api: firestore Issues related to the googleapis/nodejs-firestore API. size: m Pull request size is medium.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants