Skip to content

Commit 62fa0af

Browse files
committed
Improve test and fix the second case of description leakage.
1 parent e24b06c commit 62fa0af

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

src/python-fastui/fastui/json_schema.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ def deference_json_schema(
310310
if def_schema is None:
311311
raise ValueError(f'Invalid $ref "{ref}", not found in {defs}')
312312
else:
313-
return def_schema, required
313+
# clone dict to avoid attribute leakage
314+
return def_schema.copy(), required
314315
elif any_of := schema.get('anyOf'):
315316
if len(any_of) == 2 and sum(s.get('type') == 'null' for s in any_of) == 1:
316317
# If anyOf is a single type and null, then it is optional

src/python-fastui/tests/test_forms.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import enum
12
from contextlib import asynccontextmanager
23
from io import BytesIO
3-
from typing import List, Literal, Tuple, Union
4+
from typing import List, Tuple, Union
45

56
import pytest
67
from fastapi import HTTPException
@@ -471,8 +472,15 @@ def test_form_textarea_form_fields():
471472
}
472473

473474

475+
class SelectEnum(str, enum.Enum):
476+
one = 'one'
477+
two = 'two'
478+
479+
474480
class FormSelectMultiple(BaseModel):
475-
values: List[Literal['foo', 'bar']] = Field(title='Select Multiple', description='First Selector')
481+
select_single: SelectEnum = Field(title='Select Single', description='first field')
482+
select_single_2: SelectEnum = Field(title='Select Single') # unset description
483+
select_multiple: List[SelectEnum] = Field(title='Select Multiple', description='third field')
476484

477485

478486
def test_form_select_multiple():
@@ -481,15 +489,34 @@ def test_form_select_multiple():
481489
assert m.model_dump(by_alias=True, exclude_none=True) == {
482490
'formFields': [
483491
{
484-
'description': 'First Selector',
492+
'description': 'first field',
493+
'locked': False,
494+
'multiple': False,
495+
'name': 'select_single',
496+
'options': [{'label': 'One', 'value': 'one'}, {'label': 'Two', 'value': 'two'}],
497+
'required': True,
498+
'title': ['Select Single'],
499+
'type': 'FormFieldSelect',
500+
},
501+
{
502+
'locked': False,
503+
'multiple': False,
504+
'name': 'select_single_2',
505+
'options': [{'label': 'One', 'value': 'one'}, {'label': 'Two', 'value': 'two'}],
506+
'required': True,
507+
'title': ['Select Single'],
508+
'type': 'FormFieldSelect',
509+
},
510+
{
511+
'description': 'third field',
485512
'locked': False,
486513
'multiple': True,
487-
'name': 'values',
488-
'options': [{'label': 'Foo', 'value': 'foo'}, {'label': 'Bar', 'value': 'bar'}],
514+
'name': 'select_multiple',
515+
'options': [{'label': 'One', 'value': 'one'}, {'label': 'Two', 'value': 'two'}],
489516
'required': True,
490517
'title': ['Select Multiple'],
491518
'type': 'FormFieldSelect',
492-
}
519+
},
493520
],
494521
'method': 'POST',
495522
'submitUrl': '/foobar/',

0 commit comments

Comments
 (0)