question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: does swagger spec support root nodes

See original GitHub issue

Hey there,

I am writing documentation for rails application. And I am struggling with root nodes in responses.

Here is the example to describe what I mean by that.

GET /orders/ # get all orders

{
  "orders": [
    {
      "id": 28,
      "customer_name": "Peter"
    },
    {
      "id": 29,
      "customer_name": "Tomas"
    }
  ]
}

I came up with the workaround and so far I was creating artificial model Orders, that reference real model Order.

paths:
  /orders:
    get:
      summary: Get all orders
      description: Get All orders
      responses:
        '200':
          description: An array of orders inside root node
          schema:
            type: object
            $ref: '#/definitions/Orders'

definitions:
  Orders:
    type: object
    required:
      - orders
    properties:
      orders:
        type: array
        items:
          $ref: '#/definitions/Order'

  Order:
    type: object
    properties:
      id:
        type: integer
        description: Unique identifier representing a specific order
      customer_name:
        type: string
        description: The customer that the order is for

But I guess there has to be better way to do it. Because this is not the only endpoint (for instance /order/:id has root node ‘order’ etc.). So I will have to have a lot of artificial models. So my question is.

How am I supposed to write specs for responses that contains root node?

Thank you for any ideas.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jharmncommented, Jan 8, 2016

This seems to fit in with #519. The ability to define a “collection” wrapper, which can be reused for various resources avoids creating bespoke ‘wrappers’ for each collection in a CRUD resource pattern.

That said, this example design is somewhat flawed from taking advantage from that type of approach…if orders were items, this would be a reusable collection format.

1reaction
fehguycommented, Dec 11, 2015

Really not sure what the question is, apologies. You are going to always want to describe your Order object. If it’s stuffed inside another object, you have two choices:

  1. Define a holding object, if it’s going to be reused across many places. That’s what is described in the first post with the Orders holder, which has an array of type order
  2. Define the response in-line so that the order attribute points to the Order array:
responses:
  200:
    description: the order you wanted
    schema:
      type: object
      properties:
        order:
          type: array
          items:
            $ref: '#/definintions/Order'
Read more comments on GitHub >

github_iconTop Results From Across the Web

OpenAPI Specification - Version 3.0.3 - Swagger
It is RECOMMENDED that the root OpenAPI document be named: openapi.json or openapi.yaml . Data Types. Primitive data types in the OAS are...
Read more >
Include root element in Swagger JSON sample - Stack Overflow
I was facing the same issue with openApi swagger. I've fixed it by adding a OpenApiCustomiser :
Read more >
Swagger UI tutorial | Documenting APIs - Idratherbewriting.com
Swagger UI provides a display framework that reads an OpenAPI specification document and generates an interactive documentation website.
Read more >
OpenAPI (Swagger) | NestJS - A progressive Node.js framework
The OpenAPI specification is a language-agnostic definition format used to describe RESTful APIs. Nest provides a dedicated module which allows generating ...
Read more >
Accessing the Swagger Visualization Portal
Access the Swagger Visualization Portal on the API Catalog page. ... avoid a "Failed to load specification" error, rename the default root node...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found