Skip to content

Orval does not support optional by default requestBody that OpenAPI 3.0 specifies #2028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Jon-Mallozzi opened this issue Apr 14, 2025 · 5 comments
Labels
bug Something isn't working workaround A workaround has been provided

Comments

@Jon-Mallozzi
Copy link

Hi, the fix for #1111 did not support the optional by default which is in the OpenAPI 3.0 spec [1] as originally requested. It only seems to handle the required: false value.

In my project, I have in the OpenApi Spec

"requestBody": {
          "content": {
            "application/json": { "schema": { "$ref": "#/components/schemas/BodyDto" } }
          }
        }

The backend expects no body to have certain behavior happen. When the backend generates the OpenApi spec, it knows it's optional with no required field, so it always generates nothing for required.

Generating with "orval": "7.7.0" doesn't create my axios request function with an optional body:

BodyDto: BodyDto,

but if I set required: false manually in the OpenApi spec the backend provides it does generate as optional:

BodyDto?: BodyDto,

The issue is whenever the backend generates the OpenApi spec for the frontend it will overwrite the require: false so I think proper optional body support is needed.

What are the steps to reproduce this issue?

  1. Do not have the required in the requestBody
  2. The schema object doesn't generate as optional

What happens?

The request body is not optional.

What were you expecting to happen?

The request body to be optional.

Any logs, error output, etc?

No

Any other comments?

No

What versions are you using?

Package Version: 7.7.0

@melloware melloware added bug Something isn't working good first issue Good for newcomers labels Apr 14, 2025
@AllieJonsson
Copy link
Contributor

I think the decision was made by anymaniax to not follow the OpenAPI specification in the linked pr

@melloware
Copy link
Collaborator

Agree with @AllieJonsson this is by design.

@melloware melloware added workaround A workaround has been provided and removed bug Something isn't working good first issue Good for newcomers labels Apr 15, 2025
@melloware
Copy link
Collaborator

The workaround is to explicitly add the required option in your OpenApi so it's explicit the intent.

@melloware melloware closed this as not planned Won't fix, can't repro, duplicate, stale Apr 15, 2025
@melloware melloware added the duplicate This issue or pull request already exists label Apr 15, 2025
@seschis
Copy link

seschis commented Apr 15, 2025

@melloware I think the decision to selectively follow the openapi specification is shortsighted will cause more issues like this. There are openapi json generators that follow the specification in this regard and do not output required: false when its not needed because that is the default in the specification.
Those tools don't play well with tools that decided to diverge from the specification, like orval in this instance, and causes a waste of developer time to track down the problem to something like this issue when it never had to be an issue in the first place.
what good is a specification like openapi if people are allowed to selective choose what behaviors they want to implement?

@melloware melloware reopened this Apr 15, 2025
@melloware
Copy link
Collaborator

@anymaniax thoughts?

@melloware melloware added bug Something isn't working and removed duplicate This issue or pull request already exists labels Apr 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working workaround A workaround has been provided
Projects
None yet
Development

No branches or pull requests

4 participants