Skip to content

Commit d429690

Browse files
zhzLuke96freddyaboultongradio-pr-bot
authored
Fix request serialization for fastapi /docs (#8530)
* Fix request serialization for fastapi `/docs` * fix issues identified in code review * unit test * add changeset --------- Co-authored-by: freddyaboulton <[email protected]> Co-authored-by: gradio-pr-bot <[email protected]>
1 parent d43d696 commit d429690

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

.changeset/dull-lizards-suffer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gradio": patch
3+
---
4+
5+
fix:Fix request serialization for fastapi `/docs`

gradio/data_classes.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,25 @@ class PredictBody(BaseModel):
110110
None # dictionary of request headers, query parameters, url, etc. (used to to pass in request for queuing)
111111
)
112112

113+
@classmethod
114+
def __get_pydantic_json_schema__(cls, core_schema, handler):
115+
return {
116+
"title": "PredictBody",
117+
"type": "object",
118+
"properties": {
119+
"session_hash": {"type": "string"},
120+
"event_id": {"type": "string"},
121+
"data": {"type": "array", "items": {"type": "object"}},
122+
"event_data": {"type": "object"},
123+
"fn_index": {"type": "integer"},
124+
"trigger_id": {"type": "integer"},
125+
"simple_format": {"type": "boolean"},
126+
"batched": {"type": "boolean"},
127+
"request": {"type": "object"},
128+
},
129+
"required": ["data"],
130+
}
131+
113132

114133
class ResetBody(BaseModel):
115134
event_id: str

test/test_routes.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,3 +1320,19 @@ def test_max_file_size_used_in_upload_route(connect):
13201320
with open("test/test_files/alphabet.txt", "rb") as f:
13211321
r = test_client.post("/upload", files={"files": f})
13221322
assert r.status_code == 200
1323+
1324+
1325+
def test_docs_url():
1326+
with gr.Blocks() as demo:
1327+
num = gr.Number(value=0)
1328+
button = gr.Button()
1329+
button.click(lambda n: n + 1, [num], [num])
1330+
1331+
app, _, _ = demo.launch(app_kwargs={"docs_url": "/docs"}, prevent_thread_lock=True)
1332+
try:
1333+
test_client = TestClient(app)
1334+
with test_client:
1335+
r = test_client.get("/docs")
1336+
assert r.status_code == 200
1337+
finally:
1338+
demo.close()

0 commit comments

Comments
 (0)