Skip to content

fix(python): support default values for array/map field #21707

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

Closed

Conversation

CatBraaain
Copy link

What done

Setting mutable objects as default values is safe in Pydantic v2.
Therefore, this change updates the generator to support default values for array and map types
by treating them similarly to primitive types.

Rationale

A common source of bugs in Python is to use a mutable object as a default value for a function or method argument, as the same instance ends up being reused in each call.
In the event that the default value is not hashable, Pydantic will create a deep copy of the default value when creating each instance of the model.

from pydantic import BaseModel


class Model(BaseModel):
    item_counts: list[dict[str, int]] = [{}]


m1 = Model()
m1.item_counts[0]['a'] = 1
print(m1.item_counts)
#> [{'a': 1}]

m2 = Model()
print(m2.item_counts)
#> [{}]

Reference: https://docs.pydantic.dev/latest/concepts/fields/#mutable-default-values

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

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.

@wing328
Copy link
Member

wing328 commented Aug 7, 2025

thanks for the pr

can you please add some tests schemas to modules/openapi-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing.yaml and follow step 3 to update the samples so that the CI can verify the change?

@CatBraaain
Copy link
Author

@wing328
Thanks for the review.
I've added the test schema to modules/openapi-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing.yaml and followed step 3 to update the samples. Please let me know if anything else is needed.

@CatBraaain CatBraaain force-pushed the python-default-array-map branch from b47c2b8 to e95377a Compare August 7, 2025 08:58
@wing328
Copy link
Member

wing328 commented Aug 7, 2025

can you please review CI build errors when you've time?

let us know if you need any help

@CatBraaain CatBraaain force-pushed the python-default-array-map branch from e95377a to 09d8d0c Compare August 8, 2025 02:26
@CatBraaain
Copy link
Author

@wing328
Sorry for the fails many times.
I believe I've fixed it now.
Could you please take a look and confirm?

@wing328
Copy link
Member

wing328 commented Aug 8, 2025

triggered the workflow again but some tests still failed

please take a look when you've time

have a nice weekend

@CatBraaain
Copy link
Author

This PR was created to support default values for array and map types on the Python client side. However, after further investigation, I recognized that embedding default values on the client side is incorrect for the following reasons.

According to the OpenAPI specification, the default value is "the value the server uses if the client does not supply the parameter value in the request" (Swagger official documentation).

Additionally, the OpenAPI Generator TypeScript client does not set default values for model properties, leaving unset properties as undefined (example in Bird.ts). This approach allows the server-side default values to be applied properly.

For these reasons, I close this PR.

@CatBraaain CatBraaain closed this Aug 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants