Skip to content

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

Description

@claudiadpp

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: claudiadpp/openapi-python-demo#1

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions