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

[BUG] python-nextgen circular refs lead to no imports #16010

Closed
5 of 6 tasks
indietyp opened this issue Jul 5, 2023 · 8 comments · Fixed by #16199
Closed
5 of 6 tasks

[BUG] python-nextgen circular refs lead to no imports #16010

indietyp opened this issue Jul 5, 2023 · 8 comments · Fixed by #16199

Comments

@indietyp
Copy link

indietyp commented Jul 5, 2023

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

Generation is successful, but for recursive models the imports are missing, when trying to generate from https://github.com/hashintel/hash/blob/main/apps/hash-graph/openapi/openapi.json you can see how in models/filter.py the import for AnyFilter is missing and how in models/any_filter.py the import is missing for Filter.

openapi-generator version

6.6.0

OpenAPI declaration file content or URL

https://github.com/hashintel/hash/blob/main/apps/hash-graph/openapi/openapi.json

Generation Details
    "generatorName": "python-nextgen",
    "packageName": "hash_openapi",
Steps to reproduce

Simply generate with the aforementioned schema

Related issues/PRs
Suggest a fix
@wing328
Copy link
Member

wing328 commented Jul 5, 2023

can you please try the latest master (snapshot version can be found in the readme)?

I remember merging a fix for that.

@wing328
Copy link
Member

wing328 commented Jul 5, 2023

in the latest master, python-nextgen has been renamed to python

@indietyp
Copy link
Author

indietyp commented Jul 5, 2023

I just tested with openapi-generator-cli-7.0.0-20230705.091805-173.jar using java -jar ~/Downloads/openapi-generator-cli-7.0.0-20230705.091805-173.jar generate -g python -i input/apps/hash-graph/openapi/openapi.json with the same result.

The imports are still absent.

@spacether
Copy link
Contributor

spacether commented Jul 5, 2023

FYI this was working in the python client in the v6.5.0 release. That client was deprecated in a later release and moved to a new repo.

That generator has moved to a different repo where it has test of self referencing object and array models. You can see them here:

@wing328
Copy link
Member

wing328 commented Jul 8, 2023

I tested it with the latest master and pytest runs fine without issues with a test as follows in test/test_filter.py:

    def testFilter(self):
        """Test Filter"""
        model = openapi_client.models.filter.Filter()

what error did you get when using the auto-generated client/sdk?

fa0311 added a commit to fa0311/openapi-generator that referenced this issue Jul 26, 2023
@fa0311
Copy link
Contributor

fa0311 commented Jul 27, 2023

Here is how to reproduce this

  • Build /bin/configs/python.yaml
    java -jar openapi-generator-cli.jar generate -g python -c . /bin/configs/python.yaml
  • Execute the following
from petstore_api.models.first_ref import FirstRef
FirstRef.from_dict({})

This is the output

Traceback (most recent call last):

  ....

  File "/home/yuki/openapi-generator/test,py", line 3, in <module>
    FirstRef.from_dict({})
  File "/home/yuki/openapi-generator/.venv/lib/site-packages/petstore_api/models/first_ref.py", line 70, in from_dict
    _obj = FirstRef.parse_obj({
  File "pydantic/main.py", line 526, in pydantic.main.BaseModel.parse_obj
  File "pydantic/main.py", line 339, in pydantic.main.BaseModel.__init__
  File "pydantic/main.py", line 1076, in pydantic.main.validate_model
  File "pydantic/fields.py", line 860, in pydantic.fields.ModelField.validate
pydantic.errors.ConfigError: field "self_ref" not yet prepared so type is still a ForwardRef, you might need to call FirstRef.update_forward_refs().

@wing328
Copy link
Member

wing328 commented Jul 28, 2023

@fa0311 thanks for the details. Will look into it with your PR this weekend.

wing328 pushed a commit that referenced this issue Jul 29, 2023
* [python-nextgen] fix #16010 circular refs lead to no imports

Signed-off-by: ふぁ <[email protected]>

* [python-nextgen] update samples

Signed-off-by: ふぁ <[email protected]>

* [python-nextgen] remove unnecessary Postponed annotations

Signed-off-by: ふぁ <[email protected]>

* [python-nextgen] update samples

Signed-off-by: ふぁ <[email protected]>

* [python-nextgen] remove unnecessary Postponed annotations

Signed-off-by: ふぁ <[email protected]>

* [python-nextgen] update samples

Signed-off-by: ふぁ <[email protected]>

* [python-nextgen] update samples

Signed-off-by: ふぁ <[email protected]>

---------

Signed-off-by: ふぁ <[email protected]>
@wing328
Copy link
Member

wing328 commented Jul 29, 2023

Thanks for the PR from @fa0311

@indietyp please pull the latest master to give it a try.

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

Successfully merging a pull request may close this issue.

4 participants