Skip to content

fix(wtsite): copy the jsonld context before rewriting it - #178

Open
LukasGold wants to merge 1 commit into
mainfrom
fix/177-context-mapping-copy
Open

LukasGold wants to merge 1 commit into
mainfrom
fix/177-context-mapping-copy

Conversation

@LukasGold

Copy link
Copy Markdown
Contributor

Fixes #177.

Changes

  • WtSite._replace_jsonld_context_mapping deep-copies a dict argument at
    entry and rewrites the copy. The caller's object is no longer modified.
  • The dict branch is no longer nested in an if. An argument of an
    unhandled type returns unchanged instead of falling through to an implicit
    None.
  • New unit tests in tests/test_wtsite_jsonld_context_mapping.py.

Rationale

  • The method returned a different kind of result per input type: the same
    string, a new list, the same mutated dict, or None. The new list was a
    shallow copy of the caller's dictionaries, which were rewritten in place, so
    it gave no protection.
  • deepcopy rather than dict(context): the replaced values are nested
    dictionaries such as {"@id": ..., "@type": "@id"} and scoped @context
    blocks. A shallow copy repeats the defect one level down.
  • The only caller in this repository, get_jsonld_context_loader, was safe by
    accident. WtPage.get_slot_content returns a fresh deepcopy on every call,
    so the loader rewrote a throwaway object. Nothing documented or tested that
    dependency.
  • Contexts are small (66 to 77 keys for the merged Task chain on a live
    instance), so the copy is not a performance concern.

Verification

  • 9 new tests pass.
  • 7 of the 9 fail against the previous src/osw/wtsite.py, confirming they
    guard the reported behaviour. The 2 that pass assert the rewriting rules,
    which are unchanged.

- _replace_jsonld_context_mapping deep-copies a dict argument at entry
- values of an unhandled type are returned unchanged instead of None
- add unit tests covering dicts, lists, scoped contexts and copy depth

Closes #177
@github-actions

Copy link
Copy Markdown
Contributor

Release preview

Merging this PR would release v2.3.4 (current: v2.3.3).

Changelog preview (truncated)
## v2.3.4 (2026-09-18)

### Bug Fixes

- **wtsite**: Copy the jsonld context before rewriting it
  ([`ea0cddf`](https://github.com/OpenSemanticLab/osw-python/commit/ea0cddfa85a0025560dda49f78ff99577a86f526))

### Chores

- Relicense to Apache-2.0 ([#173](https://github.com/OpenSemanticLab/osw-python/pull/173),
  [`848615c`](https://github.com/OpenSemanticLab/osw-python/commit/848615cbd1e06401922eda88231a51028440a242))

Preview via python-semantic-release and conventional commits.

@simontaurus
simontaurus self-requested a review September 18, 2026 09:46
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.

_replace_jsonld_context_mapping modifies the context object it is given

2 participants