Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add decimal.Decimal type support to RESTful and gRPC #53

Merged
merged 8 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions cmd/internal/openapi/v3/codegen/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,15 @@ func (receiver *OpenAPICodeGenerator) GenGoDto(schemas map[string]v3.Schema, out
astutils.FixImport([]byte(source), output)
}

func (receiver *OpenAPICodeGenerator) schema2Field(schema *v3.Schema, name string) *astutils.FieldMeta {
// TODO example2Schema converts example to *v3.Schema
func (receiver *OpenAPICodeGenerator) example2Schema(example interface{}, exampleType v3.ExampleType) *v3.Schema {
return v3.Any
}

func (receiver *OpenAPICodeGenerator) schema2Field(schema *v3.Schema, name string, example interface{}, exampleType v3.ExampleType) *astutils.FieldMeta {
if schema == nil {
schema = receiver.example2Schema(example, exampleType)
}
var comments []string
if stringutils.IsNotEmpty(schema.Description) {
comments = append(comments, strings.Split(schema.Description, "\n")...)
Expand Down Expand Up @@ -303,16 +311,16 @@ func (receiver *OpenAPICodeGenerator) responseBody(endpoint, httpMethod string,
}

if content.JSON != nil {
results = append(results, *receiver.schema2Field(content.JSON.Schema, "ret"))
results = append(results, *receiver.schema2Field(content.JSON.Schema, "ret", content.JSON.Example, v3.JSON_EXAMPLE))
} else if content.Stream != nil {
results = append(results, astutils.FieldMeta{
Name: "_downloadFile",
Type: "*os.File",
})
} else if content.TextPlain != nil {
results = append(results, *receiver.schema2Field(content.TextPlain.Schema, "ret"))
results = append(results, *receiver.schema2Field(content.TextPlain.Schema, "ret", content.TextPlain.Example, v3.TEXT_EXAMPLE))
} else if content.Default != nil {
results = append(results, *receiver.schema2Field(content.Default.Schema, "ret"))
results = append(results, *receiver.schema2Field(content.Default.Schema, "ret", content.Default.Example, v3.TEXT_EXAMPLE))
} else {
return nil, errors.Errorf("200 response content definition not support yet in api %s %s", httpMethod, endpoint)
}
Expand Down Expand Up @@ -434,7 +442,7 @@ func (receiver *ClientOperationConverter) form(operation *v3.Operation) (bodyPar
content := operation.RequestBody.Content
if content.JSON != nil {
} else if content.FormURL != nil {
bodyParams = receiver.Generator.schema2Field(content.FormURL.Schema, "bodyParams")
bodyParams = receiver.Generator.schema2Field(content.FormURL.Schema, "bodyParams", content.FormURL.Example, v3.TEXT_EXAMPLE)
if !operation.RequestBody.Required && bodyParams != nil {
bodyParams.Type = v3.ToOptional(bodyParams.Type)
}
Expand All @@ -455,7 +463,7 @@ func (receiver *ClientOperationConverter) ConvertOperation(endpoint, httpMethod
receiver.operationParams(operation.Parameters, &qSchema, &pathvars, &headervars)

if len(qSchema.Properties) > 0 {
qparams = receiver.Generator.schema2Field(&qSchema, "queryParams")
qparams = receiver.Generator.schema2Field(&qSchema, "queryParams", nil, v3.UNKNOWN_EXAMPLE)
if qSchema.Type == v3.ObjectT && len(qSchema.Required) == 0 {
qparams.Type = v3.ToOptional(qparams.Type)
}
Expand Down Expand Up @@ -535,7 +543,7 @@ func (receiver *OpenAPICodeGenerator) requestBody(operation *v3.Operation) (body

content := operation.RequestBody.Content
if content.JSON != nil {
bodyJSON = receiver.schema2Field(content.JSON.Schema, "bodyJSON")
bodyJSON = receiver.schema2Field(content.JSON.Schema, "bodyJSON", content.JSON.Example, v3.JSON_EXAMPLE)
if !operation.RequestBody.Required && bodyJSON != nil {
bodyJSON.Type = v3.ToOptional(bodyJSON.Type)
}
Expand All @@ -551,12 +559,12 @@ func (receiver *OpenAPICodeGenerator) requestBody(operation *v3.Operation) (body
}
files = append(files, f)
} else if content.TextPlain != nil {
bodyJSON = receiver.schema2Field(content.TextPlain.Schema, "bodyJSON")
bodyJSON = receiver.schema2Field(content.TextPlain.Schema, "bodyJSON", content.TextPlain.Example, v3.TEXT_EXAMPLE)
if !operation.RequestBody.Required && bodyJSON != nil {
bodyJSON.Type = v3.ToOptional(bodyJSON.Type)
}
} else if content.Default != nil {
bodyJSON = receiver.schema2Field(content.Default.Schema, "bodyJSON")
bodyJSON = receiver.schema2Field(content.Default.Schema, "bodyJSON", content.TextPlain.Example, v3.TEXT_EXAMPLE)
if !operation.RequestBody.Required && bodyJSON != nil {
bodyJSON.Type = v3.ToOptional(bodyJSON.Type)
}
Expand Down Expand Up @@ -596,7 +604,7 @@ func (receiver *OpenAPICodeGenerator) parseFormData(formData *v3.MediaType) (bod
}
}
if len(aSchema.Properties) > 0 {
bodyParams = receiver.schema2Field(&aSchema, "bodyParams")
bodyParams = receiver.schema2Field(&aSchema, "bodyParams", nil, v3.UNKNOWN_EXAMPLE)
}
return
}
Expand Down
7 changes: 7 additions & 0 deletions cmd/internal/openapi/v3/codegen/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ func TestPattern2Method(t *testing.T) {
},
want: "Goodfood_BigappleBooks_Mybird",
},
{
name: "",
args: args{
pattern: "/api/v1/query_range",
},
want: "ApiV1Query_range",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/internal/openapi/v3/codegen/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func Pattern2Method(pattern string) string {
pattern = strings.TrimSuffix(strings.TrimPrefix(pattern, "/"), "/")
partials := strings.Split(pattern, "/")
var converted []string
nosymbolreg := regexp.MustCompile(`[^a-zA-Z0-9]`)
nosymbolreg := regexp.MustCompile(`[^a-zA-Z0-9_]`)
for _, item := range partials {
if strings.HasPrefix(item, "{") && strings.HasSuffix(item, "}") {
item = strings.TrimSuffix(strings.TrimPrefix(item, "{"), "}")
Expand Down Expand Up @@ -102,7 +102,7 @@ func (receiver *ServerOperationConverter) parseForm(form *v3.MediaType) (bodyPar
if stringutils.IsNotEmpty(gotype) {
continue
}
field := receiver.Generator.schema2Field(v, k)
field := receiver.Generator.schema2Field(v, k, nil, v3.UNKNOWN_EXAMPLE)
if !sliceutils.StringContains(schema.Required, k) {
field.Type = v3.ToOptional(field.Type)
}
Expand Down
32 changes: 32 additions & 0 deletions cmd/internal/openapi/v3/codegen/server/svc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package server

import (
"path/filepath"
"testing"
)

var dir = "../testdata"

func TestGenSvcGo(t *testing.T) {
type args struct {
dir string
docPath string
}
tests := []struct {
name string
args args
}{
{
name: "",
args: args{
dir: filepath.Join(dir, "testgensvcgo"),
docPath: filepath.Join(dir, "prometheus_openapi3.json"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
GenSvcGo(tt.args.dir, tt.args.docPath)
})
}
}
Loading