From fd1502630534112b6ab65f9a498fbebfe8fe349f Mon Sep 17 00:00:00 2001 From: Chaojie Date: Mon, 18 Dec 2023 17:03:20 +0800 Subject: [PATCH 1/2] Fix bytes rendered wrong --- src/python-fastui/fastui/forms.py | 2 +- src/python-fastui/fastui/json_schema.py | 6 ++++-- src/python-fastui/tests/test_forms.py | 25 ++++++++++++++++++++++--- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/python-fastui/fastui/forms.py b/src/python-fastui/fastui/forms.py index cd200af7..6b6f565f 100644 --- a/src/python-fastui/fastui/forms.py +++ b/src/python-fastui/fastui/forms.py @@ -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 diff --git a/src/python-fastui/fastui/json_schema.py b/src/python-fastui/fastui/json_schema.py index bbd4a7f6..46e584ea 100644 --- a/src/python-fastui/fastui/json_schema.py +++ b/src/python-fastui/fastui/json_schema.py @@ -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 @@ -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, @@ -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', } diff --git a/src/python-fastui/tests/test_forms.py b/src/python-fastui/tests/test_forms.py index 751eb456..f9faec0a 100644 --- a/src/python-fastui/tests/test_forms.py +++ b/src/python-fastui/tests/test_forms.py @@ -6,7 +6,7 @@ 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 @@ -14,6 +14,7 @@ class SimpleForm(BaseModel): name: str size: int = 4 + base64: Base64Str = 'ZmFzdHVp' class FakeRequest: @@ -54,6 +55,15 @@ def test_simple_form_fields(): 'htmlType': 'number', 'type': 'FormFieldInput', }, + { + 'name': 'base64', + 'title': ['Base64'], + 'initial': 'ZmFzdHVp', + 'required': False, + 'locked': False, + 'htmlType': 'text', + 'type': 'FormFieldInput', + }, ], } @@ -85,6 +95,15 @@ def test_inline_form_fields(): 'htmlType': 'number', 'type': 'FormFieldInput', }, + { + 'name': 'base64', + 'title': ['Base64'], + 'initial': 'ZmFzdHVp', + 'required': False, + 'locked': False, + 'htmlType': 'text', + 'type': 'FormFieldInput', + }, ], } @@ -92,11 +111,11 @@ def test_inline_form_fields(): async def test_simple_form_submit(): form_dep = fastui_form(SimpleForm) - request = FakeRequest([('name', 'bar'), ('size', '123')]) + request = FakeRequest([('name', 'bar'), ('size', '123'), ('base64', 'ZmFzdHVp')]) 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, 'base64': 'ZmFzdHVp\n'} async def test_simple_form_submit_repeat(): From 7ec0913fb971a461cf46adfbcf1365244436daaf Mon Sep 17 00:00:00 2001 From: Chaojie Date: Mon, 18 Dec 2023 17:55:45 +0800 Subject: [PATCH 2/2] Add more tests --- src/python-fastui/tests/test_forms.py | 29 ++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/python-fastui/tests/test_forms.py b/src/python-fastui/tests/test_forms.py index f9faec0a..a862be1b 100644 --- a/src/python-fastui/tests/test_forms.py +++ b/src/python-fastui/tests/test_forms.py @@ -14,7 +14,8 @@ class SimpleForm(BaseModel): name: str size: int = 4 - base64: Base64Str = 'ZmFzdHVp' + bytes_: bytes = b'fastui' + base64: Base64Str = 'fastui' class FakeRequest: @@ -55,10 +56,19 @@ 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': 'ZmFzdHVp', + 'initial': 'fastui', 'required': False, 'locked': False, 'htmlType': 'text', @@ -95,10 +105,19 @@ 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': 'ZmFzdHVp', + 'initial': 'fastui', 'required': False, 'locked': False, 'htmlType': 'text', @@ -111,11 +130,11 @@ def test_inline_form_fields(): async def test_simple_form_submit(): form_dep = fastui_form(SimpleForm) - request = FakeRequest([('name', 'bar'), ('size', '123'), ('base64', 'ZmFzdHVp')]) + request = FakeRequest([('name', 'bar'), ('size', '123')]) m = await form_dep.dependency(request) assert isinstance(m, SimpleForm) - assert m.model_dump() == {'name': 'bar', 'size': 123, 'base64': 'ZmFzdHVp\n'} + assert m.model_dump() == {'name': 'bar', 'size': 123, 'bytes_': b'fastui', 'base64': 'ZmFzdHVp\n'} async def test_simple_form_submit_repeat():