Skip to content

Commit ac63500

Browse files
authored
Merge pull request #121 from koxudaxi/support_form_with_request
Support form with Request
2 parents 5259805 + 42d8157 commit ac63500

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

fastapi_code_generator/parser.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,18 @@ def request(self) -> Optional[Argument]:
143143
)
144144
)
145145
self.imports.extend(data_type.imports_)
146-
146+
elif content_type == 'application/x-www-form-urlencoded':
147+
arguments.append(
148+
# TODO: support form with `Form()`
149+
Argument(
150+
name='request', # type: ignore
151+
type_hint='Request', # type: ignore
152+
required=True,
153+
)
154+
)
155+
self.imports.append(
156+
Import.from_full_path('starlette.requests.Request')
157+
)
147158
if not arguments:
148159
return None
149160
return arguments[0]

tests/data/expected/openapi/default_template/body_and_parameters/main.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from typing import List, Optional
88

99
from fastapi import FastAPI, Query
10+
from starlette.requests import Request
1011

1112
from .models import (
1213
Pet,
@@ -25,6 +26,14 @@
2526
)
2627

2728

29+
@app.post('/bar', response_model=None)
30+
def post_bar(request: Request) -> None:
31+
"""
32+
Create a bar
33+
"""
34+
pass
35+
36+
2837
@app.get('/foo', response_model=str)
2938
def get_foo(foo: Optional[str] = None) -> str:
3039
pass

tests/data/openapi/default_template/body_and_parameters.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,16 @@ paths:
213213
type: string
214214
parameters:
215215
- $ref: '#/components/parameters/MyParam'
216+
/bar:
217+
post:
218+
summary: Create a bar
219+
tags:
220+
- bar
221+
requestBody:
222+
content:
223+
application/x-www-form-urlencoded:
224+
schema:
225+
$ref: '#/components/schemas/PetForm'
216226
/user:
217227
get:
218228
tags:

0 commit comments

Comments
 (0)