Swagger:在哈希错误中表示枚举属性 [英] Swagger: Representing enum property in hash error

查看:251
本文介绍了Swagger:在哈希错误中表示枚举属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在处理Ruby on Rails API的Swagger文档.该API有很多枚举器(枚举),这些枚举器包含在各种模型中.枚举存储为哈希,而不存储为数组app/models/concerns目录中,以便以后可以对其进行修改而不会出现问题.

I am currently working on a Swagger documentation for Ruby on Rails API. The API has lots of enumerators (enums) which are included in various models. The enums are stored as hashes and not arrays in the app/models/concerns directory so that they can be modified without issues later.

状态枚举(state.rb)

module State
  extend ActiveSupport::Concern
  included do
    enum state: { state1: 'State 1',
                  state2: 'State 2',
                  state3: 'State 3',
                  state4: 'State 4',
                  state5: 'State 5' }
  end
end

但是,当我尝试在Swagger的组件模式中表示这种情况时:

However, when I try to represent this in a component schema in Swagger like this:

components:
  schemas:
    State:
      type: object
      properties:
        enum: { state1: 'State 1',
                state2: 'State 2',
                state3: 'State 3',
                state4: 'State 4',
                state5: 'State 5' }

我得到一个错误:

不应具有其他属性

should not have additional properties

状态1:状态1"

state2:状态2"

state2: 'State 2'

state3:状态3"

state3: 'State 3'

state4:状态4"

state4: 'State 4'

状态5:状态5"

我想用哈希而不是数组表示枚举.有什么解决方法可以使我正常工作吗?谢谢.

I want to represent the enums in hashes and not in arrays. Is there any workaround I can get to make this work? Thank you.

推荐答案

我终于想出了一种方法来完成它. 此解决方案适用于OpenAPI 3 – OpenAPI规范的最新版本,是回答此问题的要点.

I finally figured out a way to get it done. This solution applies to OpenAPI 3 – the latest version of the OpenAPI Specification as the point of answering this question.

这是我的做法:

解决方案1 ​​

components:
  schemas:
    State:
      type: object
      additionalProperties:
        type: string
      example:
        state1: State 1
        state2: State 2
        state3: State 3
        state4: State 4
        state5: State 5

这会将整个哈希传递到请求的响应主体中,因此会引发错误

This was passing the entire hash into the response body of a request, and thus it was throwing errors

解决方案2:

另一种方法是将它们表示为数组,这不是我的理想解决方案,但是它允许Swagger从数组中仅选择一项以传递到请求的响应主体中.但是,我要注意,枚举是散列,并且我必须在客户端对枚举进行collection_select hashes的操作.

Another way is to represent them as arrays, which was not my ideal solution, but it allowed Swagger to select only one item from the array to pass into the response body of a request. I would, however, take note that the enums are hashes and I will have to do a collection_select of hashes for the enums on my client-side.

components:
  schemas:
    State:
      type: string
      description: List of States
      enum:
        - State 1
        - State 2
        - State 3
        - State 4
        - State 5

最后,无论您选择哪种解决方案,都可以在其他模型中引用它们,如下所示:

Lastly, whichever solution you choose you can then reference them in other models like this:

components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: integer
          format: int64
        first_name:
          type: string
        last_name:
          type: string
        password:
          type: string
          format: password
        state:
          $ref: '#/components/schemas/State'

以下是Swagger文档的链接:字典,HashMap和关联数组

Here's a link to the Swagger Documentation: Dictionaries, HashMaps and Associative Arrays

仅此而已.

我希望这会有所帮助

这篇关于Swagger:在哈希错误中表示枚举属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆