Skip to content

Commit

Permalink
Merge pull request #36 from link-duan/main
Browse files Browse the repository at this point in the history
feat: 自定义响应参数类型按照顺序遍历防止生成的文档内容随机
  • Loading branch information
link-duan authored Dec 6, 2022
2 parents 568e4ba + 9cef7db commit ae878a3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
23 changes: 14 additions & 9 deletions plugins/echo/handler_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
analyzer "github.com/gotomicro/eapi"
"github.com/gotomicro/eapi/plugins/common"
"github.com/gotomicro/eapi/spec"
"github.com/gotomicro/eapi/utils"
"github.com/iancoleman/strcase"
"github.com/robertkrimen/otto"
"github.com/samber/lo"
Expand Down Expand Up @@ -356,15 +357,19 @@ func (p *handlerAnalyzer) parseDataType(call *ast.CallExpr, dataType *common.Dat
case common.DataTypeObject:
schema := spec.NewObjectSchema()
properties := make(spec.Schemas)
for name, dataSchema := range dataType.Properties {
if !dataSchema.Optional {
schema.Required = append(schema.Required, name)
}
s := p.parseDataType(call, dataSchema, contentType)
if s != nil {
properties[name] = s
}
}
utils.RangeMapInOrder(
dataType.Properties,
func(a, b string) bool { return a < b },
func(name string, dataSchema *common.DataSchema) {
if !dataSchema.Optional {
schema.Required = append(schema.Required, name)
}
s := p.parseDataType(call, dataSchema, contentType)
if s != nil {
properties[name] = s
}
},
)
schema.Properties = properties
return spec.NewSchemaRef("", schema)
default: // fallback to js expression
Expand Down
23 changes: 14 additions & 9 deletions plugins/gin/handler_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
analyzer "github.com/gotomicro/eapi"
"github.com/gotomicro/eapi/plugins/common"
"github.com/gotomicro/eapi/spec"
"github.com/gotomicro/eapi/utils"
"github.com/iancoleman/strcase"
"github.com/robertkrimen/otto"
"github.com/samber/lo"
Expand Down Expand Up @@ -373,15 +374,19 @@ func (p *handlerAnalyzer) parseDataType(call *ast.CallExpr, dataType *common.Dat
case common.DataTypeObject:
schema := spec.NewObjectSchema()
properties := make(spec.Schemas)
for name, dataSchema := range dataType.Properties {
if !dataSchema.Optional {
schema.Required = append(schema.Required, name)
}
s := p.parseDataType(call, dataSchema, contentType)
if s != nil {
properties[name] = s
}
}
utils.RangeMapInOrder(
dataType.Properties,
func(a, b string) bool { return a < b },
func(name string, dataSchema *common.DataSchema) {
if !dataSchema.Optional {
schema.Required = append(schema.Required, name)
}
s := p.parseDataType(call, dataSchema, contentType)
if s != nil {
properties[name] = s
}
},
)
schema.Properties = properties
return spec.NewSchemaRef("", schema)
default: // fallback to js expression
Expand Down

0 comments on commit ae878a3

Please sign in to comment.