diff --git a/internal/examples/main.go b/internal/examples/main.go index 6d0f648..6882b4f 100644 --- a/internal/examples/main.go +++ b/internal/examples/main.go @@ -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 { @@ -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")) } diff --git a/opendoc/operation.go b/opendoc/operation.go index 5e21f0b..df7985b 100644 --- a/opendoc/operation.go +++ b/opendoc/operation.go @@ -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 diff --git a/opendoc/util.go b/opendoc/util.go index cd30f50..367fa5a 100644 --- a/opendoc/util.go +++ b/opendoc/util.go @@ -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{}): @@ -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)) @@ -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 == "" {