diff --git a/src/python-fastui/fastui/components/__init__.py b/src/python-fastui/fastui/components/__init__.py index 0fb50862..ca151d20 100644 --- a/src/python-fastui/fastui/components/__init__.py +++ b/src/python-fastui/fastui/components/__init__.py @@ -185,7 +185,7 @@ def __get_pydantic_json_schema__( ) -> _t.Any: # until https://github.com/pydantic/pydantic/issues/8413 is fixed json_schema = handler(core_schema) - json_schema['required'].append('links') + json_schema.setdefault('required', []).append('links') return json_schema diff --git a/src/python-fastui/tests/test_json_schema.py b/src/python-fastui/tests/test_json_schema.py index f01cd60c..c6ba4b45 100644 --- a/src/python-fastui/tests/test_json_schema.py +++ b/src/python-fastui/tests/test_json_schema.py @@ -1,6 +1,8 @@ from dirty_equals import IsPartialDict -from fastui import FastUI +from fastapi import FastAPI +from fastui import FastUI, components from fastui.generate_typescript import generate_json_schema +from httpx import AsyncClient async def test_json_schema(): @@ -12,3 +14,25 @@ async def test_json_schema(): 'type': 'array', } # TODO test more specific cases + + +async def test_openapi(): + app = FastAPI() + + @app.get('/api/', response_model=FastUI, response_model_exclude_none=True) + def test_endpoint(): + return [components.Text(text='hello')] + + async with AsyncClient(app=app, base_url='http://test') as client: + r = await client.get('/openapi.json') + assert r.status_code == 200 + assert r.headers['content-type'] == 'application/json' + assert r.json() == { + 'openapi': '3.1.0', + 'info': { + 'title': 'FastAPI', + 'version': '0.1.0', + }, + 'paths': IsPartialDict(), + 'components': IsPartialDict(), + }