fix(python-fastapi): omit security imports for unauthenticated APIs
Merged
KA
2mo ago+52+52+01234567890123456789-7-7-0123456789
Fixes #23008.
Summary
The Python FastAPI API template unconditionally imports TokenModel, even when an API has no authenticated operations.
When only APIs and models are generated, supporting files such as extra_models.py are skipped. The resulting API therefore imports a module that was not generated:
from issue_23008_api.models.extra_models import TokenModel
This PR emits the TokenModel and security_api imports only when the API has security imports.
PR checklist
- Read the contribution guidelines.
- Run the following to build the project and update samples:
(For Windows users, please run the script in WSL)./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit
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. - 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)
Summary by cubic
Fixes the Python FastAPI template importing TokenModel and security_api for APIs with no authenticated operations, which previously referenced modules that were not generated.
- Emits
extra_models.TokenModeland security API imports only when the API has security imports. - Adds a regression test and a minimal OpenAPI spec that reproduces the unauthenticated case.