Skip to content

[python-fastapi] add useExternalImplementationPackage to reference an external absolue impl package - #24955

Merged
wing328 merged 1 commit into
OpenAPITools:masterfrom
LeComptoirDesPharmacies:bugfix/24954
Sep 17, 2026
Merged

wing328 merged 1 commit into
OpenAPITools:masterfrom
LeComptoirDesPharmacies:bugfix/24954

Conversation

@AntoineDuComptoirDesPharmacies

@AntoineDuComptoirDesPharmacies AntoineDuComptoirDesPharmacies commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Since #20970 (7.13.0) fastapiImplementationPackage is unconditionally prefixed with packageName. 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 by api.mustache (import <value> + pkgutil autoload), so the implementation could live in a sibling package that contains no generated code (e.g. generated code in api.provide.gen.orders, hand-written controllers in api.provide.impl.orders.controllers). With 7.13.0+ the same
configuration generates import api.provide.gen.orders.api.provide.impl.orders.controllers.

This PR adds an opt-in boolean, useExternalImplementationPackage (default false, no change to the currentbehaviour). When true:

  • fastapiImplementationPackage is used as-is, both for implPackage and for the template value read by api.mustache (import {{fastapiImplementationPackage}} and the pkgutil autoload);
  • the impl/__init__.py supporting 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 (interfaceOnly in spring, onlyInterfaces in go-server, skipStubs in scala-play-server).

No auto-detection from the value on purpose: a dotted value such as impl.v1 is a legitimate relative sub-package today, so a heuristic would be a breaking change.

Files:

  • CodegenConstants: USE_EXTERNAL_IMPLEMENTATION_PACKAGE and 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: new testExternalImplementationPackage (import as-is, no nodesc. prefix, no __init__.py generated 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

./mvnw -pl modules/openapi-generator -am -Dtest=PythonFastapiCodegenTest test

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \
  -i modules/openapi-generator/src/test/resources/3_1/nodesc.yaml -g python-fastapi -o /tmp/out \
  --additional-properties=packageName=nodesc,fastapiImplementationPackage=my_company.handlers,useExternalImplementationPackage=true
grep -n "my_company.handlers" /tmp/out/src/nodesc/apis/nodesc_api.py   # import my_company.handlers / ns_pkg = my_company.handlers
find /tmp/out -name __init__.py | grep -c handlers                       # 0

No sample uses the option, so ./bin/generate-samples.sh produces no diff.

PR checklist

  • [ X ] Read the contribution guidelines.
  • [ X ] 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.
  • [ X ] 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

Adds useExternalImplementationPackage, an opt-in flag for the python-fastapi generator. Since 7.13.0, fastapiImplementationPackage gets unconditionally prefixed with packageName, breaking absolute package paths. The flag restores the earlier behavior for users who need it.

  • When true, the package is used as-is and no __init__.py is generated for it.
  • Defaults to false so existing setups keep current behavior.
  • Includes tests and a docs update.

Written for commit c87706f. Summary will update on new commits.

Review in cubic

… implementation package outside the generated one

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 4 files

Re-trigger cubic

@wing328
wing328 merged commit c83886d into OpenAPITools:master Sep 17, 2026
15 checks passed
@wing328 wing328 added this to the 7.26.0 milestone Sep 17, 2026
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