|
| 1 | +import enum |
1 | 2 | from contextlib import asynccontextmanager
|
2 | 3 | from io import BytesIO
|
3 | 4 | from typing import List, Tuple, Union
|
|
6 | 7 | from fastapi import HTTPException
|
7 | 8 | from fastui import components
|
8 | 9 | from fastui.forms import FormFile, Textarea, fastui_form
|
9 |
| -from pydantic import BaseModel |
| 10 | +from pydantic import BaseModel, Field |
10 | 11 | from starlette.datastructures import FormData, Headers, UploadFile
|
11 | 12 | from typing_extensions import Annotated
|
12 | 13 |
|
@@ -469,3 +470,55 @@ def test_form_textarea_form_fields():
|
469 | 470 | }
|
470 | 471 | ],
|
471 | 472 | }
|
| 473 | + |
| 474 | + |
| 475 | +class SelectEnum(str, enum.Enum): |
| 476 | + one = 'one' |
| 477 | + two = 'two' |
| 478 | + |
| 479 | + |
| 480 | +class FormSelectMultiple(BaseModel): |
| 481 | + select_single: SelectEnum = Field(title='Select Single', description='first field') |
| 482 | + select_single_2: SelectEnum = Field(title='Select Single') # unset description to test leakage from prev. field |
| 483 | + select_multiple: List[SelectEnum] = Field(title='Select Multiple', description='third field') |
| 484 | + |
| 485 | + |
| 486 | +def test_form_description_leakage(): |
| 487 | + m = components.ModelForm(model=FormSelectMultiple, submit_url='/foobar/') |
| 488 | + |
| 489 | + assert m.model_dump(by_alias=True, exclude_none=True) == { |
| 490 | + 'formFields': [ |
| 491 | + { |
| 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', |
| 512 | + 'locked': False, |
| 513 | + 'multiple': True, |
| 514 | + 'name': 'select_multiple', |
| 515 | + 'options': [{'label': 'One', 'value': 'one'}, {'label': 'Two', 'value': 'two'}], |
| 516 | + 'required': True, |
| 517 | + 'title': ['Select Multiple'], |
| 518 | + 'type': 'FormFieldSelect', |
| 519 | + }, |
| 520 | + ], |
| 521 | + 'method': 'POST', |
| 522 | + 'submitUrl': '/foobar/', |
| 523 | + 'type': 'ModelForm', |
| 524 | + } |
0 commit comments