Flask-restplus:如何使用“allOf”操作定义嵌套模型?

     2023-02-19     274

关键词:

【中文标题】Flask-restplus:如何使用“allOf”操作定义嵌套模型?【英文标题】:Flask-restplus: how to define a nested model with 'allOf' operation? 【发布时间】:2018-03-10 02:00:04 【问题描述】:

创建一个 python flask rest plus 服务器应用程序, 我正在尝试使用 'allOf' 运算符为输入主体(在 POST 操作中)创建模型, 这等效于以下示例,取自我使用 swagger 编辑器创建的 swagger.yaml:

definitions:
  XXXOperation:
    description: something...
    properties:
      oper_type:
      type: string
        enum:
          - oper_a
          - oper_b
          - oper_c
      operation:
        allOf:
          - $ref: '#/definitions/OperA'
          - $ref: '#/definitions/OperB'
          - $ref: '#/definitions/OperC'

应该是这样的(只是在我疯狂的想象中):

xxx_oper_model = api.model('XXXOperation', 
    'oper_type': fields.String(required=True, enum['oper_a', 'oper_b', 'oper_c']),
    'operation': fields.Nested([OperA, OperB, OperC], type='anyof')
)

当 OperA、OperB、OperC 也被定义为模型时。 我该怎么做?

实际上,我更喜欢使用 'oneOf',但据我所知,即使在 swagger 编辑器中也不支持它,所以我尝试将 'allOf' 与非必填字段。

版本:flask restplus:0.10.1,flask:0.12.2,python:3.6.2

非常感谢

【问题讨论】:

【参考方案1】:

您需要使用api.inherit。如documentation示例第30页所述;

parent = api.model('Parent', 
  'name': fields.String,
  'class': fields.String(discriminator=True)
)

child = api.inherit('Child', parent, 
  'extra': fields.String
)

这样,Child 将拥有 Parent 的所有属性 + 自己的附加属性 extra


  "Child": 
    "allOf": [
      
        "$ref": "#/definitions/Parent"
      ,
      
        "properties": 
          "extra": 
            "type": "string"
          
        
      
    ]
  

【讨论】:

flask-restplus知识点

资料网址官方搜索页(搜索比较方便)https://flask-restplus.readthedocs.io/en/latest/search.html官方文档https://flask-restplus.readthedocs.io/en/latest/index.htmlgithubhttps://github.com/noirbizarre/flask-restplus 查看详情

flask学习-41.flask-restplus入门到放弃(代码片段)

前言Flask-restfull是flask框架开发接口的一个框架,Flask-RESTPlus是Flask-restfull升级版,功能做了一些优化,主要是可以生成swagger在线文档了。环境准备先安装Flask-RESTPlus插件pipinstallflask-restplus目前最新版本v0.13.0官方文档地... 查看详情

JSON 模式验证 oneOf 两个或 allOf

...-06-0217:09:12【问题描述】:我想验证以下json架构,我正在使用Ajvnpm包。"email":"xyzmail@gmail.com","phone":"1112223334","country_code":"91"我只想要email,或者只想要phone和country_cod 查看详情

CompletableFuture.allOf().orTimeout() 的意外行为

】CompletableFuture.allOf().orTimeout()的意外行为【英文标题】:UnexpectedBehaviorforCompletableFuture.allOf().orTimeout()【发布时间】:2021-02-2416:12:33【问题描述】:有两种方法可以强制CompletableFuture在给定时间后超时:orTimeout(longtimeout,TimeUnitunit)... 查看详情

JSON 模式:“allof”和“additionalProperties”

】JSON模式:“allof”和“additionalProperties”【英文标题】:JSONschema:"allof"with"additionalProperties"【发布时间】:2014-05-0613:28:18【问题描述】:假设我们有模式跟随模式(来自教程here):"$schema":"http://json-schema.org/draft-0... 查看详情

带有集合或列表的 Java 8 CompletableFuture.allOf(...) [重复]

】带有集合或列表的Java8CompletableFuture.allOf(...)[重复]【英文标题】:Java8CompletableFuture.allOf(...)withCollectionorList[duplicate]【发布时间】:2016-06-1900:02:06【问题描述】:Java8有一个函数CompletableFuture.allOf(CompletableFuture<?>... 查看详情

传递给 CompletableFuture.allOf() 的所有期货都会运行吗?

】传递给CompletableFuture.allOf()的所有期货都会运行吗?【英文标题】:WillallfuturespassedtoCompletableFuture.allOf()run?【发布时间】:2019-06-1705:36:40【问题描述】:所以我有几个希望运行的未来,即使有些失败我希望所有人都有机会运行... 查看详情

javacompletablefuture:allof等待所有异步线程任务结束

privatevoidmethod()throwsExecutionException,InterruptedException{CompletableFuture<String>f1=CompletableFuture.supplyAsync(()->{try{TimeUnit.SECONDS.sleep(3);}catch(InterruptedExceptione){e.p 查看详情

flask学习-49.flask-restx使用namespaces命名空间(代码片段)

前言本页介绍了构建一个稍微复杂的Flask-RESTPlus应用程序,该应用程序将涵盖在设置实际基于Flask-RESTPlus的API时的一些最佳实践。多个namespaces命名空间组织Flask-RESTPlus应用程序有很多不同的方法,但在这里我们将描述一种... 查看详情

使用 espresso 设置 Spinner 项目

...sso【发布时间】:2017-01-1017:53:55【问题描述】:我想知道如何在浓缩咖啡测试中设置微调器中的项目。onView(withId(R.id.spinner_gender)).perform(click());onData(allOf(is(instanceOf(String.class)))).atPosition(0 查看详情

flask学习-42.flask-restx快速入门(代码片段)

前言Flask-restfull是flask框架开发接口的一个框架,Flask-RESTPlus是Flask-restfull升级版,可以生成swagger在线文档了。但是Flask-RESTPlus这个项目不再维护了,迁移到Flask-RESTX了。Flask-RESTX与Flask-RESTPlus的API保持100%兼容。环境安装... 查看详情

如何在 Kiwi 中为模拟方法设置参数期望

】如何在Kiwi中为模拟方法设置参数期望【英文标题】:HowtosetexpectationsonparameterstomockedmethodsinKiwi【发布时间】:2013-03-0702:59:22【问题描述】:使用OCMockito和OCHamcrest,我可以对模拟方法的参数设置期望,因此:[verify(aMockObject)doSome... 查看详情

completablefuture异步编排(多任务组合)(代码片段)

目录一、CompletableFuture源码中多任务组合的相关方法二、allOf方法代码示例三、anyOf方法代码示例一、CompletableFuture源码中多任务组合的相关方法CompletableFuture源码中多任务组合的相关方法二、allOf方法代码示例allOf方法:等待... 查看详情

completablefuture异步编排(多任务组合)(代码片段)

目录一、CompletableFuture源码中多任务组合的相关方法二、allOf方法代码示例三、anyOf方法代码示例一、CompletableFuture源码中多任务组合的相关方法CompletableFuture源码中多任务组合的相关方法二、allOf方法代码示例allOf方法:等待... 查看详情

如何定义至少需要许多属性之一的 JSON 模式

】如何定义至少需要许多属性之一的JSON模式【英文标题】:HowtodefineaJSONschemathatrequiresatleastoneofmanyproperties【发布时间】:2015-10-2815:30:56【问题描述】:我想知道我是否可以定义一个JSON模式(草案4),该模式至少需要一个对象的... 查看详情

junit

...没问题的。JUnit4HelloWorldnewproject建立类建立testcaseassertThat使用hamcrest的匹配方法放弃旧的断言,使用hamcrest断言a) 更自然示例a) assertThat(n,allOf(greaterThan(1),lessTha 查看详情

如何在 Espresso 测试中单击快餐栏按钮?

】如何在Espresso测试中单击快餐栏按钮?【英文标题】:HowtoclickthesnackbarbuttoninEspressotesting?【发布时间】:2016-01-2715:42:54【问题描述】:我不认为这是一个愚蠢的问题。我正在编写一个简单的Espresso测试,其中一部分涉及单击快... 查看详情

Espresso - 在列表视图中按文本点击

...列表视图中单击文本。我知道他们有thisguide,但我不知道如何通过查找文本来完成这项工作。这是我尝试过的Espresso.onData(Matchers.allOf(Matchers.is(Matchers.instance 查看详情