Skip to content

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

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

LukasGold wants to merge 4 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 on the result of merge_generated_definitions, for every fetched page with a jsonschema slot. JsonSchema: pages do not pass through either function.
    • 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 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: 15 offline tests.
    • fetch_schema with a referenced page that has a local pointer in root allOf, a chain through $defs/generated, 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, such as title*. A local pointer of the referenced page is then looked up in the wrong document, and generation fails with A Parser can not resolve classes. 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.
  • 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

  • Without the helper call, 4 of the 15 new tests fail: the allOf pointer, the chain, the root $ref (the field is typed Any) and the remote allOf case.
  • make check passes, and make test reports 1072 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-03)

### 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))

- **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
  ([`1899d7f`](https://github.com/OpenSemanticLab/osw-python/commit/1899d7f2c070a3bee614602509109bc97ea19f79))

### Features

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

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