-
Notifications
You must be signed in to change notification settings - Fork 409
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 invalid validation for _id
field
#3523
Conversation
Codecov Report
@@ Coverage Diff @@
## main #3523 +/- ##
==========================================
- Coverage 61.12% 61.05% -0.07%
==========================================
Files 426 426
Lines 27185 27187 +2
==========================================
- Hits 16616 16600 -16
- Misses 9573 9594 +21
+ Partials 996 993 -3
... and 18 files with indirect coverage changes
Flags with carried forward coverage won't be shown. Click here to find out more. |
I'm not so sure that this is a better solution, I have a couple of thoughts: Instead of creating every time new boolean variable func (d *Document) validateTopLevelData() error { return d.validateData(true) }
func (d *Document) validateInnerData() error { return d.validateData(false) } and inside ...
case *Document:
err := value.validateInnerData()
... But this approach add complexity and not clarity May be the better solution(instead of passing stave over recursion functions), is add in the @AlekSi what do you think? |
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.
I think the current approach is simple and good, it just needs a small clean-up. There is no need to shadow and redefine that variable
Agree, my first attempt was like you suggested 😄 |
_id
field
Thanks for contributing! It would also be nice to add integration tests for this case, so we ensure that the behavior is compatible with MongoDB. I think var DocumentsDocuments = &Values[primitive.ObjectID]{
name: "DocumentsDocuments",
data: map[primitive.ObjectID]any{
{0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}: bson.D{{"foo", int32(42)}},
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}: bson.D{{"bar", bson.D{}}},
{0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02}: bson.D{{"_id", bson.A{int32(42), int32(42)}}},
{0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03}: bson.D{{"_id", bson.D{{"foo", "bar"}}}},
{0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04}: bson.D{{"_id", primitive.Regex{Pattern: "foo", Options: "i"}}},
},
} As these documents are inserted when the integration tests are run, there will be a check that the insertion was successful. Alternatively, please feel free to suggest some other tests. |
_id
field_id
field
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.
Also, please feel free to ping me if you need any help.
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.
Hi @rumyantseva! thank you for your examples, I slightly adjust the last example about regex field. |
@rumyantseva can you please help me investigate one test
|
It is not you – it's us :) Just ignore it. We plan to fix that soon |
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.
Thank you for contributing!
The changes look good to me (I manually tested the cases with valid/invalid top-level _id
fields as we don't have integration tests for it).
@@ -115,7 +120,7 @@ func (d *Document) ValidateData() error { | |||
return err | |||
} | |||
case *Array: | |||
if key == "_id" { | |||
if isTopLevel && key == "_id" { |
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.
@slavabobik codecov still marks some lines uncovered, it's because we don't have tests where we try to insert documents with top-level _id
set as array or regex. A good candidate for such tests could be TestInsertCommandErrors
, we can add examples where top-level _id
field is filled with those invalid types and we know which error we expect. Personally, I would add such tests, but in this PR I leave it up to you to do it (if you have more time and wish, please feel free to do it, otherwise we can do it separately).
What else we don't have is a test case where top-level _id
is set as a document (a valid case, but probably rate in real life), but it's rather a separate story :)
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.
@rumyantseva I can add more tests in separate PR :) Do I need a separate task or just create a PR with tests?
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.
Let‘s have a separate PR only. A separate task is not needed. Thank you!
Description
Closes #2804.
Readiness checklist
task all
, and it passed.@FerretDB/core
), Milestone (Next
), Labels, Project and project's Sprint fields.