Skip to content

Commit

Permalink
feat(requests): allow fileupload and allow custom media types
Browse files Browse the repository at this point in the history
  • Loading branch information
Nightapes authored Sep 6, 2022
2 parents 56b9bb9 + 8b9d5e3 commit 3942d2a
Show file tree
Hide file tree
Showing 14 changed files with 171 additions and 36 deletions.
1 change: 1 addition & 0 deletions example/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var MyGet = &openapi.Get{
Path: openapi.NewPathBuilder().
Add("users").
AddParameter("userId", openapi.TYPEENUM([]string{"aws"}), "UserID").
AddParameterList("ids", openapi.STRING, "DDDD", nil).
WithQueryParameter("filter", openapi.STRING, "Filter stuff", false),
HandlerFunc: func(writer http.ResponseWriter, request *http.Request) {
userID := chi.URLParam(request, "userID")
Expand Down
2 changes: 2 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func main() {
})
err = api.Get(MyGet)
checkErr(err)
err = api.Post(MyPost)
checkErr(err)
authMiddleware := api.ChiAuthMiddleware(func(authName string, scopes []string, r *http.Request) bool {
log.Printf("Auth check %s %s", authName, scopes)
return authName != "mybearer"
Expand Down
58 changes: 58 additions & 0 deletions example/post.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"encoding/json"
"github.com/Nightapes/go-rest/pkg/openapi"
"github.com/go-chi/chi/v5"
"net/http"
)

var example = []string{}

var MyPost = &openapi.Post{
Summary: "Post User",
Description: "Post User with given ID",
OperationID: "PostMyTest",
Tags: []string{"UserService"},
Authentication: map[string][]string{"mybasic": nil, "mybearer": {"users:read"}},
RequestBodies: &openapi.RequestBodies{
Description: "Test",
Required: true,
Bodies: map[string]interface{}{
"application/json": &example,
"application/xml": openapi.FileUploadBinary,
},
},
Response: map[string]openapi.MethodResponse{
"200": {
Description: "The response with userID",
Value: UserList{"test"},
},
"201": {
Description: "The response with userID",
Value: &[]User{{"test"}},
},
"202": {
Description: "Map Test",
Value: map[string]User2{
"test": {
UserID: "myID",
},
},
},
"204": {
Description: "The response with userID",
},
},
Headers: []openapi.Parameter{{Description: "My custom header", Name: "test-header", Required: false, Type: openapi.INTEGER}},
Path: openapi.NewPathBuilder().
Add("users").
AddParameter("userId", openapi.TYPEENUM([]string{"aws"}), "UserID").
WithQueryParameter("filter", openapi.STRING, "Filter stuff", false),
HandlerFunc: func(writer http.ResponseWriter, request *http.Request) {
userID := chi.URLParam(request, "userID")
user := &User{UserID: userID}
resp, _ := json.Marshal(user)
_, _ = writer.Write(resp)
},
}
3 changes: 1 addition & 2 deletions pkg/openapi/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ type Delete struct {
Response map[string]MethodResponse
Path *PathBuilder
Headers []Parameter
RequestBody interface{}
http.HandlerFunc
}

Expand Down Expand Up @@ -47,7 +46,7 @@ func (m *Delete) GetHeaders() []Parameter {
return m.Headers
}

func (m *Delete) GetRequestBody() interface{} {
func (m *Delete) GetRequestBodies() *RequestBodies {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/openapi/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (m *Get) GetHeaders() []Parameter {
return m.Headers
}

func (m *Get) GetRequestBody() interface{} {
func (m *Get) GetRequestBodies() *RequestBodies {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/openapi/head.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (m *Head) GetHeaders() []Parameter {
return m.Headers
}

func (m *Head) GetRequestBody() interface{} {
func (m *Head) GetRequestBodies() *RequestBodies {
return nil
}

Expand Down
47 changes: 31 additions & 16 deletions pkg/openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"github.com/Nightapes/go-rest/pkg/jsonschema"
"github.com/go-playground/validator/v10"

"gopkg.in/yaml.v2"
"log"
"net/http"
Expand Down Expand Up @@ -35,7 +34,7 @@ type PathDesc interface {
GetResponse(string) (string, interface{})
GetAuthentication(key string) (bool, []string)
GetHeaders() []Parameter
GetRequestBody() interface{}
GetRequestBodies() *RequestBodies
GetTags() []string
GetHandlerFunc() http.HandlerFunc
}
Expand Down Expand Up @@ -255,22 +254,38 @@ func (a *API) toPath(desc PathDesc, pathBuilder *PathBuilder) (*Operation, *Hand
}
}

if desc.GetRequestBody() != nil {
body := a.jsonschemaRefl.Reflect(desc.GetRequestBody())
for s, t := range body.Definitions {
a.handleEnumInProperties(t)
a.handleEnumInArrays(t)
a.OpenAPI.Components.Schemas[s] = t
t.Definitions = nil
t.Version = ""
}
if desc.GetRequestBodies() != nil {

content := map[string]*MediaType{}

for mediaType, requestBody := range desc.GetRequestBodies().Bodies {

if tmp, ok := requestBody.(FileUpload); ok {
content[mediaType] = &MediaType{Schema: &Schema{
Type: "string",
Format: string(tmp),
}, Example: requestBody}
continue
}

body := a.jsonschemaRefl.Reflect(requestBody)
for s, t := range body.Definitions {
a.handleEnumInProperties(t)
a.handleEnumInArrays(t)
a.OpenAPI.Components.Schemas[s] = t
t.Definitions = nil
t.Version = ""
}

content[mediaType] = &MediaType{Schema: &Schema{Ref: body.Ref}, Example: requestBody}

}
ops.RequestBody = &RequestBody{
Required: true,
Content: map[string]*MediaType{
"application/json": {Schema: &SchemaRef{Ref: body.Ref}, Example: desc.GetRequestBody()},
},
Required: desc.GetRequestBodies().Required,
Description: desc.GetRequestBodies().Description,
Content: content,
}

}

if a.defaultResponse != nil {
Expand Down Expand Up @@ -306,7 +321,7 @@ func (a *API) handleResponse(respDesc string, resp interface{}, i string, ops *O

ops.Responses[i] = &Response{
Content: map[string]*MediaType{
"application/json": {Schema: &SchemaRef{Value: schema.Type}, Example: resp},
"application/json": {Schema: &Schema{Value: schema.Type}, Example: resp},
},
Description: respDesc,
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/openapi/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (m *Options) GetHeaders() []Parameter {
return m.Headers
}

func (m *Options) GetRequestBody() interface{} {
func (m *Options) GetRequestBodies() *RequestBodies {
return nil
}

Expand Down
21 changes: 18 additions & 3 deletions pkg/openapi/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ type Patch struct {
Response map[string]MethodResponse
Path *PathBuilder
Headers []Parameter
RequestBody interface{}
// Deprecated: Please migrate to RequestBodies
RequestBody interface{}
RequestBodies *RequestBodies
http.HandlerFunc
}

Expand Down Expand Up @@ -47,8 +49,21 @@ func (m *Patch) GetHeaders() []Parameter {
return m.Headers
}

func (m *Patch) GetRequestBody() interface{} {
return m.RequestBody
func (m *Patch) GetRequestBodies() *RequestBodies {
if m.RequestBodies != nil {
return m.RequestBodies
}

if m.RequestBody != nil {
return &RequestBodies{
Required: true,
Bodies: map[string]interface{}{
"application/json": m.RequestBody,
},
}
}

return nil
}

func (m *Patch) GetTags() []string {
Expand Down
34 changes: 31 additions & 3 deletions pkg/openapi/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ import (
"net/http"
)

type RequestBodies struct {
Description string
Required bool
Bodies map[string]interface{}
}

type FileUpload string

const (
FileUploadBinary FileUpload = "binary"
FileUploadBase64 FileUpload = "base64"
)

type Post struct {
Summary string
Description string
Expand All @@ -13,7 +26,9 @@ type Post struct {
Response map[string]MethodResponse
Path *PathBuilder
Headers []Parameter
RequestBody interface{}
// Deprecated: Please migrate to RequestBodies
RequestBody interface{}
RequestBodies *RequestBodies
http.HandlerFunc
}

Expand Down Expand Up @@ -47,8 +62,21 @@ func (m *Post) GetHeaders() []Parameter {
return m.Headers
}

func (m *Post) GetRequestBody() interface{} {
return m.RequestBody
func (m *Post) GetRequestBodies() *RequestBodies {
if m.RequestBodies != nil {
return m.RequestBodies
}

if m.RequestBody != nil {
return &RequestBodies{
Required: true,
Bodies: map[string]interface{}{
"application/json": m.RequestBody,
},
}
}

return nil
}

func (m *Post) GetTags() []string {
Expand Down
21 changes: 18 additions & 3 deletions pkg/openapi/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ type Put struct {
Response map[string]MethodResponse
Path *PathBuilder
Headers []Parameter
RequestBody interface{}
// Deprecated: Please migrate to RequestBodies
RequestBody interface{}
RequestBodies *RequestBodies
http.HandlerFunc
}

Expand Down Expand Up @@ -47,8 +49,21 @@ func (m *Put) GetHeaders() []Parameter {
return m.Headers
}

func (m *Put) GetRequestBody() interface{} {
return m.RequestBody
func (m *Put) GetRequestBodies() *RequestBodies {
if m.RequestBodies != nil {
return m.RequestBodies
}

if m.RequestBody != nil {
return &RequestBodies{
Required: true,
Bodies: map[string]interface{}{
"application/json": m.RequestBody,
},
}
}

return nil
}

func (m *Put) GetTags() []string {
Expand Down
10 changes: 6 additions & 4 deletions pkg/openapi/simpleTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,12 @@ var (
)

type Schema struct {
Type string `json:"type,omitempty" yaml:"type,omitempty"`
Format string `json:"format,omitempty" yaml:"format,omitempty"`
Enum []string `json:"enum,omitempty" yaml:"enum,omitempty"`
Items *Schema `json:"items,omitempty" yaml:"items,omitempty"`
Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"`
Value interface{} `json:"value,omitempty" yaml:"value,omitempty"`
Type string `json:"type,omitempty" yaml:"type,omitempty"`
Format string `json:"format,omitempty" yaml:"format,omitempty"`
Enum []string `json:"enum,omitempty" yaml:"enum,omitempty"`
Items *Schema `json:"items,omitempty" yaml:"items,omitempty"`
}

func (e *TYPE) toSchema(isArray bool) *SchemaRef {
Expand Down
2 changes: 1 addition & 1 deletion pkg/openapi/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (m *Trace) GetHeaders() []Parameter {
return m.Headers
}

func (m *Trace) GetRequestBody() interface{} {
func (m *Trace) GetRequestBodies() *RequestBodies {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/openapi/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ type RequestBody struct {
}

type MediaType struct {
Schema *SchemaRef `json:"schema,omitempty" yaml:"schema,omitempty"`
Schema *Schema `json:"schema,omitempty" yaml:"schema,omitempty"`
Example interface{} `json:"example,omitempty" yaml:"example,omitempty"`
}

Expand Down

0 comments on commit 3942d2a

Please sign in to comment.