【问题标题】:How can I define a Map with arbitrary keys in a Swagger model如何在 Swagger 模型中定义具有任意键的 Map
【发布时间】:2015-06-22 08:57:36
【问题描述】:

如何在 Swagger 模型中使用任意键定义 map

假设我有以下国际化模型(在 Ruby 风格的伪代码中,假设使用类似 Globalize 的东西)

class Thingy
  translates :name
  attribute :code
end

我的 API 希望能够返回类似

{
  "thingy": {
    "code": "barn", 
    "translations": {
      "default": "barn", 
      "en": "barn", 
      "ru": "cарай", 
      "fr": "grange", 
      "nl": "schuur"
    }
  }
}

但我不想在实际 API 中限制翻译键的范围

我可以在我的招摇文档中定义

definitions:
  thingy:
    required:
      - code
    properties:
      code:
        type: string
    additionalProperties:
      translations:
        required:
          - default
        property:
          default:
            type: string
        additonalProperties: string

这可以验证,但 Swagger Codegen 不会从 additionalProperties 生成任何内容,并且与以某种方式能够定义一个包含必需键和任意键的 map 类型相比,它不是很明确。

任何从事国际化工作的人都会面临类似的问题,所以我的问题是,其他人是如何处理这种情况的?

【问题讨论】:

    标签: internationalization swagger rails-i18n


    【解决方案1】:

    这可以在 swagger-codegen-2.1.1-M1 (Java/JavaJaxRS) 下工作......在 Ron 的建议下......

    YAML ...

    translation:
      required:
        - default
      properties:
        default:
          type: string
      additionalProperties:
        type: string
    
    thingy:
      required:
        - code
      properties:
        code:
          type: string
        translations:
          $ref: '#/definitions/translation'
    

    创建一个带有“默认”属性的地图...

    public class Translation extends HashMap<String, String> {
    
        /**
         * 
         */
        @Expose
        private String _default = null;
    
        /**
         * @return  _default the _default
         */
        public String getDefault() {
            return _default;
        }
    
        /**
         * @param  _default to set
         */
        public void setDefault(String _default) {
            this._default = _default;
        }
    
    }
    

    这又嵌入到一个Thingy .....

    public class Thingy  {
    
        /**
         * 
         */
        @Expose
        private String code = null;
    
        /**
         * 
         */
        @Expose
        private Translation translations = null;
    
        /**
         * @return  code the code
         */
        public String getCode() {
            return code;
        }
    
        /**
         * @param  code to set
         */
        public void setCode(String code) {
            this.code = code;
        }
    
        /**
         * @return  translations the Translations
         */
        public Translation getTranslations() {
            return translations;
        }
    
        /**
         * @param  translations the Translations to set
         */
        public void setTranslations(Translation translations) {
            this.translations = translations;
        }
    
    }
    

    【讨论】:

    • 这正是我最终得到的结果——感谢 Ron 和你的帮助。
    【解决方案2】:

    虽然上面的定义在理论上是有效的,但它并不能转化为您要描述的内容,也不是 Swagger 真正支持的。

    为了描述您想要的结构,您需要以下定义:

    thingy:
      type: object
      required:
        - code
      properties:
        code:
          type: string
        translations:
          type: object
          required:
              - default
          properties:
              default:
                type: string
          additonalProperties: 
              type: string
    

    虽然您可以像上面一样内联定义内部对象,但我强烈建议您将定义外部化并使用$reftranslations 定义中引用它。

    至于代码生成器,最近引入了对地图的支持,因此它应该可以工作。如果您发现没有,请直接在项目上打开一个包含示例 Swagger 定义的问题以帮助调试。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    • 2019-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    相关资源
    最近更新 更多