Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] Support for package hierarchy #1302

Open
mmunz-nextdoor opened this issue Oct 23, 2018 · 9 comments
Open

[Python] Support for package hierarchy #1302

mmunz-nextdoor opened this issue Oct 23, 2018 · 9 comments

Comments

@mmunz-nextdoor
Copy link

mmunz-nextdoor commented Oct 23, 2018

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!

@mmunz-nextdoor
Copy link
Author

This sounds like a related, if not the same, issue.

@wing328
Copy link
Member

wing328 commented Nov 5, 2018

cc @taxpon (2017/07) @frol (2017/07) @mbohlool (2017/07) @cbornet (2017/09) @kenjones-cisco (2017/11) @tomplus (2018/10)

@ndjensen
Copy link
Contributor

ndjensen commented Nov 9, 2018

I'm upvoting this as helpful. When I ran the command line with a config.json that had packageName of foo.bar.baz (no model-package or api-package) I ended up with two directory structures. The first was one directory named foo.bar.baz that included api_client.py, the __init__.py files, configuration.py, and rest.py. The second directory structure I ended up with was foo/bar/baz/ which is more of what I was expecting and it had foo/bar/baz/api/pet_api.py and foo/bar/baz/models/pet.py. And the imports don't work.

@mmunz-nextdoor
Copy link
Author

So I was able to work around this by customizing 3 of the python templates to use relative imports, as follows.

__init__api.mustache

from __future__ import absolute_import

# flake8: noqa

# import apis into api package
{{#apiInfo}}{{#apis}}from .{{classVarName}} import {{classname}}
{{/apis}}{{/apiInfo}}

__init__model.mustache

# coding: utf-8

# flake8: noqa

{{>partial_header}}

from __future__ import absolute_import

# import models into model package
{{#models}}{{#model}}from .{{classFilename}} import {{classname}}{{/model}}
{{/models}}

__init__package.mustache

# coding: utf-8

# flake8: noqa

{{>partial_header}}

from __future__ import absolute_import

__version__ = "{{packageVersion}}"

# import apis into sdk package
{{#apiInfo}}{{#apis}}from .{{apiPackage}}.{{classVarName}} import {{classname}}
{{/apis}}{{/apiInfo}}

# import ApiClient
from .api_client import ApiClient
from .configuration import Configuration

# import models into sdk package
{{#models}}{{#model}}from .model.{{classFilename}} import {{classname}}
{{/model}}{{/models}}

@wing328
Copy link
Member

wing328 commented Nov 19, 2018

@mmunz-nextdoor thanks for sharing the workaround. Do you mind submitting a PR so that we can review the change more easily?

@houdejun214
Copy link

houdejun214 commented Nov 30, 2018

Thanks @wing328 linking me to this discussion, this is very closed to my requirement.

Let me share my user case:
We are providing a generated client code for my user, but the generated code will be part of my whole client package with some other custom code module together.

|-- <my root package>
|-- |---- <my sub module 1>
|-- |---- <my sub module 2>
|-- |---- <generated client module>
|-- setup.py
|-- {other files}

A package hierarchy is required to support my case well.

I have a PR be committed to swagger-codegen to solve this problem, it works well in my side.
swagger-api/swagger-codegen#8955

@svenpanne
Copy link
Contributor

I have submitted a patch for the client part in #2016 and will look at the server part, soon.

@svenpanne
Copy link
Contributor

BTW: generateSourceCodeOnly is currently broken, too, fixed in #2015.

@svenpanne
Copy link
Contributor

Fix for server part in #2041.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants