swagger-api / swagger-ui

Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.
https://swagger.io
Apache License 2.0
26.56k stars 8.96k forks source link

Can't read from server [...] #2154

Closed marcoliceti closed 7 years ago

marcoliceti commented 8 years ago

Hi. I wrote a valid swagger file and I'd like to feed it to swagger-ui.

(I know the file is valid because I validated it with swagger-cli and with the online editor).

To avoid CORS issues, I copied my swagger file inside the dist folder and started a temporary local web server with the node http-server module. When I paste the URL of my swagger file I get the error Can't read from server. It may not have the appropriate access-control-origin settings.

schermata 2016-05-12 alle 12 59 54

As you can see from the screenshot, the swagger file is read from server, so the error message is obviously wrong. And the sentence It may not have the appropriate access-control-origin settings is kind of misleading: the swagger-ui and my swagger file come from the same origin, so CORS issues shouldn't be involved.

The swagger-ui version I used is 2.1.4.

fehguy commented 8 years ago

Share the file, please? Just because the swagger-editor says it's valid doesn't necessarily mean it's valid. There are some situations that cannot be programmatically checked (they are decreasing, but it is possible).

marcoliceti commented 8 years ago
{
  "swagger": "2.0",
  "info": {
    "title": "ZeroTitoli",
    "version": "0.0.0",
  },
  "schemes": [ "https" ],
  "consumes": [ "application/json" ],
  "produces": [ "application/json" ],
  "paths": {
    "/partite": {
      "post": {
        "description": "
          Creazione di una nuova partita per un certo gruppo. Occorre indicare
          sia il gruppo sia le informazioni sulla nuova partita. L'utente deve
          essere amministratore del gruppo indicato.
        ",
        "parameters": [
          {
            "name": "partita",
            "in": "body",
            "schema": {
              "$ref": "#/definitions/Partita"
            },
            "required": true
          }
        ],
        "responses": {
          "201": {
            "description": "La partita è stata creata con successo.",
            "schema": {
              "$ref": "#/definitions/Partita"
            }
          },
          "403": {
            "description": "L'utente non è amministratore del gruppo indicato."
          }
        },
      }
    }
  },
  "security": [
    {
      "api_key": []
    }
  ],
  "definitions": {
    "Partita": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string"
        },
        "giorno": {
          "type": "string"
        },
        "ora": {
          "type": "string"
        },
        "circolo": {
          "type": "string"
        },
        "quota": {
          "type": "string"
        },
        "note": {
          "type": "string"
        },
        "gruppo": {
          "type": "string",
          "description": "ID del gruppo per il quale è stata creata la partita."
        }
      },
      "required": [ "giorno", "ora", "circolo", "gruppo" ]
    }
  },
  "securityDefinitions": {
    "api_key": {
      "type": "apiKey",
      "in": "header",
      "name": "X-ZeroTitoli-Api-Key"
    }
  }
}
fehguy commented 8 years ago

Hi, your contents are not valid JSON. Try opening it in a JSON viewer and you'll see a number of errors:

image

marcoliceti commented 8 years ago

Thank you. The editor I use didn't complain and I didn't think about using a JSON validator. However, a more specific error message could be helpful. I think the failure of JSON.parse can be detected.