Skip to content

Keep required nullable properties as pointers that write null - #166

Merged
cubahno merged 1 commit into
mainfrom
ig/fix-req-nullable-anyof
Oct 8, 2026
Merged

cubahno merged 1 commit into
mainfrom
ig/fix-req-nullable-anyof

Conversation

@cubahno

@cubahno cubahno commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

A required property declared as anyOf: [T, {type: "null"}], which Pydantic and FastAPI emit for T | None without a default, was generated as a plain T. Building the union saw the null option, but the object's property loop then recomputed nullability from the property's own type, which an anyOf doesn't have. So a null from the server decoded to the zero value and was sent back as 0001-01-01T00:00:00Z or "". Separately, every required nullable property got omitempty, so a nil value dropped a key the schema requires instead of writing null. That one already applied to type: [T, "null"] and nullable: true.

What changes

  • A property whose anyOf or oneOf lists {type: "null"} next to a single other type is nullable, so a required one becomes a pointer: UpdatedAt *time.Time.
  • A required property gets no omitempty, so a nil pointer encodes as null. That holds for type: [T, "null"] and nullable: true too, and for the MarshalJSON generated for types with additionalProperties or a union next to their properties.
  • readOnly and writeOnly properties still count as optional and keep omitempty. Optional properties, unions of two or more types besides null, and request parameters generate as before.
  • For custom templates: Constraints.RequiredKey says whether the object requires the property's key, and Property.OmitsNil whether a nil value leaves the key out.

Fixes #162

@cubahno
cubahno force-pushed the ig/fix-req-nullable-anyof branch from 94c7c94 to 80c9c5b Compare October 8, 2026 09:36
@cubahno
cubahno marked this pull request as ready for review October 8, 2026 09:42
@cubahno
cubahno merged commit 932ebd9 into main Oct 8, 2026
3 checks passed
@cubahno
cubahno deleted the ig/fix-req-nullable-anyof branch October 8, 2026 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Required property declared as anyOf: [T, {type: "null"}] is generated as a non-pointer

1 participant