Skip to content

Instantly share code, notes, and snippets.

@davesag
Last active August 29, 2015 14:19
Show Gist options
  • Save davesag/c8cf393f1a14d4df757e to your computer and use it in GitHub Desktop.
Save davesag/c8cf393f1a14d4df757e to your computer and use it in GitHub Desktop.
Defining a map of arbitrary translation keys in Swagger 2.0
swagger: '2.0'
info:
version: "0.0.1"
title: Localised Thingy demonstration
definitions:
translation:
required:
- default
properties:
default:
type: string
additionalProperties:
type: string
thingy:
required:
- code
properties:
code:
type: string
translations:
$ref: '#/definitions/translation'
localisedThingy:
required:
- code
- name
properties:
code:
type: string
description: A locale code
pattern: '^[a-z]{2}(\|[a-z]+)?$'
name:
type: string
description: The localised name
paths:
/thingies:
get:
description: |-
Gets `Thingy` objects.
responses:
# Response code
200:
description: A bunch of `Thingies`
schema:
type: array
items:
$ref: '#/definitions/thingy'
/{locale}/thingies:
get:
description: |-
Gets correctly localised `Thingies`
parameters:
- name: locale
in: path
type: string
required: true
default: default
responses:
200:
description: A bunch of localised `Thingies`
schema:
type: array
items:
$ref: '#/definitions/localisedThingy'
@davesag
Copy link
Author

davesag commented Apr 16, 2015

Updated based on suggestions by Ron in SO.

@webron
Copy link

webron commented Apr 16, 2015

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

should be

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

@davesag
Copy link
Author

davesag commented Apr 16, 2015

Fair enough - i think in my original schema the translations set was optional but it doesn't actually make sense that it be optional. I'll edit. Ta.

@jreece1567
Copy link

Updated version (w/webron's corrections) works with swagger-codegen-2.1.1-M1 (Java/JavaJaxRS) ... makes a Map<String,String> for 'translation' and embeds that as a property of thingy ...

@davesag
Copy link
Author

davesag commented Apr 16, 2015

That's excellent news @jreece1567

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment