Json requestBody cannot be optional #1354
Description
Describe the bug
Generating an endpoint from an operation where requestBody has required: false (or required is unspecified) and an application/json content, still requires a body, despite it being optional.
Providing a None body with ...sync_detailed(body=None) will result in nil dereference in _kwargs["json"] = body.to_dict().
Providing an 'empty' body such as ...sync_detailed(body=RequestBody()) will result in an empty json object. i.e. {}, which is not desirable.
OpenAPI Spec File
openapi: 3.1.1
info:
title: ""
version: ""
paths:
/request:
post:
operationId: request
requestBody:
required: false
content:
application/json:
schema:
type: object
responses:
"201":
description: ""
Desktop (please complete the following information):
- OS: macOS 15.7.1
- Python version: 3.13.3
- openapi-python-client version: 0.25.1
Additional context
I've described the bug for application/json request bodies, but it may exist for other content types as well.
Similarly, I provided an example using openAPI spec version 3.1.1, but it might be relevant for other versions as well.
Activity
dbanty commentedon Nov 5, 2025
If the body is not required, that enables you to omit the parameter entirely, like
sync_detailed().Passing any value there will try to set that value as the body.
Nonebecomesnull, classes become objects, etc.The client also has a special
UNSETvalue it uses as a sentinel to determine when a value hasn't been passed. If you need to, you can import that fromtypes.pyand pass it like sync_detailed(body=UNSET).The preference, though, is to not pass a value at all if it's not required and you don't need it.
orelmaliach commentedon Nov 5, 2025
Not passing any value results of course in
TypeError: sync_detailed() missing 1 required positional argument: 'body'.And
Unsetfails whento_dict()is invoked on it in_get_kwargs.I generated a test client with above yaml. No custom config/templates, and result is same.
Am I missing something?!
dbanty commentedon Nov 6, 2025
Nope, you're right! The thing that works for every other type of input doesn't work for bodies 😱.
dbanty commentedon Nov 6, 2025
I whipped up a potential fix in #1357, please give it a try and let me know if it works!
Sorry for accidentally gaslighting you 😅
orelmaliach commentedon Nov 6, 2025
Your quick responses and resolution more than make up for it :)
A quick test from my side looks good for both
application/jsonandapplication/x-www-form-urlencodedcontent types!Release 0.28.0 (#1368)