diff --git a/openapi_to_fastapi/model_generator.py b/openapi_to_fastapi/model_generator.py index 1498dd9..639994b 100644 --- a/openapi_to_fastapi/model_generator.py +++ b/openapi_to_fastapi/model_generator.py @@ -11,12 +11,13 @@ from openapi_to_fastapi.logger import logger -def generate_model_from_schema(schema: str) -> str: +def generate_model_from_schema(schema: str, format_code: bool = False) -> str: """ Given an OpenAPI schema, generate pydantic models from everything defined in the "components/schemas" section :param schema: Content of an OpenAPI spec, plain text + :param format_code: Whether to format generated code :return: Importable python code with generated models """ parser = OpenAPIParser( @@ -37,7 +38,7 @@ def generate_model_from_schema(schema: str) -> str: aliases=None, ) - result = parser.parse() + result = parser.parse(format_=format_code) return str(result) @@ -52,7 +53,9 @@ def _clean_tempfile(tmp_file, delete=True): Path(tmp_file.name).unlink() -def load_models(schema: str, name: str = "", cleanup: bool = True): +def load_models( + schema: str, name: str = "", cleanup: bool = True, format_code: bool = False +): """ Generate pydantic models from OpenAPI spec and return a python module, which contains all the models from the "components/schemas" section. @@ -61,6 +64,7 @@ def load_models(schema: str, name: str = "", cleanup: bool = True): :param schema: OpenAPI spec, plain text :param name: Prefix for a module name, optional :param cleanup: Whether to remove a file with models afterwards + :param format_code: Whether to format generated code :return: Module with pydantic models """ prefix = name.replace("/", "").replace(" ", "").replace("\\", "") + "_" @@ -70,7 +74,7 @@ def load_models(schema: str, name: str = "", cleanup: bool = True): ), delete=cleanup, ) as tmp_file: - model_py = generate_model_from_schema(schema) + model_py = generate_model_from_schema(schema, format_code) tmp_file.write(model_py) if not cleanup: logger.info("Generated module %s: %s", name, tmp_file.name) diff --git a/openapi_to_fastapi/routes.py b/openapi_to_fastapi/routes.py index fe17ec5..d5f322a 100644 --- a/openapi_to_fastapi/routes.py +++ b/openapi_to_fastapi/routes.py @@ -78,9 +78,11 @@ def __init__( self, specs_path: Union[str, Path], validators: Optional[List[Type[BaseValidator]]] = None, + format_code: bool = False, ): self._validators = [DefaultValidator] + (validators or []) # type: ignore self._routes = RoutesMapping(post_map={}, get_map={}) + self._format_code = format_code self.specs_path = Path(specs_path) self._validate_and_parse_specs() @@ -115,7 +117,7 @@ def _validate_and_parse_specs(self): raw_spec = spec_path.read_text(encoding="utf8") json_spec = json.loads(raw_spec) for path, path_item in parse_openapi_spec(json_spec).items(): - models = load_models(raw_spec, path) + models = load_models(raw_spec, path, format_code=self._format_code) post = path_item.post if post: req_model = getattr(models, post.requestBodyModel, EmptyBody) diff --git a/pyproject.toml b/pyproject.toml index 1ef0731..41530c7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "openapi-to-fastapi" -version = "0.11.1" +version = "0.11.2" description = "Create FastAPI routes from OpenAPI spec" authors = ["IOXIO Ltd"] license = "BSD-3-Clause"