Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: form field Enum overrides #235

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/python-fastui/fastui/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ def deference_json_schema(
if def_schema is None:
raise ValueError(f'Invalid $ref "{ref}", not found in {defs}')
else:
def_schema.update({k: v for k, v in schema.items() if k != '$ref'}) # type: ignore
return def_schema, required
elif any_of := schema.get('anyOf'):
if len(any_of) == 2 and sum(s.get('type') == 'null' for s in any_of) == 1:
Expand Down
59 changes: 58 additions & 1 deletion src/python-fastui/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from contextlib import asynccontextmanager
from enum import Enum
from io import BytesIO
from typing import List, Tuple, Union

import pytest
from fastapi import HTTPException
from fastui import components
from fastui.forms import FormFile, Textarea, fastui_form
from pydantic import BaseModel
from pydantic import BaseModel, Field
from starlette.datastructures import FormData, Headers, UploadFile
from typing_extensions import Annotated

Expand Down Expand Up @@ -469,3 +470,59 @@ def test_form_textarea_form_fields():
}
],
}


class ToolEnum(str, Enum):
"""Tools that can be leveraged to complete a job."""

hammer = 'hammer'
screwdriver = 'screwdriver'
saw = 'saw'
claw_hammer = 'claw_hammer'


class FormEnumWithOptionalEnum(BaseModel):
job_name: str
tool_required: Union[ToolEnum, None] = Field(
json_schema_extra={
# Override certain schema fields.
'placeholder': 'tool',
'description': '',
}
)


def test_form_with_enum():
m = components.ModelForm(model=FormEnumWithOptionalEnum, submit_url='/foobar')

assert m.model_dump(by_alias=True, exclude_none=True) == {
'submitUrl': '/foobar',
'method': 'POST',
'type': 'ModelForm',
'formFields': [
{
'name': 'job_name',
'title': ['Job Name'],
'required': True,
'locked': False,
'htmlType': 'text',
'type': 'FormFieldInput',
},
{
'name': 'tool_required',
'title': ['ToolEnum'],
'required': False,
'locked': False,
'description': '',
'options': [
{'value': 'hammer', 'label': 'Hammer'},
{'value': 'screwdriver', 'label': 'Screwdriver'},
{'value': 'saw', 'label': 'Saw'},
{'value': 'claw_hammer', 'label': 'Claw Hammer'},
],
'multiple': False,
'placeholder': 'tool',
'type': 'FormFieldSelect',
},
],
}