Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swagger UI failed to fetch on simple server #1107

Closed
rcorre opened this issue Jul 24, 2017 · 1 comment
Closed

Swagger UI failed to fetch on simple server #1107

rcorre opened this issue Jul 24, 2017 · 1 comment

Comments

@rcorre
Copy link

rcorre commented Jul 24, 2017

Problem statement

Even the simplest server I could come up with fails on swagger UI with TypeError: Failed to fetch

Swagger specification

{
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
  "schemes": [
    "http",
    "https"
  ],
  "swagger": "2.0",
  "info": {
    "description": "Title: example",
    "title": "is an example REST app",
    "version": "0.0.1"
  },
  "host": "localhost:3000",
  "basePath": "/v1",
  "paths": {
    "/foo": {
      "get": {
        "description": "Get a foo",
        "operationId": "getFoo",
        "responses": {
          "200": {
            "$ref": "#/responses/getFooResponse"
          }
        }
      }
    }
  },
  "responses": {
    "getFooResponse": {
      "description": "GetFooResponse is returned by GET /foo",
      "schema": {
        "type": "object",
        "properties": {
          "Name": {
            "description": "Name of the foo",
            "type": "string"
          }
        }
      }
    }
  },
  "securityDefinitions": {
    "bearer": {
      "type": "apiKey",
      "name": "Authorization",
      "in": "header"
    }
  },
  "security": [
    {
      "bearer": []
    }
  ]
}

Steps to reproduce

main.go:

// Package main is an example REST app
//
//     Title: example
//     Schemes: http, https
//     Host: localhost:3000
//     BasePath: /v1
//     Version: 0.0.1
//
//     Consumes:
//     - application/json
//
//     Produces:
//     - application/json
//
//	   Security:
//     - bearer:
//
//     SecurityDefinitions:
//       bearer:
//         type: apiKey
//         name: Authorization
//         in: header
// swagger:meta
package main

import (
	"encoding/json"
	"log"
	"net/http"
	"time"
)

func main() {
	s := &http.Server{
		Addr:           ":3000",
		Handler:        &myHandler{},
		ReadTimeout:    10 * time.Second,
		WriteTimeout:   10 * time.Second,
		MaxHeaderBytes: 1 << 20,
	}
	log.Fatal(s.ListenAndServe())
}

type myHandler struct{}

// GetFoo handles a GET to /foo
//
// swagger:route GET /foo getFoo
//
// Get a foo
//
//     Responses:
//       200: getFooResponse
func (myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	log.Printf("Got request %+v", r)
	defer r.Body.Close()

	var resp GetFooResponse
	resp.Body.Name = "foo"

	jsonBody, err := json.Marshal(&resp.Body)
	if err != nil {
		http.Error(w, "failed to marshal", 500)
		return
	}

	w.Write(jsonBody)
}

// GetFooArgs are the parameters to GET /foo
//
// swagger:parameters getFoo
type GetFooArgs struct {
}

// GetFooResponse is returned by GET /foo
//
// swagger:response getFooResponse
type GetFooResponse struct {
	// in:body
	Body struct {
		// Name of the foo
		Name string
	}
}
go build && swagger generate spec -o swagger.json
./example & swagger serve swagger.json -F swagger

Through the swagger UI, try to make a GET request to /foo. It will fail.

Environment

swagger version: dev
go version: go1.8.3
OS: linux/amd64

@casualjim
Copy link
Member

your server needs to add a cors middleware, that's the cause of the failure.
#481

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

No branches or pull requests

2 participants