OpenAPITools / OpenAPITools/openapi-generator

[BUG][PYTHON-SERVER] Incorrect Import Paths When Using Custom modelPackage and apiPackage

Open
#20,917 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When generating a Python FastAPI server with OpenAPI Generator, the generated import paths do not respect the custom folder names set via --additional-properties (e.g., modelPackage=dto, apiPackage=service). Instead, the generated code incorrectly references default folders, leading to errors. When using default folder names, imports are generated correctly. It is only using custom folder names via the --aditional-properties flag, the generated imports for both server and client code do not always reflect the custom folder names.

  • The generated imports for models incorrectly reference the default folder name (e.g., models) instead of the custom folder (e.g dto).
    from serverPetAPI.models.user import User
    Uses default 'models' instead of 'dto'. This causes the error
    from serverPetAPI.models.user import User ModuleNotFoundError: No module named 'serverPetAPI.models'

  • The generated imports may incorrectly reference the base folder (e.g., service) rather than the expected custom folder structure (e.g., serverPetAPI.service).
    from service.user_api_base import BaseUserApi
    Imports directly from service instead of serverPetAPI. This causes the error
    from service.user_api_base import BaseUserApi ModuleNotFoundError: No module named 'service'

Both cases can be found in the user_api.py file:

  • Incorrect generated file can be found here
  • Expected generated file can be found here
openapi-generator version

7.11.0

OpenAPI declaration file content or url

OpenAPI's Generator petstore api examples:

Command line used for generation

docker run --rm
-v ${PWD}:/local openapitools/openapi-generator-cli generate
-i /local/apis/petstore.yml
-g python-fastapi
--additional-properties=sourceFolder=,modelPackage=dto,apiPackage=service
-o /local/generated-docker

Steps to reproduce

running the command line on the specified api

Related issues/PRs

A full example, including generated code and expected output, can be found here: https://github.com/claudiadpp/openapi-python-demo/pull/1

  • Generated Code (Incorrect Imports): View Here
  • Expected Output (Fixed Imports): View Here
Suggest a fix

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the python-fastapi generator using the petstore API example and the command shown, then compare the generated service/user_api.py with the linked expected file. Trace how custom modelPackage=dto and apiPackage=service are reflected in imports. Done means generated server and client imports resolve correctly with custom package names.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, python
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.