Skip to content

Commit

Permalink
fix: update opendoc
Browse files Browse the repository at this point in the history
  • Loading branch information
kooksee committed Oct 30, 2023
1 parent a4d2544 commit 2f5f7ec
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
17 changes: 10 additions & 7 deletions internal/examples/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package main

import (
"github.com/gofiber/fiber/v2"
"github.com/pubgo/funk/assert"
"github.com/pubgo/funk/recovery"
"github.com/pubgo/opendoc/opendoc"
"github.com/pubgo/opendoc/security"
"os"
)

type TestQueryReqAAA struct {
Expand Down Expand Up @@ -42,24 +42,27 @@ func main() {
srv.PostOf(func(op *opendoc.Operation) {
op.SetPath("article_create", "/articles")
op.SetModel(new(TestQueryReq1), new(TestQueryRsp))
op.SetDescription("create article")
op.SetSummary("create article")
})

srv.GetOf(func(op *opendoc.Operation) {
op.SetPath("article_list", "/articles")
op.SetModel(new(TestQueryReq), new(TestQueryRsp))
op.SetDescription("get article list")
op.SetSummary("get article list")
op.AddResponse("Test", new(TestQueryReqAAA))
})

srv.PutOf(func(op *opendoc.Operation) {
op.SetPath("article_update", "/articles/{id}")
op.SetModel(new(TestQueryReq1), new(TestQueryRsp))
op.SetDescription("update article")
op.SetSummary("update article")
})
})

var app = fiber.New()
doc.InitRouter(app)
assert.Exit(app.Listen("localhost:8080"))
data := assert.Must1(doc.MarshalYAML())
assert.Exit(os.WriteFile("openapi.yaml", data, 0644))

//var app = fiber.New()
//doc.InitRouter(app)
//assert.Exit(app.Listen("localhost:8080"))
}
6 changes: 6 additions & 0 deletions opendoc/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ func (op *Operation) SetPath(operationID string, path string) *Operation {
return op
}

func (op *Operation) SetOperation(operationID string) *Operation {
assert.If(operationID == "", "operationID should not be nil")
op.operationID = operationID
return op
}

func (op *Operation) SetModel(req interface{}, rsp interface{}) *Operation {
checkModelType(req)
op.request = req
Expand Down
7 changes: 4 additions & 3 deletions opendoc/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func genSchema(val interface{}) (ref string, schema *openapi3.Schema) {
case reflect.TypeOf(multipart.FileHeader{}):
return "", &openapi3.Schema{Type: openapi3.TypeString, Format: "binary"}
case reflect.TypeOf([]*multipart.FileHeader{}):
schema := openapi3.NewArraySchema()
schema = openapi3.NewArraySchema()
schema.Items = openapi3.NewSchemaRef("", &openapi3.Schema{Type: openapi3.TypeString, Format: "binary"})
return "", schema
case reflect.TypeOf(time.Time{}):
Expand Down Expand Up @@ -129,7 +129,7 @@ func genSchema(val interface{}) (ref string, schema *openapi3.Schema) {
schema = openapi3.NewBoolSchema()
case reflect.Array, reflect.Slice:
schema = openapi3.NewArraySchema()
schema.Items = openapi3.NewSchemaRef(genSchema(model))
schema.Items = openapi3.NewSchemaRef(genSchema(model.Elem()))
case reflect.Map:
schema = openapi3.NewObjectSchema()
schema.Items = openapi3.NewSchemaRef(genSchema(model))
Expand Down Expand Up @@ -174,8 +174,9 @@ func genSchema(val interface{}) (ref string, schema *openapi3.Schema) {
getTag(tags, nullable, func(_ *structtag.Tag) { fieldSchema.Nullable = true })
getTag(tags, required, func(_ *structtag.Tag) { fieldSchema.AllowEmptyValue = false })
getTag(tags, doc, func(tag *structtag.Tag) { fieldSchema.Description = tag.Name })
getTag(tags, format, func(tag *structtag.Tag) { fieldSchema.Format = tag.Name })
getTag(tags, description, func(tag *structtag.Tag) { fieldSchema.Description = tag.Name })
getTag(tags, format, func(tag *structtag.Tag) { fieldSchema.Format = tag.Name })
getTag(tags, deprecated, func(tag *structtag.Tag) { fieldSchema.Deprecated = true })
getTag(tags, validate, func(tag *structtag.Tag) {
desc := fieldSchema.Description
if desc == "" {
Expand Down

0 comments on commit 2f5f7ec

Please sign in to comment.