Skip to content

Commit

Permalink
Improve test and fix the second case of description leakage.
Browse files Browse the repository at this point in the history
  • Loading branch information
sergue1 committed Feb 15, 2024
1 parent e24b06c commit 62fa0af
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/python-fastui/fastui/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ def deference_json_schema(
if def_schema is None:
raise ValueError(f'Invalid $ref "{ref}", not found in {defs}')
else:
return def_schema, required
# clone dict to avoid attribute leakage

Check warning on line 313 in src/python-fastui/fastui/json_schema.py

View check run for this annotation

Codecov / codecov/patch

src/python-fastui/fastui/json_schema.py#L313

Added line #L313 was not covered by tests
return def_schema.copy(), required
elif any_of := schema.get('anyOf'):
if len(any_of) == 2 and sum(s.get('type') == 'null' for s in any_of) == 1:
# If anyOf is a single type and null, then it is optional
Expand Down
39 changes: 33 additions & 6 deletions src/python-fastui/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import enum
from contextlib import asynccontextmanager
from io import BytesIO
from typing import List, Literal, Tuple, Union
from typing import List, Tuple, Union

import pytest
from fastapi import HTTPException
Expand Down Expand Up @@ -471,8 +472,15 @@ def test_form_textarea_form_fields():
}


class SelectEnum(str, enum.Enum):
one = 'one'
two = 'two'


class FormSelectMultiple(BaseModel):
values: List[Literal['foo', 'bar']] = Field(title='Select Multiple', description='First Selector')
select_single: SelectEnum = Field(title='Select Single', description='first field')
select_single_2: SelectEnum = Field(title='Select Single') # unset description
select_multiple: List[SelectEnum] = Field(title='Select Multiple', description='third field')


def test_form_select_multiple():
Expand All @@ -481,15 +489,34 @@ def test_form_select_multiple():
assert m.model_dump(by_alias=True, exclude_none=True) == {
'formFields': [
{
'description': 'First Selector',
'description': 'first field',
'locked': False,
'multiple': False,
'name': 'select_single',
'options': [{'label': 'One', 'value': 'one'}, {'label': 'Two', 'value': 'two'}],
'required': True,
'title': ['Select Single'],
'type': 'FormFieldSelect',
},
{
'locked': False,
'multiple': False,
'name': 'select_single_2',
'options': [{'label': 'One', 'value': 'one'}, {'label': 'Two', 'value': 'two'}],
'required': True,
'title': ['Select Single'],
'type': 'FormFieldSelect',
},
{
'description': 'third field',
'locked': False,
'multiple': True,
'name': 'values',
'options': [{'label': 'Foo', 'value': 'foo'}, {'label': 'Bar', 'value': 'bar'}],
'name': 'select_multiple',
'options': [{'label': 'One', 'value': 'one'}, {'label': 'Two', 'value': 'two'}],
'required': True,
'title': ['Select Multiple'],
'type': 'FormFieldSelect',
}
},
],
'method': 'POST',
'submitUrl': '/foobar/',
Expand Down

0 comments on commit 62fa0af

Please sign in to comment.