22import copy
33
44
5- def request_schema (schema , location = None , put_into = None , example = None , add_to_refs = False , ** kwargs ):
5+ # locations supported by both openapi and webargs.aiohttpparser
6+ VALID_SCHEMA_LOCATIONS = (
7+ "cookies" ,
8+ "files" ,
9+ "form" ,
10+ "headers" ,
11+ "json" ,
12+ "match_info" ,
13+ "path" ,
14+ "query" ,
15+ "querystring" ,
16+ )
17+
18+
19+ def request_schema (schema , location = "json" , put_into = None , example = None , add_to_refs = False , ** kwargs ):
620 """
721 Add request info into the swagger spec and
822 prepare injection keyword arguments from the specified
@@ -29,7 +43,7 @@ async def index(request):
2943 'id': data['id']})
3044
3145 :param schema: :class:`Schema <marshmallow.Schema>` class or instance
32- :param locations : Default request locations to parse
46+ :param location : Default request locations to parse
3347 :param put_into: name of the key in Request object
3448 where validated data will be placed.
3549 If None (by default) default key will be used
@@ -39,19 +53,14 @@ async def index(request):
3953 Otherwise add example to endpoint.
4054 Default False
4155 """
56+
57+ if location not in VALID_SCHEMA_LOCATIONS :
58+ raise ValueError (f"Invalid location argument: { location } " )
59+
4260 if callable (schema ):
4361 schema = schema ()
44-
45- # Compatability with old versions should be dropped,
46- # multiple locations are no longer supported by a single call
47- # so therefore **locations should never be used
4862
4963 options = {"required" : kwargs .pop ("required" , False )}
50- # to support apispec >=4 need to rename default_in
51- if location :
52- options ["default_in" ] = location
53- elif "default_in" not in options :
54- options ["default_in" ] = "body"
5564
5665 def wrapper (func ):
5766 if not hasattr (func , "__apispec__" ):
@@ -61,14 +70,17 @@ def wrapper(func):
6170 _example = copy .copy (example ) or {}
6271 if _example :
6372 _example ['add_to_refs' ] = add_to_refs
64- func .__apispec__ ["schemas" ].append ({"schema" : schema , "options" : options , "example" : _example })
73+ func .__apispec__ ["schemas" ].append (
74+ {"schema" : schema , "location" : location , "options" : options , "example" : _example }
75+ )
76+
6577 # TODO: Remove this block?
66- if location and "body" in location :
67- body_schema_exists = (
68- "body" in func_schema [ "location" ] for func_schema in func . __schemas__
69- )
70- if any ( body_schema_exists ):
71- raise RuntimeError ("Multiple body parameters are not allowed" )
78+ # "body" location was replaced by "json" location
79+ if (
80+ location == "json" and
81+ any ( func_schema [ "location" ] == "json" for func_schema in func . __schemas__ )
82+ ):
83+ raise RuntimeError ("Multiple json locations are not allowed" )
7284
7385 func .__schemas__ .append ({"schema" : schema , "location" : location , "put_into" : put_into })
7486
0 commit comments