Exemple #1
0
func (r RequestInfo) ToSwagger() swagger.Method {
	ret := swagger.Method{
		Description: r.Description,
		Responses:   map[string]swagger.Response{},
		Parameters:  nil,
		Tags:        []string{strings.Title(r.Group)},
	}

	if len(r.Params) > 0 {
		ret.Parameters = make([]swagger.Param, 0)
	}
	for _, p := range r.Params {
		if !p.Hidden {
			ret.Parameters = append(ret.Parameters, p.ToSwagger())
		}
	}

	if r.Returns != nil {

		s := jsonschema.Reflect(r.Returns)

		ret.Responses["default"] = swagger.Response{
			Description: reflect.TypeOf(r.Returns).String(),
			Schema:      swagger.Schema(s),
		}

	} else {
		ret.Responses["default"] = swagger.Response{"", jsonschema.Reflect("")}

	}

	return ret
}
Exemple #2
0
// ToSwagger Converts an API definition into a swagger API object for serialization
func (a API) ToSwagger(serverUrl string) *swagger.API {

	schemes := []string{"https"}

	// http first is important for the swagger ui
	if a.AllowInsecure {
		schemes = []string{"http", "https"}
	}
	ret := swagger.NewAPI(serverUrl, a.Title, a.Doc, a.Version, a.FullPath(""), schemes)
	ret.Consumes = []string{"text/json"}
	ret.Produces = a.Renderer.ContentTypes()
	for _, route := range a.Routes {

		ri := route.requestInfo

		p := ret.AddPath(route.Path)
		method := ri.ToSwagger()

		// copy response definitions to API definitions
		for rk, resp := range method.Responses {

			if resp.Schema != nil && resp.Schema.Definitions != nil {
				for k, v := range resp.Schema.Definitions {
					ret.Definitions[k] = swagger.Schema(&jsonschema.Schema{Type: v})

				}

				resp.Schema.Definitions = nil

				method.Responses[rk] = resp
			}
		}

		// copy global param definitions to param definitions
		for i, parm := range method.Parameters {
			if parm.Global {
				ret.Parameters[parm.Name] = parm

				method.Parameters[i] = swagger.Param{Ref: fmt.Sprintf("#/parameters/%s", parm.Name)}
			}
		}

		// register methods
		if route.Methods&POST == POST {
			p["post"] = method
		}
		if route.Methods&GET == GET {
			p["get"] = method
		}
		if route.Methods&PUT == PUT {
			p["put"] = method
		}
	}

	return ret
}