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

Dav*_*Sag 8 internationalization swagger rails-i18n

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

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

class Thingy
  translates :name
  attribute :code
end
Run Code Online (Sandbox Code Playgroud)

我的API希望能够返回类似的东西

{
  "thingy": {
    "code": "barn", 
    "translations": {
      "default": "barn", 
      "en": "barn", 
      "ru": "c????", 
      "fr": "grange", 
      "nl": "schuur"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

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

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

definitions:
  thingy:
    required:
      - code
    properties:
      code:
        type: string
    additionalProperties:
      translations:
        required:
          - default
        property:
          default:
            type: string
        additonalProperties: string
Run Code Online (Sandbox Code Playgroud)

这证实了,但Swagger Codegen不会生成任何东西,additionalProperties并且它不是非常明确的,相比之下能够定义map一个混合了必需和任意键的类型.

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

小智 9

这可以在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'
Run Code Online (Sandbox Code Playgroud)

使用"默认"属性创建地图...

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

反过来又嵌入了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;
    }

}
Run Code Online (Sandbox Code Playgroud)