Skip to content

fetch_schema fails when a referenced schema reaches its content through $defs/manual #229

Todo

Description

@LukasGold

Related: #215, #220

Problem

OSW.fetch_schema fails in datamodel-code-generator when a schema references another page whose content sits in $defs/manual, and the referencing node has a key beside its $ref:

Exception: A Parser can not resolve classes:
  [class: Test.json# references: frozenset({'Test.json#/$defs/manual'})]

The pointer #/$defs/manual belongs to the referenced page, but the error shows it looked up in the referencing document.

Reproduced on stage (8a059eb, oold 2.3.0, datamodel-code-generator 0.51.0).

Background

The OSW to OO-LD migration adds a hand_authored flag to CharacteristicType (Category:OSWffe74f291d354037b318c422591c5023). A hand-authored Characteristic keeps its hand-written schema in $defs/manual. The generated section points to it, so the stored jsonschema has this shape (abridged):

{
  "allOf": [{ "$ref": "#/$defs/generated" }],
  "title": "WikiFile",
  "$defs": {
    "generated": { "allOf": [{ "$ref": "#/$defs/manual" }], "uuid": "...", "title": "GeneratedWikiFile" },
    "manual": { "anyOf": ["..."] }
  }
}

The CharacteristicType schema template writes the title* of an array property into items, beside the $ref, so almost every reference to such a Characteristic has a key beside its $ref:

"data_sheet": {
  "type": "array",
  "items": {
    "title": "data_sheet",
    "title*": { "en": "Data sheet", "de": "Datenblatt" },
    "$ref": "/wiki/Category:OSWecdd611a465343d6b7b7030d4486218c?action=raw&slot=jsonschema"
  }
}

Minimal reproduction

Two pages, given to fetch_schema as offline_pages:

// Category:Item
{ "allOf": [{ "$ref": "#/$defs/manual" }],
  "$defs": { "manual": { "type": "string" } } }

// Category:Test
{ "type": "object", "title": "Test",
  "properties": { "files": { "type": "array",
    "items": { "title*": { "en": "x" },
               "$ref": "/wiki/Category:Item?action=raw&slot=jsonschema" } } } }
Item reaches its content through Keys beside $ref in Test Result
$defs/manual none ok
$defs/manual title ok
$defs/manual title*, description* or an invented key fails
$defs/x title* fails
$defs/generated title* ok
the root, no $defs title* ok

$defs/generated works because fetch_schema merges it into the root with merge_generated_definitions (oold) before code generation. No other $defs entry is merged.

Proposed fix

After merge_generated_definitions, also merge $defs/manual into the root, with the same semantics: remove every root reference to #/$defs/manual (root $ref and allOf entries), merge the content so that existing root keys win, and remove $defs/manual. An unreferenced $defs/manual stays unchanged. Pointers below #/$defs/manual/... are not rewritten, as for generated.

Activity

  1. self-assigned this
    on Nov 28, 2026
  2. LukasGold commented on Dec 3, 2026

    @LukasGold
    ContributorAuthor

    #230 now fixes this generically instead of flattening $defs/manual.

    • The cause does not depend on the name manual. 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* (title, description, $comment, examples, default and x- keys do not count). A local pointer of the referenced page, such as #/$defs/other, is then looked up in the referencing document. The minimal reproduction above fails in the same way with #/$defs/other.
    • The fix resolves local pointers in the root $ref and the root allOf of every fetched page with a jsonschema slot. It merges the target into the root, and the keys at the root win. A local pointer in the root $ref did not fail before, but the field got the type Any.
    • Local pointers in root anyOf/oneOf and inside properties are not resolved and still fail in the same way.
    • $defs/manual is not an OSW convention. Hand-written content sits at the schema root, and $defs/generated is the only reserved section. The OO-LD migration no longer writes $defs/manual.
    • The CharacteristicType template, which writes title* beside the $ref of an array item, stays unchanged. The failure occurs for local pointers in the root allOf, anyOf or oneOf of a referenced page and inside its properties, independent of the template, and a template change would change the generated section of every category that uses the template.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions