OpenAPITools / OpenAPITools/openapi-generator

[Python] Support for package hierarchy

Open
#1,302 9 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Python General: Suggestion
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

I am trying to generate a service client for a project that uses a particular package hierarchy by convention. I have tried the following but am yet to generate a client that I can successfully import, due to mis-matching/incorrect import statements, and files that are placed in the wrong folders.

For example #1 (below) I get code like import api where import foo.bar.baz.api is desired. With example #2, I get directories like foo.bar.baz.my_client.model when foo/bar/baz/my_client/model is desired.

ex.1)

openapi-generator generate -g python -l python -c my_config.json \
    --api-package api --model-package model \
    --http-user-agent 'my_agent' \
    --log-to-stderr \
    -i ./api/my_spec.yaml -o $CLIENT_DIR

ex.2)

openapi-generator generate -g python -l python -c my_config.json \
    --api-package foo.bar.baz.my_client.api --model-package foo.bar.baz.my_client.model \
    --http-user-agent 'my_agent' \
    --log-to-stderr \
    -i ./api/my_spec.yaml -o $CLIENT_DIR

my_config.json:

  "packageName":"foo.bar.baz.my_client",
  "projectName":"my_project",
  "packageVersion": "1.0.0",
  "sortParamsByRequiredFlag": "true",
  "hideGenerationTimestamp": "true",
  "generateSourceCodeOnly": "false",
  "library": "urllib3"
}
openapi-generator version

3.3.1

OpenAPI declaration file content or url

I don't believe this is relevant. If needed I can provide a version of the spec.

Command line used for generation

See above.

Steps to reproduce

Run either of the commands above.

Related issues/PRs

I didn't find any.

Suggest a fix/enhancement

It seems like there is some code that is parsing the "foo.bar.baz" syntax into a file path since some of the files are correctly placed with the right import statements. Presumably some of the generator code (the part that handles --api-package api --model-package model) is not correctly handling these package names.

Thanks for reviewing this. Any pointers/suggestions would be really appreciated!

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 by reproducing the issue with my_config.json, ./api/my_spec.yaml, and the two openapi-generator commands shown. Trace the Python generator's handling of --api-package, --model-package, and packageName; done means dotted package names produce matching directory paths and import statements in the generated client.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.