Skip to content

Commit 0e7b928

Browse files
authored
chore: sync generator (#53)
1 parent 9181d0f commit 0e7b928

File tree

8 files changed

+140
-16
lines changed

8 files changed

+140
-16
lines changed

.generator/src/generator/cli.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def cli(specs, output):
4040
env.filters["parameter_schema"] = openapi.parameter_schema
4141
env.filters["parameters"] = openapi.parameters
4242
env.filters["form_parameter"] = openapi.form_parameter
43+
env.filters["need_body_parameter"] = openapi.need_body_parameter
4344
env.filters["response_type"] = openapi.get_type_for_response
4445
env.filters["responses_by_types"] = openapi.responses_by_types
4546
env.filters["return_type"] = openapi.return_type

.generator/src/generator/openapi.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -271,19 +271,20 @@ def parameters(operation):
271271
yield content["name"], content
272272

273273
if "requestBody" in operation:
274-
if "multipart/form-data" in operation["requestBody"]["content"]:
275-
parent = operation["requestBody"]["content"]["multipart/form-data"]["schema"]
276-
for name, schema in parent["properties"].items():
277-
yield name, {
278-
"in": "form",
279-
"schema": schema,
280-
"name": name,
281-
"description": schema.get("description"),
282-
"required": name in parent.get("required", []),
283-
}
284-
else:
285-
name = operation.get("x-codegen-request-body-name", "body")
286-
yield name, operation["requestBody"]
274+
for content_type in operation["requestBody"]["content"]:
275+
if content_type == "multipart/form-data":
276+
parent = operation["requestBody"]["content"]["multipart/form-data"]["schema"]
277+
for name, schema in parent["properties"].items():
278+
yield name, {
279+
"in": "form",
280+
"schema": schema,
281+
"name": name,
282+
"description": schema.get("description"),
283+
"required": name in parent.get("required", []),
284+
}
285+
else:
286+
name = operation.get("x-codegen-request-body-name", "body")
287+
yield name, operation["requestBody"]
287288

288289
for content in operation.get("parameters", []):
289290
if "schema" in content and not content.get("required"):
@@ -302,6 +303,14 @@ def form_parameter(operation):
302303
}
303304

304305

306+
def need_body_parameter(operation):
307+
if "requestBody" in operation:
308+
for content_type in operation["requestBody"]["content"]:
309+
if content_type != "multipart/form-data":
310+
return True
311+
return False
312+
313+
305314
def parameter_schema(parameter):
306315
if "schema" in parameter:
307316
return parameter["schema"]

.generator/src/generator/templates/api.j2

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type {{ classname }} {{ common_package_name }}.Service
2121
{%- set httpMethod = method.upper() %}
2222
{%- set returnType = operation|return_type %}
2323
{%- set formParameter = operation|form_parameter %}
24+
{%- set needBodyParameter = operation|need_body_parameter %}
2425
{%- set operationId = operation.operationId|upperfirst %}
2526

2627
{%- for name, parameter in operation|parameters if not parameter.required and parameter.in != "path" %}
@@ -236,6 +237,12 @@ localVarQueryParams.Add("{{ parameter.name }}", {{ common_package_name }}.Parame
236237
fbs, _ := _io.ReadAll(localVarFile)
237238
formFile.FileBytes = fbs
238239
}
240+
{% if needBodyParameter %}
241+
localVarFormParams, err = {{ common_package_name }}.BuildFormParams({{ operation.get("x-codegen-request-body-name", "body")|variable_name }})
242+
if err != nil {
243+
return {% if returnType %}localVarReturnValue, {% endif %}nil, {{ common_package_name }}.ReportError("Failed to build form params: %s", err.Error())
244+
}
245+
{%- endif %}
239246
{%- endif %}
240247

241248
{%- if operation.requestBody is defined and not formParameter %}

.generator/src/generator/templates/client.j2

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,4 +581,50 @@ func (e GenericOpenAPIError) Body() []byte {
581581
func (e GenericOpenAPIError) Model() interface{} {
582582
return e.ErrorModel
583583
}
584+
585+
func BuildFormParams(body interface{}) (url.Values, error) {
586+
if body == nil {
587+
return url.Values{}, nil
588+
}
589+
localVarFormParams := url.Values{}
590+
t := reflect.TypeOf(body)
591+
v := reflect.ValueOf(body)
592+
593+
for i := 0; i < t.NumField(); i++ {
594+
field := t.Field(i)
595+
value := v.Field(i)
596+
597+
// Check if the field has json tag and is exported (starts with uppercase)
598+
tagValue := field.Tag.Get("json")
599+
if tagValue == "" || tagValue == "-" {
600+
continue // Skip fields without json tag or with "-" tag value
601+
}
602+
603+
// Handle pointer types
604+
if value.Kind() == reflect.Ptr && !value.IsNil() {
605+
value = value.Elem()
606+
} else if value.Kind() == reflect.Ptr && value.IsNil() {
607+
// Skip nil pointers
608+
continue
609+
}
610+
611+
// Get the actual value and check if it's empty
612+
actualValue := value.Interface()
613+
if actualValue == nil || actualValue == "" {
614+
continue // Skip empty values
615+
}
616+
tagValue = strings.Split(tagValue, ",")[0]
617+
// Add to form params
618+
switch val := actualValue.(type) {
619+
case string:
620+
localVarFormParams.Add(tagValue, val)
621+
case time.Time:
622+
localVarFormParams.Add(tagValue, val.Format(time.DateTime))
623+
default:
624+
// For other types, you might want to implement custom handling or convert to string
625+
localVarFormParams.Add(tagValue, fmt.Sprint(val))
626+
}
627+
}
628+
return localVarFormParams, nil
629+
}
584630
{# newline at the end of file #}

api/common/client.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,3 +575,49 @@ func (e GenericOpenAPIError) Body() []byte {
575575
func (e GenericOpenAPIError) Model() interface{} {
576576
return e.ErrorModel
577577
}
578+
579+
func BuildFormParams(body interface{}) (url.Values, error) {
580+
if body == nil {
581+
return url.Values{}, nil
582+
}
583+
localVarFormParams := url.Values{}
584+
t := reflect.TypeOf(body)
585+
v := reflect.ValueOf(body)
586+
587+
for i := 0; i < t.NumField(); i++ {
588+
field := t.Field(i)
589+
value := v.Field(i)
590+
591+
// Check if the field has json tag and is exported (starts with uppercase)
592+
tagValue := field.Tag.Get("json")
593+
if tagValue == "" || tagValue == "-" {
594+
continue // Skip fields without json tag or with "-" tag value
595+
}
596+
597+
// Handle pointer types
598+
if value.Kind() == reflect.Ptr && !value.IsNil() {
599+
value = value.Elem()
600+
} else if value.Kind() == reflect.Ptr && value.IsNil() {
601+
// Skip nil pointers
602+
continue
603+
}
604+
605+
// Get the actual value and check if it's empty
606+
actualValue := value.Interface()
607+
if actualValue == nil || actualValue == "" {
608+
continue // Skip empty values
609+
}
610+
tagValue = strings.Split(tagValue, ",")[0]
611+
// Add to form params
612+
switch val := actualValue.(type) {
613+
case string:
614+
localVarFormParams.Add(tagValue, val)
615+
case time.Time:
616+
localVarFormParams.Add(tagValue, val.Format(time.DateTime))
617+
default:
618+
// For other types, you might want to implement custom handling or convert to string
619+
localVarFormParams.Add(tagValue, fmt.Sprint(val))
620+
}
621+
}
622+
return localVarFormParams, nil
623+
}

api/kbcloud/admin/api_dms.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (a *DmsApi) DataExport(ctx _context.Context, orgName string, clusterName st
119119
}
120120

121121
// DataImport Data Import.
122-
func (a *DmsApi) DataImport(ctx _context.Context, orgName string, clusterName string, id string, file _io.Reader) (interface{}, *_nethttp.Response, error) {
122+
func (a *DmsApi) DataImport(ctx _context.Context, orgName string, clusterName string, id string, body DmsImportRequest, file _io.Reader) (interface{}, *_nethttp.Response, error) {
123123
var (
124124
localVarHTTPMethod = _nethttp.MethodPost
125125
localVarPostBody interface{}
@@ -158,6 +158,11 @@ func (a *DmsApi) DataImport(ctx _context.Context, orgName string, clusterName st
158158
fbs, _ := _io.ReadAll(localVarFile)
159159
formFile.FileBytes = fbs
160160
}
161+
162+
localVarFormParams, err = common.BuildFormParams(body)
163+
if err != nil {
164+
return localVarReturnValue, nil, common.ReportError("Failed to build form params: %s", err.Error())
165+
}
161166
common.SetAuthKeys(
162167
ctx,
163168
&localVarHeaderParams,

api/kbcloud/admin/api_engine_license.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type EngineLicenseApi common.Service
1919

2020
// CreateEngineLicense Create engineLicense.
2121
// Create a new engineLicense
22-
func (a *EngineLicenseApi) CreateEngineLicense(ctx _context.Context, licenseFile _io.Reader) (EngineLicense, *_nethttp.Response, error) {
22+
func (a *EngineLicenseApi) CreateEngineLicense(ctx _context.Context, body EngineLicenseCreate, licenseFile _io.Reader) (EngineLicense, *_nethttp.Response, error) {
2323
var (
2424
localVarHTTPMethod = _nethttp.MethodPost
2525
localVarPostBody interface{}
@@ -55,6 +55,11 @@ func (a *EngineLicenseApi) CreateEngineLicense(ctx _context.Context, licenseFile
5555
fbs, _ := _io.ReadAll(localVarFile)
5656
formFile.FileBytes = fbs
5757
}
58+
59+
localVarFormParams, err = common.BuildFormParams(body)
60+
if err != nil {
61+
return localVarReturnValue, nil, common.ReportError("Failed to build form params: %s", err.Error())
62+
}
5863
common.SetAuthKeys(
5964
ctx,
6065
&localVarHeaderParams,

api/kbcloud/api_dms.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (a *DmsApi) DataExport(ctx _context.Context, orgName string, clusterName st
119119
}
120120

121121
// DataImport Data Import.
122-
func (a *DmsApi) DataImport(ctx _context.Context, orgName string, clusterName string, id string, file _io.Reader) (interface{}, *_nethttp.Response, error) {
122+
func (a *DmsApi) DataImport(ctx _context.Context, orgName string, clusterName string, id string, body DmsImportRequest, file _io.Reader) (interface{}, *_nethttp.Response, error) {
123123
var (
124124
localVarHTTPMethod = _nethttp.MethodPost
125125
localVarPostBody interface{}
@@ -158,6 +158,11 @@ func (a *DmsApi) DataImport(ctx _context.Context, orgName string, clusterName st
158158
fbs, _ := _io.ReadAll(localVarFile)
159159
formFile.FileBytes = fbs
160160
}
161+
162+
localVarFormParams, err = common.BuildFormParams(body)
163+
if err != nil {
164+
return localVarReturnValue, nil, common.ReportError("Failed to build form params: %s", err.Error())
165+
}
161166
common.SetAuthKeys(
162167
ctx,
163168
&localVarHeaderParams,

0 commit comments

Comments
 (0)