Skip to content

refactor: Replace the models package with one models module, which contains all models #636

Closed
@mtovt

Description

@mtovt

Is your feature request related to a problem? Please describe.
One of the issues that block implementation of #466 or #563 is circular import occurs in the case of circular and recursive references.

Describe the solution you'd like
I want to move the generated models to one module because it simplifies the support of circular and recursive reference development. See #563 and #466. Seems @maz808's pull request #563 solves several problems with references and would be great to finish it and merge. But it contains a circular import problem. I've described it this message.

I've already implemented a possible solution in this branch. Maybe it's done not enough accurately but seems it's working. I want to receive feedback to complete this and #563.

Describe alternatives you've considered
The alternate solution is to continue using package models with a separate module for each model. We may prevent the circular import problem by David Beasley hack, using this statement in the model template:

try:
    from ..models import a_circular_model
except ImportError:
    import sys

    change = sys.modules[__package__ + "a_circular_model"]

It also will produce the working client code, but it has more complexity. I believe the one module is more ergonomic. Because for instance one of my OpenAPI specs has a lot of circular references. In that case, it produces a lot of strings with the statements, I've mentioned above. It will look like

        try:
            from ..models import user as user_m
            # ... 15 lines 
        except ImportError:
            import sys

            user_m = sys.modules[__package__ + "user"]
            # ... 15 lines 

And I believe I have models with more recursive references 😅

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions