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

Missing lazy import in post body to_multipart function #1051

Open
mthanded opened this issue Jun 7, 2024 · 0 comments
Open

Missing lazy import in post body to_multipart function #1051

mthanded opened this issue Jun 7, 2024 · 0 comments

Comments

@mthanded
Copy link

mthanded commented Jun 7, 2024

Describe the bug
It is possible to generate a model with a missing import leading to invalid python code.

The below to_multipart function is generated in the body for the create_upload_file_uploadfile__post model and references UploadConfig however it is not in scope.

def to_multipart(self) -> Dict[str, Any]:
        file = self.file.to_tuple()

        config: Union[Tuple[None, bytes, str], Unset]

        if isinstance(self.config, Unset):
            config = UNSET
        elif isinstance(self.config, UploadConfig):
            config = (None, json.dumps(self.config.to_dict()).encode(), "application/json")
        else:
            config = (None, str(self.config).encode(), "text/plain")

        field_dict: Dict[str, Any] = {}
        for prop_name, prop in self.additional_properties.items():
            field_dict[prop_name] = (None, str(prop).encode(), "text/plain")

        field_dict.update(
            {
                "file": file,
            }
        )
        if config is not UNSET:
            field_dict["config"] = config

        return field_dict

The function to_dict right above it in the file lazily imports UploadConfig

    def to_dict(self) -> Dict[str, Any]:
        from ..models.upload_config import UploadConfig

        file = self.file.to_tuple()

OpenAPI Spec File

{
    "openapi": "3.1.0",
    "info": {
        "title": "FastAPI",
        "version": "0.1.0"
    },
    "paths": {
        "/uploadfile/": {
            "post": {
                "summary": "Create Upload File",
                "operationId": "create_upload_file_uploadfile__post",
                "requestBody": {
                    "content": {
                        "multipart/form-data": {
                            "schema": {
                                "$ref": "#/components/schemas/Body_create_upload_file_uploadfile__post"
                            }
                        }
                    },
                    "required": true
                },
                "responses": {
                    "200": {
                        "description": "Successful Response",
                        "content": {
                            "application/json": {
                                "schema": {}
                            }
                        }
                    },
                    "422": {
                        "description": "Validation Error",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/HTTPValidationError"
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "Body_create_upload_file_uploadfile__post": {
                "properties": {
                    "file": {
                        "type": "string",
                        "format": "binary",
                        "title": "File"
                    },
                    "config": {
                        "anyOf": [
                            {
                                "$ref": "#/components/schemas/UploadConfig"
                            },
                            {
                                "type": "null"
                            }
                        ]
                    }
                },
                "type": "object",
                "required": [
                    "file"
                ],
                "title": "Body_create_upload_file_uploadfile__post"
            },
            "HTTPValidationError": {
                "properties": {
                    "detail": {
                        "items": {
                            "$ref": "#/components/schemas/ValidationError"
                        },
                        "type": "array",
                        "title": "Detail"
                    }
                },
                "type": "object",
                "title": "HTTPValidationError"
            },
            "UploadConfig": {
                "properties": {
                    "test": {
                        "type": "string",
                        "title": "Test",
                        "default": "test"
                    },
                    "config": {
                        "type": "object",
                        "title": "Config",
                        "default": {}
                    }
                },
                "type": "object",
                "title": "UploadConfig"
            },
            "ValidationError": {
                "properties": {
                    "loc": {
                        "items": {
                            "anyOf": [
                                {
                                    "type": "string"
                                },
                                {
                                    "type": "integer"
                                }
                            ]
                        },
                        "type": "array",
                        "title": "Location"
                    },
                    "msg": {
                        "type": "string",
                        "title": "Message"
                    },
                    "type": {
                        "type": "string",
                        "title": "Error Type"
                    }
                },
                "type": "object",
                "required": [
                    "loc",
                    "msg",
                    "type"
                ],
                "title": "ValidationError"
            }
        }
    }
}

Desktop (please complete the following information):

  • OS: All
  • Python Version: All
  • openapi-python-client version 0.20.0

Additional context
I have been able to fix this locally by modifying the following

{% if model.is_multipart_body %}
def to_multipart(self) -> Dict[str, Any]:
{{ _to_dict(multipart=True) | indent(8) }}
{% endif %}

Solution is to add the lazy import template that is found in to_dict

{% for lazy_import in model.lazy_imports %}
{{ lazy_import }}
{% endfor %}

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