Skip to content

Python JSON serialization emits null for absent optional non-nullable JsonStructure fields #698

Open

Description

@clemensv

Reproducer

A JsonStructure object distinguishes an absent optional field from an explicit null:

{"type":"object","properties":{"id":{"type":"string"},"county_code":{"type":"string"}},"required":["id"]}

In the checked-in Python producer data package for clemensv/real-time-sources, feeders/usgs-nwis-wq, MonitoringSite.county_code is Optional[str]. Constructing the generated record with county_code=None, then calling to_serializer_dict() or to_json(), emits "county_code": null. The authoritative JsonStructure contract allows the field to be absent but rejects null. Several optional WQ fields have this mismatch.

Expected

JSON serialization must retain the JsonStructure absence/nullability distinction. An absent optional non-nullable field must be omitted, while explicit nullable fields may emit null. Converting optional fields through an Avro nullable union must not silently broaden the JSON wire contract.

Observed consequence

Actual producer output fails strict validation against the checked-in authoritative schema. A permissive MQTT test helper previously removed got NoneType failures, so that helper concealed the mismatch.

Transitional containment

The USGS migration preserves frozen contracts/generated artifacts and supplies handwritten typed subclasses at the transport boundary that omit absent optional fields. Strict wire validation remains enabled. No generated files were hand-edited. This report concerns the codegen/JsonStructure-to-Python pipeline; the underlying implementation may belong in avrotize if original optionality can be retained there.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    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