Skip to content

[BUG] [typescript-fetch] refuses to invoke if body is nullable and required #23493

@tomsontom

Description

@tomsontom

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

If the body parameter is required: true and nullable: true the generated code throws an error if one passes null

openapi-generator version

7.22-SNAPSHOT

OpenAPI declaration file content or url
{
  "openapi": "3.0.1",
	"info": {
		"title": "RSD Test",
		"version": "1.0.0"
  },
  "paths": {
	"/sample": {
      "post": {
        "tags": [
          "BodyParameterTypes"
        ],
        "description": "",
        "parameters": [],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "boolean",
                "nullable": true
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                    "type": "boolean"
                }
              }
            }
          }
        }
      }
    } 
  }
}
Generation Details
docker run --rm \ 
-v $PWD:/local openapitools/openapi-generator-cli:latest generate \
-i /local/openapi.json \
-g typescript-fetch --additional-properties=importFileExtension=.js \
-o /local/src
Steps to reproduce
  • run the generator
  • inspect the generated source / try to run it and invoke the method with null and you get a RequiredError thrown

If you inspect the source code you see

async samplePostRequestOpts(requestParameters: SamplePostRequest): Promise<runtime.RequestOpts> {
        if (requestParameters['body'] == null) {
            throw new runtime.RequiredError(
                'body',
                'Required parameter "body" was null or undefined when calling samplePost().'
            );
        }
...

which is obviously wrong as body can be null according to the spec above.

Related issues/PRs

Not that I'm aware of

Suggest a fix

The generator needs to check if the value in body can be null and then although it is required omit generating the above code.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions