fix: preserve omitted and null JSON request bodies - #1488
Open
CodingCossack wants to merge 2 commits into
Open
CodingCossack wants to merge 2 commits into
CodingCossack wants to merge 2 commits into
Conversation
An optional nullable primitive request body rendered an orphan `else:` in the generated endpoint, so the client did not compile. The JSON body macro now uses the normal property transform, drops the `json` kwarg when the serialised value is UNSET, and sends explicit None as raw `content=b"null"` because HTTPX treats `json=None` as no body. Signature requiredness, nested serialisation and declared Content-Type are unchanged. Add generated-client regressions for OpenAPI 3.0 and 3.1 nullable bodies that assert request bytes through HTTPX for omitted, UNSET, None and concrete values, and regenerate the affected endpoint golden records. Fixes openapi-generators#1425 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1425
Problem
An optional nullable primitive request body, for example
{type: number, nullable: true}withoutrequired: true, generated an endpoint containing an orphanelse:, so the generated client did not compile. A syntax-only repair would still be wrong: HTTPX treatsjson=Noneas "no body", so an explicitNoneand an omitted argument would both send an empty body.Change
Only
openapi_python_client/templates/endpoint_macros.py.jinjachanges in production code. The JSON body macro now:skip_unset=False), which avoids the broken union branch;jsonkwarg when the serialised value isUNSET;jsonkwarg withcontent=b"null"when the value isNone.Presence and nullability stay independent:
UNSETNonenull0,False,""Signature requiredness, nested model serialisation, other body types and the declared
Content-Typeare unchanged. The 17 golden-record diffs are this boundary logic only.Tests
New functional tests generate clients from inline OpenAPI 3.0 and 3.1 specs and assert the actual request bytes through
httpx.MockTransport, sync and async, for nullable primitives, type lists,anyOf/oneOf, required/optional combinations, date-time, model, array and nested nullable fields, a custom JSON media type, and a query parameter namedjson_bodywith strict mypy on the generated package. The file fails onmain(generated package does not compile) and passes with this change.pdm run checkpasses locally: 480 tests, five snapshots, Ruff and mypy with HTTPX 0.28.1. All 12 new regression tests also pass with installed HTTPX 0.23.1 and 0.27.2. Structured object/array assertions compare decoded JSON, so valid differences in encoder whitespace do not fail the tests; omission, explicit null and primitive values retain exact-byte assertions. An independent review also generated clients from a further set of body shapes (required nullable model, nullable list/date/uuid,Any, single-member unions, form and multipart) and found no regressions.