Skip to content

Conversation

CatBraaain
Copy link

@CatBraaain CatBraaain commented Aug 13, 2025

Default values defined in OpenAPI should be handled by the server, and the client should not arbitrarily assign values. Therefore, this PR remove the default values from the Pydantic models.

For members with default values defined in OpenAPI, if no explicit value
is provided, they temporarily hold None and are omitted when sent.

The default value is the one that the server uses if the client does not supply the parameter value in the request.
openapi document

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@cbornet (2017/09) @tomplus (2018/10) @krjakbrjak (2023/02) @fa0311 (2023/10) @multani (2023/10)

@CatBraaain CatBraaain force-pushed the python-remove-default branch from dd261b2 to eb37b1a Compare August 13, 2025 12:30
@wing328
Copy link
Member

wing328 commented Aug 21, 2025

Default values defined in OpenAPI should be handled by the server, and the client should not arbitrarily assign values.

thanks for the PR

there are use cases in which users only generate the models so I think we need to keep the default values for those use cases

@CatBraaain CatBraaain changed the base branch from master to 8.0.x August 21, 2025 01:22
@CatBraaain CatBraaain changed the base branch from 8.0.x to master August 21, 2025 01:24
@CatBraaain
Copy link
Author

@wing328

Thank you for your feedback.
I understand the concern about users who generate models only. However, I believe the client generators should primarily serve the purpose of making API requests consistent with the OpenAPI specification.

Default values defined in OpenAPI are supposed to be handled by the server, not by the client. Having defaults on the client side risks introducing inconsistencies, especially when the server changes its defaults. Other generators, such as the TypeScript client, also do not assign default values, so removing them in Python would align the behavior across languages.

In addition, even in Python, primitive types currently get defaults, but arrays/maps do not. This means the "models only" use case is already not fully supported.

Since this change corrects the client behavior to match the OpenAPI specification, I consider it a fix rather than a new breaking change, which is why I have targeted the 7.x branch for this PR.
Please let me know if you think it should instead be submitted to the 8.0.x branch.

@wing328
Copy link
Member

wing328 commented Aug 21, 2025

what about adding a new rule (e.g. CLEAR_DEFAULT_VALUES) in openapi normalizer to remove default values from schemas? that should work for all generators if users prefer not to have a default value in the output

https://github.com/openapitools/openapi-generator/blob/master/docs/customization.md#openapi-normalizer

(fyi java client supports default value for array/map as well)

@CatBraaain
Copy link
Author

@wing328
Thank you for the suggestion. I agree that an optional mechanism to adjust default values could be useful.
However, I believe the standard behavior should follow the OpenAPI specification, meaning the client should not assign default values by default.

For users who want to utilize default values, we could consider providing a flag like IMPLEMENT_DEFAULT_VALUE.

@Mattias-Sehlstedt
Copy link
Contributor

I am not completely following the scenario where the one would want to retain them in the model for the case where you only generate the models? Is the purpose for documentation or what is it used for?

As the OP highlight it is incorrect for the client to send values based on the default. They should only send the default if they are sure that they want to explicitly send that (i.e., they want to retain that value even if the server rotates its default).

I understand keeping it in models as documentation. This so I as a developer can know that the server has a default value, and that that is then something that I should have to consider "are we fine with that value (and following any changes to the default value), or do we want to send something different?". But should this not rather exist as a documentation section in a generated model rather than the value that is sent by default?

@CatBraaain
Copy link
Author

Hi @wing328

Since OpenAPI Generator is meant to generate clients according to the OpenAPI specification, I believe this PR is reasonable and aligns with the spec.

I understand the concern that some users might want to use default values on the client side.
However, if there is demand for that in the future, we could consider adding a new CLI option, e.g., --implement-default-value, to support it.

For now, would it be possible to approve this PR and remove the default values from the client?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants