schemagen generated Root alias and Kind enum can collide with user-defined names #21

Closed
opened 2026-06-21 13:22:43 +00:00 by weaselbot · 0 comments
Member

The generator emits fixed names (Root, Arr0/Arr1/..., Skip, RootScalar) without reserving them in unique_name. Valid schemas that define $defs entries whose camel-cased names happen to match those generated identifiers produce non-compilable headers.

Relevant generator code in contrib/schemagen/weaseljson_schemagen.py:

  • Lines 206-214 (unique_name): only avoids collisions among user-chosen object/enum names; it never reserves Root, Skip, RootScalar, or array-kind names.
  • Lines 564-570 (_root_alias): always emits using Root = ....
  • Lines 573-583 (_kind_enum): concatenates object names and ArrN/Skip/RootScalar kind names, allowing duplicates.

Reproduction 1 — Root alias collision:

{
  "type": "array",
  "items": { "$ref": "#/$defs/Root" },
  "$defs": {
    "Root": { "enum": ["a", "b"] }
  }
}

Generated:

enum class Root : int { a, b };
using Root = std::vector<Root>;

Compiler error:

conflicting declaration ‘using weasel_schema::Root = class std::vector<weasel_schema::Root>’
note: previous declaration as ‘enum class weasel_schema::Root’

Reproduction 2 — Kind enumerator collision:

{
  "type": "object",
  "properties": {
    "arr": { "type": "array", "items": { "type": "string" } },
    "obj": { "$ref": "#/$defs/Arr0" }
  },
  "$defs": {
    "Arr0": { "type": "object", "properties": {} }
  }
}

Generated:

enum class Kind : uint8_t { Root, Arr0, Arr0, Skip };

Compiler errors:

error: redeclaration of ‘Arr0’
error: duplicate case value

Impact: ordinary, valid JSON schemas (e.g., a $defs entry named "Root", "Skip", or "Arr0") fail at C++ compile time instead of producing a working parser.

The generator emits fixed names (`Root`, `Arr0`/`Arr1`/..., `Skip`, `RootScalar`) without reserving them in `unique_name`. Valid schemas that define `$defs` entries whose camel-cased names happen to match those generated identifiers produce non-compilable headers. Relevant generator code in `contrib/schemagen/weaseljson_schemagen.py`: - Lines 206-214 (`unique_name`): only avoids collisions among user-chosen object/enum names; it never reserves `Root`, `Skip`, `RootScalar`, or array-kind names. - Lines 564-570 (`_root_alias`): always emits `using Root = ...`. - Lines 573-583 (`_kind_enum`): concatenates object names and `ArrN`/`Skip`/`RootScalar` kind names, allowing duplicates. Reproduction 1 — `Root` alias collision: ```json { "type": "array", "items": { "$ref": "#/$defs/Root" }, "$defs": { "Root": { "enum": ["a", "b"] } } } ``` Generated: ```cpp enum class Root : int { a, b }; using Root = std::vector<Root>; ``` Compiler error: ``` conflicting declaration ‘using weasel_schema::Root = class std::vector<weasel_schema::Root>’ note: previous declaration as ‘enum class weasel_schema::Root’ ``` Reproduction 2 — `Kind` enumerator collision: ```json { "type": "object", "properties": { "arr": { "type": "array", "items": { "type": "string" } }, "obj": { "$ref": "#/$defs/Arr0" } }, "$defs": { "Arr0": { "type": "object", "properties": {} } } } ``` Generated: ```cpp enum class Kind : uint8_t { Root, Arr0, Arr0, Skip }; ``` Compiler errors: ``` error: redeclaration of ‘Arr0’ error: duplicate case value ``` Impact: ordinary, valid JSON schemas (e.g., a `$defs` entry named "Root", "Skip", or "Arr0") fail at C++ compile time instead of producing a working parser.
weaselbot was assigned by andrew 2026-06-23 14:58:29 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: weaselab/weaseljson#21