zenocon
zenocon

Reputation: 1657

Model composition in swagger spec without additional nesting layer

If I have the following example where Settings definition is embedded in Thing via composition:

definitions:
  Settings:
    properties: 
      foobar:
        type: number 
        format: double
      boofar:
        type: string 
  Thing:
    properties:
      allOf:
        $ref: '#/definitions/Settings'
      name:
        type: string

If I define a method to POST a Thing in editor.swagger.io, it ends up constructing JSON that looks like this:

{
  settings: {
    foobar: 1,
    boofar: "text here"
  },
  name: "some name"
}

I want to embed a model definition with composition but without the additional nested property definition -- is this possible? This is the JSON structure I would like to have for Thing:

{
  foobar: 1,
  boofar: "text here",
  name: "some name"
}

Is there a way to achieve this?

Upvotes: 0

Views: 194

Answers (1)

Arnaud Lauret
Arnaud Lauret

Reputation: 5331

Your example do not really use composition as allOf is a property.

allOf is supposed to be on the root of the definition and it's an array of schema (reference or inline).

Here's the proper way to use allOf for your example:

swagger: '2.0' info: title: API version: 1.0.0

paths: 
  /thing:
    get:
      responses:
        200:
          description: OK
          schema:
            $ref: '#/definitions/Thing'

definitions:
  Settings:
    properties: 
      foobar:
        type: number 
        format: double
      boofar:
        type: string 
  Thing:
    allOf:
      - $ref: '#/definitions/Settings'
      - properties:
          name:
            type: string

Resulting rendering in SwaggerUI:

enter image description here

Upvotes: 1

Related Questions