Skip to content

Don't mutate the shared schema in validate_object_dict() - #926

Merged
DavidMStraub merged 2 commits into
gramps-project:masterfrom
dsblank:fix/dont-mutate-shared-schema
Aug 11, 2026
Merged

Don't mutate the shared schema in validate_object_dict()#926
DavidMStraub merged 2 commits into
gramps-project:masterfrom
dsblank:fix/dont-mutate-shared-schema

Conversation

@dsblank

@dsblank dsblank commented Aug 8, 2026

Copy link
Copy Markdown
Member

Summary

  • validate_object_dict()'s Person.OTHER gender-max workaround patched the dict returned by Person.get_schema() in place.
  • I am exploring caching get_schema() per class and returning the same object to every caller (for 40% increase in XML import speed) so mutating the returned schema in place would corrupt it for every subsequent caller — or raise outright once the cached object is made read-only.
  • This copies the schema before patching it, so the function only ever touches its own local view.

Test plan

  • Added test_validate_object_dict_does_not_mutate_shared_schema, which mocks Person.get_schema() to return a shared dict and asserts it comes back unmodified after validation.
  • Verified the new test fails against the pre-fix code (the shared object visibly gets mutated) and passes with the fix.
  • pytest tests/test_util.py — 19 passed.
  • black, isort, mypy clean on both changed files.

🤖 Generated with Claude Code

The Person.OTHER gender-max workaround patched the dict returned by
get_schema() in place. Gramps core may return the same cached object
to every caller, so this could corrupt it for every other caller (or
raise, if the cached object is made read-only). Copy it first.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Prevents validate_object_dict() from mutating the JSON schema returned by Person.get_schema(), which is important if Gramps starts returning a shared/cached schema object across calls (e.g., for improved XML import performance).

Changes:

  • Add a regression test ensuring validate_object_dict() does not mutate a shared schema object returned by Person.get_schema().
  • Copy the schema before applying the Person.OTHER gender-maximum workaround.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/test_util.py Adds a regression test verifying shared get_schema() results remain unmodified after validation.
gramps_webapi/api/resources/util.py Avoids mutating the get_schema() result in-place by copying before applying the gender maximum workaround.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread gramps_webapi/api/resources/util.py
…atch

Gate the Person.OTHER gender-max schema patch on the object being
validated actually having gender == other, instead of copying the
schema for every Person validated. Avoids avoidable deepcopy overhead
in bulk/batch validation.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@DavidMStraub
DavidMStraub merged commit 250a721 into gramps-project:master Aug 11, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants