Skip to content

Commit

Permalink
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
pre-commit-ci[bot] committed Sep 18, 2024
1 parent d9cca08 commit 8300140
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .idea/fastapi-cli.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ In most cases you would (and should) have a "termination proxy" handling HTTPS f

When you run `fastapi schema`, it will generate a swagger/openapi document.

This document will be output to stderr by default, however `--output <filename>` option can be used to write output into file. You can control the format of the JSON file by specifying indent level with `--indent #`. If set to 0, JSON will be in the minimal/compress form. Default is 2 spaces.
This document will be output to stderr by default, however `--output <filename>` option can be used to write output into file. You can control the format of the JSON file by specifying indent level with `--indent #`. If set to 0, JSON will be in the minimal/compress form. Default is 2 spaces.

## License

Expand Down
2 changes: 1 addition & 1 deletion openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
}
}
}
}
}
57 changes: 28 additions & 29 deletions src/fastapi_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
from rich.panel import Panel
from typing_extensions import Annotated

from fastapi_cli.discover import get_import_string
from fastapi_cli.discover import get_app
from fastapi_cli.discover import get_app, get_import_string
from fastapi_cli.exceptions import FastAPICLIException

from . import __version__
Expand Down Expand Up @@ -275,35 +274,34 @@ def run(
proxy_headers=proxy_headers,
)


@app.command()
def schema(
path: Annotated[
Union[Path, None],
typer.Argument(
help="A path to a Python file or package directory (with [blue]__init__.py[/blue] files) containing a [bold]FastAPI[/bold] app. If not provided, a default set of paths will be tried."
),
] = None,
*,
app: Annotated[
Union[str, None],
typer.Option(
help="The name of the variable that contains the [bold]FastAPI[/bold] app in the imported module or package. If not provided, it is detected automatically."
),
] = None,
output: Annotated[
Union[str, None],
typer.Option(
help="The filename to write schema to. If not provided, write to stderr."
),
] = None,
indent: Annotated[
int,
typer.Option(
help="JSON format indent. If 0, disable pretty printing"
),
] = 2,
) -> Any:
""" Generate schema """
path: Annotated[
Union[Path, None],
typer.Argument(
help="A path to a Python file or package directory (with [blue]__init__.py[/blue] files) containing a [bold]FastAPI[/bold] app. If not provided, a default set of paths will be tried."
),
] = None,
*,
app: Annotated[
Union[str, None],
typer.Option(
help="The name of the variable that contains the [bold]FastAPI[/bold] app in the imported module or package. If not provided, it is detected automatically."
),
] = None,
output: Annotated[
Union[str, None],
typer.Option(
help="The filename to write schema to. If not provided, write to stderr."
),
] = None,
indent: Annotated[
int,
typer.Option(help="JSON format indent. If 0, disable pretty printing"),
] = 2,
) -> Any:
"""Generate schema"""
app = get_app(path=path, app_name=app)
schema = app.openapi()

Expand All @@ -313,5 +311,6 @@ def schema(
stream.close()
return 0


def main() -> None:
app()
8 changes: 4 additions & 4 deletions src/fastapi_cli/discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ModuleData:

@contextmanager
def sys_path(self):
""" Context manager to temporarily alter sys.path"""
"""Context manager to temporarily alter sys.path"""
extra_sys_path = str(self.extra_sys_path) if self.extra_sys_path else ""
if extra_sys_path:
logger.warning("Adding %s to sys.path...", extra_sys_path)
Expand Down Expand Up @@ -179,8 +179,9 @@ def get_import_string(
logger.info(f"Using import string [b green]{import_string}[/b green]")
return import_string


def get_app(
*, path: Union[Path, None] = None, app_name: Union[str, None] = None
*, path: Union[Path, None] = None, app_name: Union[str, None] = None
) -> FastAPI:
if not path:
path = get_default_path()
Expand Down Expand Up @@ -227,5 +228,4 @@ def get_app(
obj = getattr(mod, name)
if isinstance(obj, FastAPI):
return obj
raise FastAPICLIException(
"Could not find FastAPI app in module, try using --app")
raise FastAPICLIException("Could not find FastAPI app in module, try using --app")
2 changes: 1 addition & 1 deletion tests/assets/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
}
}
}
}
}
7 changes: 4 additions & 3 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,15 @@ def test_dev_help() -> None:
assert "The name of the variable that contains the FastAPI app" in result.output
assert "Use multiple worker processes." not in result.output


def test_schema() -> None:
with changing_dir(assets_path):
with open('openapi.json', 'r') as stream:
with open("openapi.json") as stream:
expected = stream.read()
assert expected != "" , "Failed to read expected result"
assert expected != "", "Failed to read expected result"
result = runner.invoke(app, ["schema", "single_file_app.py"])
assert result.exit_code == 0, result.output
assert expected in result.output, result.output
assert expected in result.output, result.output


def test_run_help() -> None:
Expand Down

0 comments on commit 8300140

Please sign in to comment.