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

Support for the route body type like Union[TypeA, TypeB] #391

Open
alexeynick opened this issue Sep 29, 2023 · 0 comments
Open

Support for the route body type like Union[TypeA, TypeB] #391

alexeynick opened this issue Sep 29, 2023 · 0 comments

Comments

@alexeynick
Copy link

alexeynick commented Sep 29, 2023

I've noticed that routes with body argument be a union of types (Union[TypeA, TypeB] for example) when combined in a main.py file lack TypeB in import section (if it's not used anywhere else). Got an idea how to fix it (in visitor/imports.py):

def _get_most_of_reference(data_type: DataType) -> Iterable[Reference]:
    if data_type.reference:
        yield data_type.reference
    for data_type in data_type.data_types:
        for reference in _get_most_of_reference(data_type):
            yield reference


def get_imports(parser: OpenAPIParser, model_path: Path) -> Dict[str, object]:
    imports = Imports()

    imports.update(parser.imports)
    for data_type in parser.data_types:
        for reference in _get_most_of_reference(data_type):
            imports.append(data_type.all_imports)
            imports.append(
                Import.from_full_path(f'.{model_path.stem}.{reference.name}')
            )
    for from_, imports_ in parser.imports_for_fastapi.items():
        imports[from_].update(imports_)
    return {'imports': imports}

If we replace "return" with "yield" in _get_most_of_reference we'll be able to iterate through all the nested data types (otherwise it returns only TypeA).
Thank you for your project, it's great!

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

No branches or pull requests

1 participant