How can I define a card with arbitrary keys in the Swagger model

How can I define a map with arbitrary keys in a Swagger model

Let's say I have the following internationalized model (in a Ruby-style pseudo-code that assumes using something like Globalize )

 class Thingy translates :name attribute :code end 

and my API wants to return something like

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

but I don't want to limit the range of translation keys in the actual API

I can spot in my swag doc

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

This is confirmed, but Swagger Codegen does not generate anything from additionalProperties and is not very explicit compared to somehow able to determine the type of map with a combination of required and arbitrary keys.

Anyone who works with internationalization will run into similar problems, so my question is, how do other people handle this scenario?

+7
internationalization rails-i18n swagger
source share
2 answers

This works under swagger-codegen-2.1.1-M1 (Java / JavaJaxRS) ... with Ron suggestions ...

YAML ...

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

creates a map with the attribute 'default' ...

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

Which, in turn, is built into 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; } } 
+8
source share

While the above definition is theoretically valid, it does not translate into what you are trying to describe and does not support Swagger.

To describe the desired structure, you will need the following definition:

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

While you can define an internal inline object as described above, I highly recommend that you internalize the definition and use $ref to refer to it from the translations definition.

As for the code generator, card support has been introduced recently, so it should work. If you find that this is not the case, please open the problem directly in the project containing an example Swagger definition to help with debugging.

+1
source share

All Articles