twskj / pretty-swag

Pretty UI for Swagger spec
MIT License
122 stars 20 forks source link

HTML output does show nothing for map object types #49

Closed joanlofe closed 6 years ago

joanlofe commented 6 years ago

When defining a property of a type as a map, the generated html code shows nothing (empty braces) for that property. However, in swagger editor it does show correctly as expected. I am using the version from the last commit in the master branch of the repository (commit 9ceda7).

At the end I include an example swagger definition that reproduces the error.

This is how it shows in the output of pretty-swag (empty-schema):

And this how it shows in the render of swagger-editor:

The example json is also rendered ok:

Example definition that reproduces the error:

{
  "swagger": "2.0",
  "info": {
    "title": "Example of map usage in Swagger\n",
    "description": "None\n",
    "version": ""
  },
  "host": "locahost",
  "schemes": [
    "https"
  ],
  "basePath": "/api",
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
  "paths": {
    "/books": {
      "get": {
        "parameters": [
          {
            "name": "name",
            "in": "query",
            "description": "name of owner of the books",
            "type": "string"
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "schema": {
              "type": "object",
              "additionalProperties": {
                "$ref": "#/definitions/book"
              }
            }
          }
        }
      }
    }
  },
  "definitions": {
    "book": {
      "type": "object",
      "properties": {
        "book_id": {
          "type": "integer"
        },
        "book_title": {
          "type": "string"
        }
      }
    }
  }
}
twskj commented 6 years ago

Thanks for reporting.

twskj commented 6 years ago

A quick work around would be taking out additionalProperties like so

  "responses": {
    "200": {
      "description": "OK",
      "schema": {
        "type": "object",
        "$ref": "#/definitions/book"
      }
    }
  }

But I'll make a patch that merge two object properties.

twskj commented 6 years ago

Rolled out on 0.1.145 can you check?

joanlofe commented 6 years ago

Hi! Thanks for the quick action and apologies for my late response. Now it shows the following:

captura

It seems like the reponse is a book object, when in fact it should be a map with strings as keys and book instances as values, something like this:

{
  "bookInstance1": {
    "book_id": "integer",
    "book_title": "string"
  },
  "bookInstance2": {
    "book_id": "integer",
    "book_title": "string"
  },
  "bookInstance3": {
    "book_id": "integer",
    "book_title": "string"
  }
}

So, now it is better, it shows something, but the representation of the response is misleading.

joanlofe commented 6 years ago

By the way, tested with 0.1.148 (latest version so far)

twskj commented 6 years ago

I think I misinterpreted how additionalProperties work. Let's clear it up. Can you verify these assumption:

  1. The name bookInstance1-3 is something I need to infer
  2. It just happens to have 3 keys here (meaning 3 is an arbitrary number. If I only show 2,4 or n would be acceptable as well.)

Implementation aside, I am curious in what case are we using this over an array of object with additional key name

Anyway thanks for reporting 👍

twskj commented 6 years ago

rolled out in the 0.1.150 patch Check it out and let me know.

joanlofe commented 6 years ago

Hi. I've checked it with version 0.1.151 and it is ok now. As suggestion, but it is not really needed, the key could be "book1" instead of only "book", to emphasize that there can be many books.

Regarding the use case, this is useful when you want your API to return the data in a normalized form, like the one you obtain using the Normalizr library.

Thank you very much, and congrats for the good work!