Skip to content

Commit e115b4e

Browse files
committed
Merge branch 'feature/add-schema-generation' of github.com:mlasevich/fastapi-cli into feature/add-schema-generation
# Conflicts: # src/fastapi_cli/cli.py # src/fastapi_cli/discover.py
2 parents 1aebb9e + d228f30 commit e115b4e

File tree

4 files changed

+33
-31
lines changed

4 files changed

+33
-31
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ In most cases you would (and should) have a "termination proxy" handling HTTPS f
106106

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

109-
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.
109+
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.
110110

111111
## License
112112

src/fastapi_cli/cli.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
from rich.panel import Panel
1111
from typing_extensions import Annotated
1212

13-
from fastapi_cli.discover import get_import_string
14-
from fastapi_cli.discover import get_app
13+
from fastapi_cli.discover import get_app, get_import_string
1514
from fastapi_cli.exceptions import FastAPICLIException
1615

1716
from . import __version__
@@ -275,29 +274,30 @@ def run(
275274
proxy_headers=proxy_headers,
276275
)
277276

277+
278278
@app.command()
279279
def schema(
280-
path: Annotated[
281-
Union[Path, None],
282-
typer.Argument(
283-
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."
284-
),
285-
] = None,
286-
*,
287-
app: Annotated[
288-
Union[str, None],
289-
typer.Option(
290-
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."
291-
),
292-
] = None,
293-
output: Annotated[
294-
Union[str, None],
295-
typer.Option(
296-
help="The filename to write schema to. If not provided, write to stderr."
297-
),
298-
] = None,
299-
indent: Annotated[
300-
int,
280+
path: Annotated[
281+
Union[Path, None],
282+
typer.Argument(
283+
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."
284+
),
285+
] = None,
286+
*,
287+
app: Annotated[
288+
Union[str, None],
289+
typer.Option(
290+
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."
291+
),
292+
] = None,
293+
output: Annotated[
294+
Union[str, None],
295+
typer.Option(
296+
help="The filename to write schema to. If not provided, write to stderr."
297+
),
298+
] = None,
299+
indent: Annotated[
300+
int,
301301
typer.Option(
302302
help="JSON format indent. If 0, disable pretty printing"
303303
),
@@ -313,5 +313,6 @@ def schema(
313313
stream.close()
314314
return 0
315315

316+
316317
def main() -> None:
317318
app()

src/fastapi_cli/discover.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class ModuleData:
4949

5050
@contextmanager
5151
def sys_path(self) -> str:
52-
""" Context manager to temporarily alter sys.path"""
52+
"""Context manager to temporarily alter sys.path"""
5353
extra_sys_path = str(self.extra_sys_path) if self.extra_sys_path else ""
5454
if extra_sys_path:
5555
logger.warning("Adding %s to sys.path...", extra_sys_path)
@@ -179,8 +179,9 @@ def get_import_string(
179179
logger.info(f"Using import string [b green]{import_string}[/b green]")
180180
return import_string
181181

182+
182183
def get_app(
183-
*, path: Union[Path, None] = None, app_name: Union[str, None] = None
184+
*, path: Union[Path, None] = None, app_name: Union[str, None] = None
184185
) -> FastAPI:
185186
if not path:
186187
path = get_default_path()
@@ -227,5 +228,4 @@ def get_app(
227228
obj = getattr(mod, name)
228229
if isinstance(obj, FastAPI):
229230
return obj
230-
raise FastAPICLIException(
231-
"Could not find FastAPI app in module, try using --app")
231+
raise FastAPICLIException("Could not find FastAPI app in module, try using --app")

tests/test_cli.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,15 @@ def test_dev_help() -> None:
179179
assert "The name of the variable that contains the FastAPI app" in result.output
180180
assert "Use multiple worker processes." not in result.output
181181

182+
182183
def test_schema() -> None:
183184
with changing_dir(assets_path):
184-
with open('openapi.json', 'r') as stream:
185+
with open("openapi.json") as stream:
185186
expected = stream.read()
186-
assert expected != "" , "Failed to read expected result"
187+
assert expected != "", "Failed to read expected result"
187188
result = runner.invoke(app, ["schema", "single_file_app.py"])
188189
assert result.exit_code == 0, result.output
189-
assert expected in result.output, result.output
190+
assert expected in result.output, result.output
190191

191192

192193
def test_run_help() -> None:

0 commit comments

Comments
 (0)