当前位置: 首页 > 知识库问答 >
问题:

引用多个预定义的swagger 2.0参数定义抛出错误

高玮
2023-03-14

我在swagger 2.0文件中定义了两个可重用参数:

parameters:
  cursorParam:
    name: cursor
    in: query
    description: Pagination cursor.
    required: false
    type: string       
  limitParam:
    name: limit
    in: query
    description: Result limiter.
    required: false
    type: integer 

然后在我的路由定义中(在本例中为GET),我尝试这样引用这两个:

parameters:
    - $ref: '#/parameters/cursorParam'
    - $ref: '#/parameters/limitParam'  

如果我使用一个$ref,它似乎工作正常(没有编译错误),但是如果我尝试两个都使用,它会看着我:

Operation parameter already defined: undefined
at paths ▹ /users ▹ get ▹ parameters ▹ 1 ▹ name

Operation parameter already defined: undefined
at paths ▹ /authorisations ▹ get ▹ parameters ▹ 1 ▹ name

此外,如果我在查询字符串中传递这些参数并输出<code>req.swagger。params</code>,我得到一个空对象。我做错了什么?

这是规范的完整SSCCE,生成相同的错误:

swagger: "2.0"
info:
  version: "0.0.1"
  title: Test
# during dev, should point to your local machine
host: localhost
# basePath prefixes all resource paths 
basePath: /
# 
schemes:
  # tip: remove http to make production-grade
  - http
  - https
# format of bodies a client can send (Content-Type)
consumes:
  - application/json
# format of the responses to the client (Accepts)
produces:
  - application/json
x-a127-config: {}
x-volos-resources: {}
paths:
  /authorisations:
    x-swagger-router-controller: authorizations
    x-volos-authorizations: {}
    x-volos-apply: {}
    get:
      description: Returns all authorizations. Requires administrator rights to view unfiltered results, or if authenticated, returns the authorization entity belonging to the OAuth token.
      operationId: authorizationsGetAll #controller method name
      parameters:
        - $ref: '#/parameters/cursorParam'
        - $ref: '#/parameters/limitParam'
      responses:
        200:
          description: OK

# reusable parameters have parameter definitions
parameters:
  cursorParam:
    name: cursor
    in: query
    description: Pagination cursor passed to a BaaS collection. Note that if this parameter is present, lastAccessedTimestamp.{channelType} should not be updated.
    required: false
    type: string       
  limitParam:
    name: limit
    in: query
    description: Result limiter to be passed to a BaaS collection.
    required: false
    type: integer         

共有1个答案

乐正峰
2023-03-14

您似乎使用的是旧版本的 a127。最近修复了 Swagger 文档所有部分缺乏参考支持的问题,因此更新应该可以解决此问题。

 类似资料:
  • 我没有正确安装吗?我忽略了什么?

  • 试图按照官方手册实现一个模块时,我收到了以下错误消息: 但在我的代码中没有任何地方使用过这个名称。 我该怎么解决这个?

  • 我需要创建一个jpa自定义查询,使用几个表上的联接来获取记录。 以下是我想要达到的目标: 对很少的参数进行数据排序(在运行时决定) 使用where子句进行筛选(在运行时决定) 示例: @query(value=“从用户a中选择a.name,b.city,c.reason在a.id=b.id上连接地址b在a.id=c.id上连接测试c 我无法为其创建常规查询。 任何其他的方法对我来说也是可以接受的来

  • scalac抱怨第一个实例化(对重载定义的引用不明确),因为单个参数和varargs方法都是相同特定的。 搜索stackoverflow时,我发现强制使用单参数方法是可能的。将使编译器使用。 我的问题是为什么第二个实例化匹配而没有额外的“提示”?换句话说,为什么双参数方法比varargs方法更具体,而单参数方法不是?

  • 我正在开发一个API,需要抛出和异常,如下所示 如果sort by参数不是我预先确定的值之一,我有几个参数来做这件事,因为这里是我的控制器的样子 我删除了url,但它可以对传入的数据进行排序,但我需要验证并抛出正确的错误,我真的不知道如何以所需的格式执行,如json,欢迎任何帮助

  • 你可以使用这些标准预定义自动向开发版本的标题添加构建时间,以及向版本号添加日期,等等。 5.2.1 ${__COUNTER__} 扩展到一个数字 (每次使用时都从0开始,并且递增1) 5.2.2 ${__FILE__} 当前脚本名称。 5.2.3 ${__FILEDIR__} 当前脚本目录。 5.2.4 ${__LINE__} 当前行数。 5.2.5 ${__DATE__} 当脚本开始编译时当前时