Esempio n. 1
0
// handleMeta extracts data from api:meta markup
// there should only be one api:meta markup tag per project
// if there is more than one, the first tag will be used
func handleMeta(swagDoc *specs.SwagDoc, docs []string) error {
	swagDoc.Swagger = "2.0"
	y := []byte(docs[0])
	j, err := yaml.YAMLToJSON(y)
	err = json.Unmarshal(j, swagDoc)
	return err
}
Esempio n. 2
0
// handleRoute extracts data from the api:route markup
func handleRoute(swagDoc *specs.SwagDoc, docs []string) error {
	for _, doc := range docs {
		var paths = make(map[string]specs.SwagPath)
		y := []byte(doc)
		j, err := yaml.YAMLToJSON(y)
		err = json.Unmarshal(j, &paths)
		swagDoc.Paths = &paths
		if err != nil {
			return err
		}
		for k, v := range paths {
			paths[k] = v
		}
	}
	return nil
}
Esempio n. 3
0
// handleModel extracts data from the api:model markup
func handleModel(swagDoc *specs.SwagDoc, docs []string) error {
	var def = make(map[string]specs.SwagSchema)
	for _, doc := range docs {
		var model map[string]specs.SwagSchema
		y := []byte(doc)
		// for some reason yaml.Unmarshal throws error: "panic: reflect: reflect.Value.Set using unaddressable value"
		// with structs that have nested pointers
		// as a workaround, convert yaml string to json string and unmarshal
		j, err := yaml.YAMLToJSON(y)
		err = json.Unmarshal(j, &model)
		if err != nil {
			return err
		}
		for k, v := range model {
			def[k] = v
		}
	}
	swagDoc.Definitions = &def
	return nil
}
Esempio n. 4
0
func getSwagDoc() *specs.SwagDoc {
	var swagDoc specs.SwagDoc = specs.SwagDoc{}
	swagDoc.Swagger = "2.0"
	swagDoc.Info = &specs.SwagInfo{}
	swagDoc.Info.Title = "Swagger Petstore"
	swagDoc.Info.Description = `This is a sample server Petstore server.
You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).  For this sample, you can use the api key ` + "`special-key`" + ` to test the authorization filters.
`
	swagDoc.Info.TermsOfService = "http://swagger.io/terms/"
	swagDoc.Info.Contact = &specs.SwagContact{}
	swagDoc.Info.Contact.Name = "John Doe"
	swagDoc.Info.Contact.Url = "http://swagger.io"
	swagDoc.Info.Contact.Email = "*****@*****.**"
	swagDoc.Info.License = &specs.SwagLicense{}
	swagDoc.Info.License.Name = "Apache 2.0"
	swagDoc.Info.License.Url = "http://www.apache.org/licenses/LICENSE-2.0.html"
	swagDoc.Info.Version = "1.0.0"
	swagDoc.Host = "petstore.swagger.io"
	swagDoc.BasePath = "/v2"
	swagDoc.Consumes = &[]string{"application/json", "application/xml"}
	swagDoc.Produces = &[]string{"application/json", "application/xml"}
	swagDoc.Schemes = &[]string{"http"}
	swagDoc.SecurityDefinitions = &map[string]specs.SwagSecDef{}
	(*swagDoc.SecurityDefinitions)["api_key"] = specs.SwagSecDef{
		In:   "header",
		Name: "api_key",
		Type: "apiKey",
	}
	(*swagDoc.SecurityDefinitions)["petstore_auth"] = specs.SwagSecDef{
		AuthorizationUrl: "http://petstore.swagger.io/api/oauth/dialog",
		Flow:             "implicit",
		Type:             "oauth2",
		Scopes: &map[string]string{
			"read:pets":  "read your pets",
			"write:pets": "modify pets in your account",
		},
	}
	swagDoc.ExternalDocs = &specs.SwagExtDoc{
		Description: "Find out more about Swagger",
		Url:         "http://swagger.io",
	}
	swagDoc.Tags = &[]specs.SwagTag{}
	swagDoc.Tags = &[]specs.SwagTag{
		specs.SwagTag{
			Name:        "pet",
			Description: "Everything about your Pets",
			ExternalDocs: &specs.SwagExtDoc{
				Description: "Find out more",
				Url:         "http://swagger.io",
			},
		},
	}
	return &swagDoc
}