schemagen drops null elements from arrays with nullable item types #5

Closed
opened 2026-06-17 23:38:33 +00:00 by weaselbot · 0 comments
Member

For array types whose items are nullable ({"type": ["T", "null"]}), contrib/schemagen/weaseljson_schemagen.py emits std::vector<std::optional<T>> (or std::vector<std::unique_ptr<T>> for recursive objects). However, the generated cbNull callback in the _builder template (lines 967-975) simply calls valueComplete() for nullable slots and never appends an empty element when the current frame is an array.

Reproducer schema:

{
  "type": "object",
  "properties": {
    "items": {
      "type": "array",
      "items": { "type": ["string", "null"] }
    }
  }
}

Parsing {"items": [null, "a", null]} succeeds, but the resulting items->size() is 1 and contains only "a". The two null entries are silently dropped, so vector indices no longer correspond to JSON array indices.

The same problem affects arrays of nullable integers, booleans, numbers, enums, objects, and root-level nullable arrays. For nullable object items using std::unique_ptr, a null element should append a null pointer; for std::optional items, an empty optional should be appended.

Expected behavior: every null item contributes one element to the generated container.

For array types whose items are nullable (`{"type": ["T", "null"]}`), `contrib/schemagen/weaseljson_schemagen.py` emits `std::vector<std::optional<T>>` (or `std::vector<std::unique_ptr<T>>` for recursive objects). However, the generated `cbNull` callback in the `_builder` template (lines 967-975) simply calls `valueComplete()` for nullable slots and never appends an empty element when the current frame is an array. Reproducer schema: ```json { "type": "object", "properties": { "items": { "type": "array", "items": { "type": ["string", "null"] } } } } ``` Parsing `{"items": [null, "a", null]}` succeeds, but the resulting `items->size()` is 1 and contains only `"a"`. The two `null` entries are silently dropped, so vector indices no longer correspond to JSON array indices. The same problem affects arrays of nullable integers, booleans, numbers, enums, objects, and root-level nullable arrays. For nullable object items using `std::unique_ptr`, a `null` element should append a null pointer; for `std::optional` items, an empty optional should be appended. Expected behavior: every `null` item contributes one element to the generated container.
weaselbot was assigned by andrew 2026-06-18 14:05:36 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: weaselab/weaseljson#5