具有负载和查询参数的 Flask-restx

问题描述 投票:0回答:1

我有一个 API put() 调用,需要路径参数、查询参数和 JSON 负载。由于各种原因,我们没有对整个有效负载进行建模,我们只想要一个建模的外部 JSON 信封,并且使用 fields.Raw() 而不进行子类化允许此工作。


问题是如何使用 @api.expect() 来指定有效负载和查询参数解析器。 下面的代码片段显示了我们当前的方法 - 在类上使用 .doc() 装饰器指定查询参数,然后在 put() 调用上使用 .expect() 装饰器指定有效负载参数。我尝试过的所有其他操作都会导致 Swagger 不显示其中一个参数。


我的问题是:这种 split 方法是实现此目的的正确方法吗?还是有办法将两种参数类型滚动到单个 .expect() 调用中?


class WBRTRoutes():

    @staticmethod
    def bind(api):
        # create a namespace
        ns_workareas = api.namespace("workareas", description = "Registered workareas")
            
        # create a model for a wrapped "agnostic" payload 
        # I just want to model the wrapper, not the detailed content
        mod_workarea_update = api.model("WorkareaUpdate", {
            "workarea":  fields.Raw(description = "the workarea object")
            })

        # create a model for "internal" response content - a list of strings
        mod_details = api.model("Details", {
            "details": fields.List(fields.String)
            })

        # for consisitency between calls, all returned responses are wrapped in a message/data envelope
        resp_version = api.model("RspVersion", {
            "message":   fields.String(description = "used to return message to the caller"),
            "data":      fields.Nested(mod_version, description = "the incremented version after an update", skip_none = True)
            })
            
        # create a non-parser params dict (instead of more common RequestParser)
        userid_route_params = {
            'workid' : 'The internal workarea identifier'
            }

        # there is a also a path param, which we specify in the .route decorator
        @ns_workareas.route('/<workid>')
        @api.doc(
          description='Represents a registered workarea by its workid',
          # MUST SPECIFY QUERY PARAMS HERE (USING SIMPLE DICT) IF PAYLOAD PARAMS ALSO EXPECTED
          params=userid_route_params)
        class Workarea(Resource):
            # CANNOT SPECIFY QUERY PARAMS HERE (USING PARSER) IF PAYLOAD PARAMS ALSO EXPECTED
            @api.expect(mod_workarea_update, validate=True)
            @api.marshal_with(resp_version)
            @api.response(200, 'Success')
            @api.response(403, 'Forbidden')
            @api.response(404, 'Resource not found')
            def put(self, workid):
                """Performs an undoable update to current a workarea, after checking for interleaved updates"""
                pass

python flask-restx
1个回答
0
投票

发布问题后,我了解到可以将多个模型指定为@api.expect(),作为单独的参数。 我仍然不明白 @api.doc(...,params=) 和 @api.expect() 之间的重叠,但这可以再等一天。

@api.expect(WBRTModels.mod_workarea_update, WBRTParsers.args_update, validate=True)
© www.soinside.com 2019 - 2024. All rights reserved.