Skip to content
Snippets Groups Projects
Commit 515ab5b3 authored by Johan de Klerk's avatar Johan de Klerk
Browse files

docs: Added definitions

parent f2a69c27
No related branches found
No related tags found
No related merge requests found
...@@ -2,6 +2,7 @@ package api_documentation ...@@ -2,6 +2,7 @@ package api_documentation
import ( import (
"fmt" "fmt"
"reflect"
"strings" "strings"
"gitlab.com/uafrica/go-utils/handler_utils" "gitlab.com/uafrica/go-utils/handler_utils"
...@@ -13,6 +14,7 @@ type DocPath map[string]DocMethodInfo ...@@ -13,6 +14,7 @@ type DocPath map[string]DocMethodInfo
type Docs struct { type Docs struct {
Paths map[string]DocPath `json:"paths"` Paths map[string]DocPath `json:"paths"`
Definitions map[string]interface{}
} }
...@@ -87,32 +89,41 @@ func GetDocs(endpointHandlers map[string]map[string]interface{}) (Docs, error) { ...@@ -87,32 +89,41 @@ func GetDocs(endpointHandlers map[string]map[string]interface{}) (Docs, error) {
docMethod.Parameters = append(docMethod.Parameters, parameter) docMethod.Parameters = append(docMethod.Parameters, parameter)
} }
// Request
if handler.RequestBodyType != nil { if handler.RequestBodyType != nil {
body := reflect.New(handler.RequestBodyType).Interface()
bodyTypeString := getType(body)
parameter := DocParam{ parameter := DocParam{
Name: "body", Name: "body",
In: "body", In: "body",
Schema: DocSchema{Ref: "#/definitions/Pet"}, Schema: DocSchema{Ref: "#/definitions/"+bodyTypeString},
} }
docMethod.Parameters = append(docMethod.Parameters, parameter) docMethod.Parameters = append(docMethod.Parameters, parameter)
docs.Definitions[bodyTypeString] = ""
} }
// Response
if handler.ResponseType != nil { if handler.ResponseType != nil {
responses := map[string]DocResponseValue{} responses := map[string]DocResponseValue{}
responseBody := reflect.New(handler.RequestBodyType).Interface()
responseBodyTypeString := getType(responseBody)
responses["200"] = DocResponseValue{ responses["200"] = DocResponseValue{
Description: "successful operation", Description: "successful operation",
Schema: &DocSchemaResponse{ Schema: &DocSchemaResponse{
Items: DocSchema{Ref: "#/definitions/Pet"}, Items: DocSchema{Ref: "#/definitions/"+responseBodyTypeString},
}, },
} }
docMethod.Responses = responses docMethod.Responses = responses
docs.Definitions[responseBodyTypeString] = ""
} }
//describe request schema //describe request schema
// var err error // var err error
// docMethod.Request, err = DocSchema(fmt.Sprintf("%s %s %s", method, path, "request"), handler.RequestBodyType) // 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) { ...@@ -129,8 +140,18 @@ func GetDocs(endpointHandlers map[string]map[string]interface{}) (Docs, error) {
} }
docs.Paths[path] = docPath docs.Paths[path] = docPath
} }
return docs, nil 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) { // func DocSchema(description string, t reflect.Type) (interface{}, error) {
// if t == nil { // if t == nil {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment