Skip to content

Commit 0c24fa6

Browse files
authored
fix arguments validation. (#20)
1 parent 1f0f240 commit 0c24fa6

File tree

4 files changed

+103
-4
lines changed

4 files changed

+103
-4
lines changed

CHANGES.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## Version 0.12.1
2+
3+
* Fix arguments validation.
4+
5+
* ## Version 0.12.0
6+
7+
* Validating and serializing arguments from `request.view_args` to `request.first_view_args`.
8+
* Validating and serializing arguments from `request.args` to `request.first_args`.
9+
* Validating and serializing arguments from `request.cookies` to `request.first_cookies`.
10+
111
## Version 0.11.0
212

313
* Add using marshmallow schemas for validation and serialization data.

src/flask_first/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from .schema.tools import convert_schemas
3131
from .schema.tools import resolving_refs
3232

33-
__version__ = '0.12.0'
33+
__version__ = '0.12.1'
3434

3535

3636
class First:
@@ -136,10 +136,14 @@ def _resolved_params(self, payload: MultiDict) -> dict:
136136

137137
def _arg_to_list(self, args: dict, schema_fields: dict) -> dict:
138138
for arg in args:
139-
if isinstance(schema_fields[arg], marshmallow.fields.List) and not isinstance(
140-
args[arg], list
141-
):
139+
arg_value = schema_fields.get(arg, ...)
140+
141+
if arg_value is ...:
142+
continue
143+
144+
if isinstance(arg_value, marshmallow.fields.List) and not isinstance(args[arg], list):
142145
args[arg] = [args[arg]]
146+
143147
return args
144148

145149
def _registration_swagger_ui_blueprint(self, swagger_ui_path: str | Path) -> None:

tests/specs/v3.0/args.openapi.yaml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
openapi: 3.0.3
2+
info:
3+
title: Mini API for testing Flask-First
4+
version: 1.0.0
5+
paths:
6+
/parameters_endpoint:
7+
parameters:
8+
- name: exist_arg
9+
in: query
10+
schema:
11+
type: string
12+
get:
13+
operationId: mini_endpoint
14+
responses:
15+
"200":
16+
description: OK
17+
content:
18+
application/json:
19+
schema:
20+
type: object
21+
properties:
22+
non_exist_arg:
23+
type: string
24+
"400":
25+
description: Bad Request
26+
content:
27+
application/json:
28+
schema:
29+
$ref: "#/components/schemas/ERROR"
30+
components:
31+
schemas:
32+
ERROR:
33+
type: object
34+
required:
35+
- code
36+
- name
37+
- description
38+
additionalProperties: false
39+
properties:
40+
code:
41+
type: integer
42+
minimum: 400
43+
maximum: 599
44+
example: 400
45+
name:
46+
type: string
47+
example: "Bad Request"
48+
description:
49+
type: string
50+
example: "Request data not valid."

tests/test_args.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from pathlib import Path
2+
3+
from flask import Flask
4+
from flask import request
5+
from flask_first import First
6+
7+
from .conftest import BASEDIR
8+
9+
10+
def test_specification__non_exist_args():
11+
def mini_endpoint() -> dict:
12+
args = request.first_args
13+
assert args.get('non_exist_arg') is None
14+
15+
return {'non_exist_arg': None}
16+
17+
first = First(Path(BASEDIR, 'specs/v3.0/args.openapi.yaml'))
18+
19+
def create_app():
20+
app = Flask('non_exist_args')
21+
app.debug = 1
22+
app.testing = 1
23+
app.config['FIRST_RESPONSE_VALIDATION'] = True
24+
first.init_app(app)
25+
first.add_view_func(mini_endpoint)
26+
return app
27+
28+
app = create_app()
29+
30+
with app.test_client() as test_client:
31+
r = test_client.get(
32+
'/parameters_endpoint',
33+
query_string={'non_exist_arg': 'NON_EXIST_ARGS'},
34+
)
35+
assert r.status_code == 400

0 commit comments

Comments
 (0)