Empty body sent to POST that expects JSON, if all parameters are omitted #7178
Labels
feature/openapi-parsing
The parsing logic for OpenAPI
product/sdk-generator
Fern's SDK Generator that outputs client libraries in 7 languages
Which Fern component?
SDK Generator
How urgent is this?
P0 - Critical (Blocking work)
What's the issue?
Summary: When you define a POST endpoint that accepts a JSON body, and all its fields are optional, fern-generated SDK sends empty body to the endpoint, instead of sending the expected
{}
.Details:
We define an OpenAPI schema with a POST endpoint that accepts a JSON object with all fields optional. Something like:
Note, in particular, that none of the fields is marked as required, so they're all optional. The generated Python API respects that, and creates a function such as:
Calling the function works as expected as long as I pass at least one parameter. But, if I call it without parameters, the backend complains of EOF while parsing JSON in request body. Adding further debugging to the backend shows that, indeed, an empty (zero-bytes) body is being sent, which is not valid JSON.
That's a bit surprising, because the generated code certainly looks like it's doing the right thing:
However,
HttpClient.request
generates JSON body by calling:and
get_request_body
does this:The last line is causing the issue. "Just sending None" means sending empty POST body instead of the expected
{}
. An empty document isn't even JSON, and this will cause problems on any POST endpoint that parses JSON with a JSON parser. For example, in Python:Could this behavior be changed? Or, if it is needed to make something else work, could it be made optional?
Fern CLI & Generator Versions
Fern CLI version: 0.57.17
The text was updated successfully, but these errors were encountered: