Repository navigation
Compare entity and property models by identity (plan item 2.7) - #4396
Merged
Merged
Conversation
PropertyModel.hashCode included its lazily specialized codec, and EntityModel.hashCode hashed its property models, so both hashes changed the first time an entity was encoded, stranding them in hash-based collections. Both overrides are removed, so the models compare by identity, which is what every caller relies on.
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
Identity semantics fit the reviewed callers, regression coverage addresses hash stability, and no blocking issues remain.
0 open findings
What changed in this PR
Fixes unstable model hashes by using identity equality for entity and property models in Morphia’s mapping layer.
Changes:
- Removes structural
equalsandhashCodeoverrides from both model classes. - Adds hash-set stability coverage across encoding and decoding under both mappers.
- Marks plan item 2.7 complete.
| File | Description |
|---|---|
design/mapping-game-plan.md |
Marks item 2.7 complete. |
core/src/test/java/dev/morphia/test/mapping/codec/harness/CodecHarnessTest.java |
Tests model hash stability under both mappers. |
core/src/main/java/dev/morphia/mapping/codec/pojo/PropertyModel.java |
Switches property models to identity equality and hashing. |
core/src/main/java/dev/morphia/mapping/codec/pojo/EntityModel.java |
Switches entity models to identity equality and hashing. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Plan item 2.7 (review D5).
The bug
PropertyModel.hashCodeincludedcodec, which is set lazily the first time the property is encoded or decoded.EntityModel.hashCodehashed itspropertyModelsByNameandpropertyModelsByMappedNamemaps, which contain those property models. So both hashes changed after the first encode. Any model placed in aHashSetorHashMapkey before then could no longer be found. The hierarchy's subtype sets are hash-based, too. The deep structuralequalsalso cost a full comparison on everyCopyOnWriteArraySet.addand wherever models were compared.Change
The
equalsandhashCodeoverrides onEntityModelandPropertyModelare deleted, so both compare by identity. Each mapper holds exactly one model per type and one property model per property, and copied mappers build new instances. No caller compared two distinct instances structurally.Audit
propertyModel.equals(idModel)inEntityEncoder, subtype sets, andIndexHelper'sparentModels.contains, always compare instances from the same mapper.TestMapper.testMapperCopyingalready asserts that copies are different instances, usingassertNotSame.Tests
CodecHarnessTest.modelHashesAreStableAcrossEncodingruns under both mappers. It putsHarnessScalars' entity model and its property models intoHashSets, encodes and decodes a sample (which specializes every property's codec), and checks that the sets still contain them. It failed before the fix: "REFLECTION: the model's hash changed".The core and Kotlin suites pass with
-Dmorphia.mapper=critterand with reflection: 1482 + 5 tests, 0 failures.Decisions to review
None beyond the plan.
TypeDataandWildCardTypeDatakeep their value-basedequals/hashCode: they're immutable, and code compares them as values.