Skip to content

Fix bytes rendered wrong #106

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

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
2 changes: 1 addition & 1 deletion src/python-fastui/fastui/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def __get_pydantic_core_schema__(self, source_type: _t.Type[_t.Any], *_args) ->
def __get_pydantic_json_schema__(self, core_schema_: core_schema.CoreSchema, *_args) -> 'json_schema.JsonSchemaAny':
from . import json_schema

s = json_schema.JsonSchemaFile(type='string', format='binary')
s = json_schema.JsonSchemaFile(type='string', format='data-url')
if self.accept:
s['accept'] = self.accept

Expand Down
6 changes: 4 additions & 2 deletions src/python-fastui/fastui/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class JsonSchemaStringSearch(JsonSchemaBase, total=False):

class JsonSchemaFile(JsonSchemaBase, total=False):
type: _ta.Required[_t.Literal['string']]
format: _ta.Required[_t.Literal['binary']]
format: _ta.Required[_t.Literal['data-url']]
accept: str


Expand Down Expand Up @@ -214,7 +214,7 @@ def special_string_field(
schema: JsonSchemaConcrete, name: str, title: _t.List[str], required: bool, multiple: bool
) -> _t.Union[FormField, None]:
if schema['type'] == 'string':
if schema.get('format') == 'binary':
if schema.get('format') == 'data-url':
return FormFieldFile(
name=name,
title=title,
Expand Down Expand Up @@ -315,6 +315,8 @@ def as_title(s: _t.Any) -> str:
'string-uri': 'url',
'string-uuid': 'text',
'string-password': 'password',
'string-binary': 'text',
'string-base64': 'text',
'number': 'number',
'integer': 'number',
}
Expand Down
42 changes: 40 additions & 2 deletions src/python-fastui/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
from fastapi import HTTPException
from fastui import components
from fastui.forms import FormFile, fastui_form
from pydantic import BaseModel
from pydantic import Base64Str, BaseModel
from starlette.datastructures import FormData, Headers, UploadFile
from typing_extensions import Annotated


class SimpleForm(BaseModel):
name: str
size: int = 4
bytes_: bytes = b'fastui'
base64: Base64Str = 'fastui'


class FakeRequest:
Expand Down Expand Up @@ -54,6 +56,24 @@ def test_simple_form_fields():
'htmlType': 'number',
'type': 'FormFieldInput',
},
{
'name': 'bytes_',
'title': ['Bytes '],
'initial': 'fastui',
'required': False,
'locked': False,
'htmlType': 'text',
'type': 'FormFieldInput',
},
{
'name': 'base64',
'title': ['Base64'],
'initial': 'fastui',
'required': False,
'locked': False,
'htmlType': 'text',
'type': 'FormFieldInput',
},
],
}

Expand Down Expand Up @@ -85,6 +105,24 @@ def test_inline_form_fields():
'htmlType': 'number',
'type': 'FormFieldInput',
},
{
'name': 'bytes_',
'title': ['Bytes '],
'initial': 'fastui',
'required': False,
'locked': False,
'htmlType': 'text',
'type': 'FormFieldInput',
},
{
'name': 'base64',
'title': ['Base64'],
'initial': 'fastui',
'required': False,
'locked': False,
'htmlType': 'text',
'type': 'FormFieldInput',
},
],
}

Expand All @@ -96,7 +134,7 @@ async def test_simple_form_submit():

m = await form_dep.dependency(request)
assert isinstance(m, SimpleForm)
assert m.model_dump() == {'name': 'bar', 'size': 123}
assert m.model_dump() == {'name': 'bar', 'size': 123, 'bytes_': b'fastui', 'base64': 'ZmFzdHVp\n'}


async def test_simple_form_submit_repeat():
Expand Down