Skip to content

fix(core): resolve local root pointers of fetched schemas - #230

Open
LukasGold wants to merge 5 commits into
stagefrom
fix/flatten-manual-defs
Open

LukasGold wants to merge 5 commits into
stagefrom
fix/flatten-manual-defs

Conversation

@LukasGold

@LukasGold LukasGold commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Closes #229

Changes

  • Reverts the $defs/manual flattening of the first version of this PR (_flatten_manual_definitions and tests/test_fetch_schema_manual_defs.py). $defs/manual is not an OSW convention: hand-written content sits at the schema root, and $defs/generated is the only reserved section for template output.
  • src/osw/core.py: new helper _resolve_local_root_pointers. OSW._fetch_schema calls it for every fetched page with a jsonschema slot. JsonSchema: pages do not pass through it.
    • First on the $defs/generated section, if that section is a dict, with pointers looked up in the page schema. oold's merge_generated_definitions drops the $ref of the generated section when the root holds "$ref": "#/$defs/generated", because the root $ref wins the merge and is then removed.
    • Then on the result of merge_generated_definitions.
    • It resolves local JSON pointers (#/...) in the root $ref and in the root allOf.
    • It merges the pointer target into the root with merge_deep(target, root), so keys at the root win. This is the merge rule of resolveSchema in MwJson_util.js. Several allOf pointers are merged in list order.
    • Remote allOf entries stay in place. Chains through the root $ref or root allOf, including chains through $defs/generated, are resolved, and a cycle stops the resolution. $defs is kept, so deeper pointers stay valid.
    • An unresolvable pointer, for example below "$defs": null or to a missing target, stays unchanged and raises no error.
    • The merge order of $defs/generated does not change: root keys overwrite generated keys, and generated @context items come before root items.
  • tests/test_fetch_schema_local_root_pointers.py: 16 offline tests.
    • fetch_schema with a referenced page that has a local pointer in root allOf, a chain through $defs/generated from the root allOf and from the root $ref, a local root $ref, the convention shape, and a remote allOf entry next to a local one.
    • Merge order: a root key overrides a $defs/generated key, also partially inside one property, and generated @context items come first.
    • Unit tests of the helper.

Rationale

datamodel-code-generator copies the root of a referenced page into the referencing node when that node has a key beside its $ref that is not metadata-only, such as title* (for example title, description, $comment, examples and default do not count). A local pointer of the referenced page is then looked up in the wrong document. For a pointer in the root allOf, generation fails with A Parser can not resolve classes. For a pointer in the root $ref, generation does not fail, but the field gets the type Any. The failure does not depend on the name manual. Merging the pointer targets into the root before the file is written removes the local pointer.

Limits

  • Local pointers in root anyOf/oneOf and inside properties are not resolved, and still fail in the same way when such a key sits beside the $ref.
  • The kept $defs can produce an extra, unused model class.
  • "$defs": null still raises AttributeError in oold's merge_generated_definitions, which runs before the new helper.

Tests

  • With _resolve_local_root_pointers replaced by a function that returns its input, the 5 new fetch_schema tests that involve a local pointer fail: the allOf pointer, both chains, the root $ref (the field is typed Any) and the remote allOf case.
  • make check passes, and make test reports 1073 passed.

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Release preview

Merging this PR would release v2.10.0 (current: v2.9.1).

Changelog preview (truncated)
## v2.10.0 (2026-12-04)

### Bug Fixes

- Align the jsonschema regeneration with the form editor
  ([`cc89362`](https://github.com/OpenSemanticLab/osw-python/commit/cc8936279d4ebdfbca985b046aaa1221cb0fcddc))

- Botfilter for user sync
  ([`4d6f440`](https://github.com/OpenSemanticLab/osw-python/commit/4d6f4401c63a148c924bef89963b8f18bc8d4b0c))

- Follow properties and items when collecting category templates
  ([#220](https://github.com/OpenSemanticLab/osw-python/pull/220),
  [`55e500e`](https://github.com/OpenSemanticLab/osw-python/commit/55e500efb896f0a3d6bde98409b3a6f43a652bdc))

- Keep the stored jsonschema when store_entity regenerates it
  ([`9b76a8f`](https://github.com/OpenSemanticLab/osw-python/commit/9b76a8fbeb813b34c4881b54d329e9e837b08f95))

- Select category templates from the schema allOf like the wiki
  ([#220](https://github.com/OpenSemanticLab/osw-python/pull/220),
  [`9f606f4`](https://github.com/OpenSemanticLab/osw-python/commit/9f606f48fefd640cca48e6718f1a92eca118da6b))

- **core**: Flatten $defs/manual before schema code generation
  ([#229](https://github.com/OpenSemanticLab/osw-python/pull/229),
  [`51fa791`](https://github.com/OpenSemanticLab/osw-python/commit/51fa791e427e5e53a65ee6bd0f65530848436b66))

- **core**: Resolve local root pointers of fetched schemas
  ([#229](https://github.com/OpenSemanticLab/osw-python/pull/229),
  [`0dfc8d6`](https://github.com/OpenSemanticLab/osw-python/commit/0dfc8d6725c91b7fc8152ef1806073ed91299f61))

- **core**: Resolve pointers of $defs/generated before merging
  ([#229](https://github.com/OpenSemanticLab/osw-python/pull/229),
  [`4c3a056`](https://github.com/OpenSemanticLab/osw-python/commit/4c3a056c49a308b4aabe533a3d12dadf7668ca92))

- **express**: Connect to http and localhost wikis
  ([`99584f1`](https://github.com/OpenSemanticLab/osw-python/commit/99584f16d5720d83ba621a3ef4427a5b1f4744bd))

- **user-sync**: Actually remove pruned fields via empty relations and remove_empty
  ([`87cf85b`](https://github.com/OpenSemanticLab/osw-python/commit/87cf85b31cbfec6b80e2e30b92852c359586133c))

- **user-sync**: Align with stage and the tools docs
  ([`bc6b0d6`](https://github.com/OpenSemanticLab/osw-python/commit/bc6b0d642f5697ca8048b4be05fd26c9ad2162f6))

- **user-sync**: Count and redirect only stored items
  ([#224](https://github.com/OpenSemanticLab/osw-python/pull/224),
  [`0b8dc97`](https://github.com/OpenSemanticLab/osw-python/commit/0b8dc974e972e8adaa7a2bb03bd3ae4396589f40))

- **user-sync**: Drop python-dotenv, use default accounts.pwd.yaml
  ([`02570c8`](https://github.com/OpenSemanticLab/osw-python/commit/02570c8f88d6d27c58e1e595bc8bb13e90354fd4))

- **user-sync**: Prune a single-valued key stored with an empty value
  ([`ebda514`](https://github.com/OpenSemanticLab/osw-python/commit/ebda514c731718ab20a084b93288491b3b944627))

- **user-sync**: Prune by field type and decide from stored jsondata
  ([`9b36e4e`](https://github.com/OpenSemanticLab/osw-python/commit/9b36e4ef8417b8847ba304535a95c0eedaf628b9))

### Build System

- Require opensemantic>=0.2.5 from PyPI
  ([`3464469`](https://github.com/OpenSemanticLab/osw-python/commit/3464469d78de95f5b5acdca66d1b8b0e83c53cd6))

### Chores

- **user-sync**: Scaffold module, example and tracking
  ([`217b101`](https://github.com/OpenSemanticLab/osw-python/commit/217b1017b46295340bc79cee050a91c45c0d379d))

### Continuous Integration

- Run Main on pushes to stage and Integration on PRs into stage
  ([`1b55f02`](https://github.com/OpenSemanticLab/osw-python/commit/1b55f02c90cae646d690f83d6208c01f0e26e62b))

### Documentation

- State which schema references the template walk follows
  ([#220](https://github.com/OpenSemanticLab/osw-python/pull/220),
  [`799bc07`](https://github.com/OpenSemanticLab/osw-python/commit/799bc07952aac7e6ab579bc127c5cb3000710491))

- **user-sync**: Add user sync guide and finalize example
  ([`830d120`](https://github.com/OpenSemanticLab/osw-python/commit/830d12072f9acfcbbaa265e5b75e6db38ca0a0d1))

- **user-sync**: Document prune and opt-in extras

Preview via python-semantic-release and conventional commits.

@LukasGold LukasGold left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Manually reviewed the code. Looks fine to me

@LukasGold LukasGold self-assigned this Nov 18, 2026
@simontaurus

Copy link
Copy Markdown
Contributor

Current convention: anything hand-written sits at the schema root level, $defs/generated is the reserved location for template-generated content. stripRootMembersSupersededByGenerated in PageBot.js builds on exactly that split: it retires root copies of generated members and keeps the rest. Module:MwJson and the editor only know $defs.generated.

$defs/manual and hand_authored have no producer, no consumer and no instances today:

  • 0 of 1209 Category schemas in the published packages carry $defs/manual
  • CharacteristicType on wiki-dev has neither the flag nor the section
  • a code search over the OpenSemanticLab and OO-LD orgs finds neither term

The failure is also not specific to manual. Any local $defs pointer at the root of a referenced page breaks datamodel-code-generator when a key sits beside the $ref. Why not resolve local root pointers generically instead of reserving a second name? If the name stays, two cases need handling: a root $ref chain (merge_generated_definitions pops the root $ref after merging, so _flatten_manual_definitions sees no reference and $defs/manual is left dangling) and "$defs": null, which raises in schema.get("$defs", {}).get("manual").

- merge targets of local $ref and allOf pointers at the root into the root
- root keys win; $defs/generated merge order unchanged
- replaces the reverted $defs/manual flattening
@LukasGold LukasGold changed the title fix(core): flatten $defs/manual before schema code generation fix(core): resolve local root pointers of fetched schemas Dec 3, 2026
@LukasGold

Copy link
Copy Markdown
Contributor Author

@simontaurus I reworked the PR along your comment. The title and the description are updated.

Changes

  • Commit 8db8673 reverts the $defs/manual flattening. Hand-written content stays at the schema root, and $defs/generated stays the only reserved section.
  • Commit 0dfc8d6 resolves local pointers generically. _resolve_local_root_pointers runs in _fetch_schema after merge_generated_definitions. Commit 4c3a056 also runs it on the $defs/generated section before merge_generated_definitions. The helper merges the target of a local #/... pointer in the root $ref or root allOf into the root, and the keys at the root win. This is the merge rule of resolveSchema in MwJson_util.js. The merge order of $defs/generated does not change.
  • Your two cases:
    • A chain through $defs/generated is resolved, from the root allOf (test_chain_via_generated) and from the root $ref (test_root_ref_chain_via_generated). In the root $ref form, oold's merge_generated_definitions dropped the $ref of the generated section: the root $ref #/$defs/generated wins the merge and is then removed. Commit 4c3a056 therefore resolves the generated section first.
    • "$defs": null does not raise in the new helper. It still raises AttributeError earlier, in oold's merge_generated_definitions, which reads schema.get("$defs", {}).get("generated"). That needs a fix in oold.
  • Limit: local pointers in root anyOf/oneOf and inside properties are not resolved, and still fail with the same error when a key that is not metadata-only, such as title*, sits beside the $ref.

Why the CharacteristicType template stays unchanged

The template writes title* beside the $ref of an array item. We do not change the template for #229:

  • The failure does not depend on the template. When the referencing node has a key beside its $ref that is not metadata-only, such as title* (for example title, description, $comment, examples and default do not count), datamodel-code-generator fails for a local pointer in the root allOf, anyOf or oneOf of the referenced page, or inside its properties. A local pointer in the root $ref does not fail, but the field gets the type Any.
  • A template change would change the generated section of every category that uses the template. The fix in fetch_schema changes no stored schema.

Stripping of root members

As we understand the convention, a root definition overwrites the generated definition, also partially, for example only the title, format or range of one property. The readers behave this way: oold's merge_generated_definitions and Module:MwJson p.loadJson let the root win. stripRootMembersSupersededByGenerated in PageBot.js contradicts this. In definitions_section mode it deletes such a partial overwrite on the next save. OpenSemanticLab/mediawiki-extensions-OpenSemanticLab#60 proposes to remove the function and to migrate the root residues of earlier template runs case by case. The osw-python port strip_root_members_superseded_by_generated stays unchanged in this PR. It should be removed together with the JS function.

Migration and Module:MwJson

This branch has not been deployed

No deployments
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.

2 participants