diff --git a/api_documentation/api_documentation.go b/api_documentation/api_documentation.go index 57517c79d6faf6d9a36fe0db3763a5d68fbd9a46..13810e02d49404069c6c3e92599e21deaf5e6fdf 100644 --- a/api_documentation/api_documentation.go +++ b/api_documentation/api_documentation.go @@ -2,6 +2,7 @@ package api_documentation import ( "fmt" + "reflect" "strings" "gitlab.com/uafrica/go-utils/handler_utils" @@ -13,6 +14,7 @@ type DocPath map[string]DocMethodInfo type Docs struct { Paths map[string]DocPath `json:"paths"` + Definitions map[string]interface{} } @@ -87,32 +89,41 @@ func GetDocs(endpointHandlers map[string]map[string]interface{}) (Docs, error) { docMethod.Parameters = append(docMethod.Parameters, parameter) } + // Request if handler.RequestBodyType != nil { + body := reflect.New(handler.RequestBodyType).Interface() + bodyTypeString := getType(body) parameter := DocParam{ Name: "body", In: "body", - Schema: DocSchema{Ref: "#/definitions/Pet"}, + Schema: DocSchema{Ref: "#/definitions/"+bodyTypeString}, } docMethod.Parameters = append(docMethod.Parameters, parameter) + docs.Definitions[bodyTypeString] = "" } + // Response if handler.ResponseType != nil { responses := map[string]DocResponseValue{} + responseBody := reflect.New(handler.RequestBodyType).Interface() + responseBodyTypeString := getType(responseBody) responses["200"] = DocResponseValue{ Description: "successful operation", Schema: &DocSchemaResponse{ - Items: DocSchema{Ref: "#/definitions/Pet"}, + Items: DocSchema{Ref: "#/definitions/"+responseBodyTypeString}, }, } docMethod.Responses = responses - + docs.Definitions[responseBodyTypeString] = "" } + + //describe request schema // var err error // docMethod.Request, err = DocSchema(fmt.Sprintf("%s %s %s", method, path, "request"), handler.RequestBodyType) @@ -129,8 +140,18 @@ func GetDocs(endpointHandlers map[string]map[string]interface{}) (Docs, error) { } docs.Paths[path] = docPath } + + return docs, nil } + +func getType(myvar interface{}) string { + if t := reflect.TypeOf(myvar); t.Kind() == reflect.Ptr { + return t.Elem().Name() + } else { + return t.Name() + } +} // // func DocSchema(description string, t reflect.Type) (interface{}, error) { // if t == nil {