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

Fix embedded document inheritance #56

Closed
wants to merge 6 commits into from
Closed

Fix embedded document inheritance #56

wants to merge 6 commits into from

Conversation

lafrech
Copy link
Collaborator

@lafrech lafrech commented Oct 6, 2016

I'm afraid there's an issue with embedded document inheritance when deserializing from DB: the _cls attribute is ignored. Looks like it only works when loading from outside world, not from DB.

This dirty fix seems to do the trick in my use case, but it is certainly not the right implementation. It is mostly a copy-paste from _deserialize to _deserialize_from_mongo. It should at least be factorized.

I'm not sure the instance should be created using to_use_cls(**value). It didn't work for me (complained about an unknown field name due to a dump_only field), so I randomly tried with build_from_mongo and it happened to work...

I think I had this case covered in my PR since I overrode build_from_mongo in EmbeddedDocumentImplementation to pick the right class. But I'm not sure this is relevant to your implementation. I basically copied inheritance related stuff from Document to EmbeddedDocument, while you addressed inheritance in EmbeddedField, AFAIU.

BTW, isn't there a way in EmbeddedField to get embedded_document_cls at init time (when declaring the field in the schema)?

Use _cls attribute when deserializing from DB
@coveralls
Copy link

coveralls commented Oct 6, 2016

Coverage Status

Coverage decreased (-0.3%) to 95.506% when pulling 6d72bc6 on Nobatek:dev_fix_embedded_doc_inheritance into 26ff4b8 on Scille:master.

@coveralls
Copy link

coveralls commented Oct 19, 2016

Coverage Status

Coverage decreased (-0.3%) to 95.466% when pulling a1861fd on Nobatek:dev_fix_embedded_doc_inheritance into 26ff4b8 on Scille:master.

@lafrech
Copy link
Collaborator Author

lafrech commented Oct 19, 2016

I'm still struggling with embedded document inheritance.

I don't understand why your implementation calls from_mongo stuff when deserializing from OO world. I'm not sure this is wanted, or a consequence of the use of super()._deserialize the line above. In fact, I don't understand those two lines.

When modifying them as I did (see new commit), I get both my use case and your tests to pass.

I realize this is a pretty crappy bug report... It's still a bit blurry to me and I was not able to isolate a broken use case I could provide to illustrate the issue I had.

If you're sure about your implementation, then there's definitely something I need to understand. Otherwise, I hope this will help. And I'm available to talk about it.

@coveralls
Copy link

coveralls commented Oct 26, 2016

Coverage Status

Coverage decreased (-0.08%) to 95.482% when pulling 7753be4 on Nobatek:dev_fix_embedded_doc_inheritance into c09cc8a on Scille:master.

@lafrech
Copy link
Collaborator Author

lafrech commented Oct 26, 2016

I just pushed an update to my workaround branch and I supposed you were notified, so I might as well comment on this.

It is just a rebase to latest master. It is still hackish. Just a workaround that seems to work for me.

Anyway the core issue (with failing test) is in #62.

Unless I'm mistaken, this is a major fail in the EmbeddedField inheritance feature.

@touilleMan
Copy link
Member

e5cd0bb should have fixed this, thx for the work anyway 👍

@touilleMan touilleMan closed this Oct 29, 2016
@lafrech
Copy link
Collaborator Author

lafrech commented Nov 2, 2016

I'm still having issues with this even with latest revision from master.

I think it comes from the call to self._deserialize_from_mongo in self._deserialize in EmbeddedField (here).

I really don't understand why we'd call _deserialize_from_mongo with data from the OO world.

The line before,

data, errors = self.schema.load(value, partial=True)

returns OO stuff. There can be Implementation classes in there (think embedded doc in embedded doc). Not just data.

Why not just call

TheRightClassSelectedUsingCls(**data)

(See updated branch. I can't reopen this PR and since it is closed, the new diff was not computed by GitHub.)

If I get the time and manage to do it, I'll propose a failing test.

@touilleMan
Copy link
Member

Yes, please provide a testcase ^^

@lafrech
Copy link
Collaborator Author

lafrech commented Dec 5, 2016

Reopening so that GitHub tracks my rebases and updates the diffs.

Did you get any time to look into the test case I pushed (#67)?

@lafrech lafrech reopened this Dec 5, 2016
@lafrech lafrech added the bug label Dec 5, 2016
…doc_inheritance

Conflicts:
	tests/test_fields.py
	umongo/fields.py
@coveralls
Copy link

coveralls commented Dec 5, 2016

Coverage Status

Coverage decreased (-0.05%) to 95.548% when pulling c0ee027 on Nobatek:dev_fix_embedded_doc_inheritance into 65c8e01 on Scille:master.

@coveralls
Copy link

coveralls commented Dec 5, 2016

Coverage Status

Coverage decreased (-0.05%) to 95.548% when pulling c0ee027 on Nobatek:dev_fix_embedded_doc_inheritance into 65c8e01 on Scille:master.

@coveralls
Copy link

coveralls commented Dec 5, 2016

Coverage Status

Coverage decreased (-0.05%) to 95.548% when pulling e34daf7 on Nobatek:dev_fix_embedded_doc_inheritance into 65c8e01 on Scille:master.

@lafrech
Copy link
Collaborator Author

lafrech commented Jan 2, 2017

Fixed in #84.

@lafrech lafrech closed this Jan 2, 2017
@lafrech lafrech deleted the dev_fix_embedded_doc_inheritance branch March 20, 2019 17:00
@lafrech lafrech restored the dev_fix_embedded_doc_inheritance branch March 20, 2019 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants