[python-fastapi] add useExternalImplementationPackage to reference an external absolue impl package - #24955
Merged
wing328 merged 1 commit intoSep 17, 2026
Conversation
… implementation package outside the generated one
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Since #20970 (7.13.0)
fastapiImplementationPackageis unconditionally prefixed withpackageName. That fixed the default value (#20063), but it changed the semantics of explicitly set values from absolute to relative without fallback: up to 7.12.0 the value was used as-is byapi.mustache(import <value>+pkgutilautoload), so the implementation could live in a sibling package that contains no generated code (e.g. generated code inapi.provide.gen.orders, hand-written controllers inapi.provide.impl.orders.controllers). With 7.13.0+ the sameconfiguration generates
import api.provide.gen.orders.api.provide.impl.orders.controllers.This PR adds an opt-in boolean,
useExternalImplementationPackage(defaultfalse, no change to the currentbehaviour). Whentrue:fastapiImplementationPackageis used as-is, both forimplPackageand for the template value read byapi.mustache(import {{fastapiImplementationPackage}}and thepkgutilautoload);impl/__init__.pysupporting file is not generated, since the package belongs to the user. This mirrors what other server generators do when the implementation is provided by the user (interfaceOnlyin spring,onlyInterfacesin go-server,skipStubsin scala-play-server).No auto-detection from the value on purpose: a dotted value such as
impl.v1is a legitimate relative sub-package today, so a heuristic would be a breaking change.Files:
CodegenConstants:USE_EXTERNAL_IMPLEMENTATION_PACKAGEand its description.PythonFastAPIServerCodegen: CLI option,processOpts(prefix only when the flag is false, single write-back of the fully qualified value), conditional__init__.py.PythonFastapiCodegenTest: newtestExternalImplementationPackage(import as-is, nonodesc.prefix, no__init__.pygenerated for the implementation); the existing test also asserts the generated import for the default layout.docs/generators/python-fastapi.md: option row.How to validate
No sample uses the option, so
./bin/generate-samples.shproduces no diff.PR checklist
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.
@cbornet (2017/09) @tomplus (2018/10) @krjakbrjak (2023/02) @fa0311 (2023/10)
Summary by cubic
Adds
useExternalImplementationPackage, an opt-in flag for the python-fastapi generator. Since 7.13.0,fastapiImplementationPackagegets unconditionally prefixed withpackageName, breaking absolute package paths. The flag restores the earlier behavior for users who need it.true, the package is used as-is and no__init__.pyis generated for it.falseso existing setups keep current behavior.Written for commit c87706f. Summary will update on new commits.