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

[Go] Fix deepObject serialization that are anyOf #19090

Merged
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
20 changes: 10 additions & 10 deletions modules/openapi-generator/src/main/resources/go/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,15 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
if reflect.TypeOf(t).Kind() == reflect.Slice {
s := reflect.ValueOf(t)
for i := 0; i < s.Len(); i++ {
parameterAddToHeaderOrQuery(localVarQueryParams, "{{{baseName}}}", s.Index(i).Interface(), "{{collectionFormat}}")
parameterAddToHeaderOrQuery(localVarQueryParams, "{{{baseName}}}", s.Index(i).Interface(), "{{style}}", "{{collectionFormat}}")
}
} else {
parameterAddToHeaderOrQuery(localVarQueryParams, "{{{baseName}}}", t, "{{collectionFormat}}")
parameterAddToHeaderOrQuery(localVarQueryParams, "{{{baseName}}}", t, "{{style}}", "{{collectionFormat}}")
}
}
{{/isCollectionFormatMulti}}
{{^isCollectionFormatMulti}}
parameterAddToHeaderOrQuery(localVarQueryParams, "{{{baseName}}}", r.{{paramName}}, "{{collectionFormat}}")
parameterAddToHeaderOrQuery(localVarQueryParams, "{{{baseName}}}", r.{{paramName}}, "{{style}}", "{{collectionFormat}}")
{{/isCollectionFormatMulti}}
{{/required}}
{{^required}}
Expand All @@ -207,14 +207,14 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
if reflect.TypeOf(t).Kind() == reflect.Slice {
s := reflect.ValueOf(t)
for i := 0; i < s.Len(); i++ {
parameterAddToHeaderOrQuery(localVarQueryParams, "{{{baseName}}}", s.Index(i).Interface(), "{{collectionFormat}}")
parameterAddToHeaderOrQuery(localVarQueryParams, "{{{baseName}}}", s.Index(i).Interface(), "{{style}}", "{{collectionFormat}}")
}
} else {
parameterAddToHeaderOrQuery(localVarQueryParams, "{{{baseName}}}", t, "{{collectionFormat}}")
parameterAddToHeaderOrQuery(localVarQueryParams, "{{{baseName}}}", t, "{{style}}", "{{collectionFormat}}")
}
{{/isCollectionFormatMulti}}
{{^isCollectionFormatMulti}}
parameterAddToHeaderOrQuery(localVarQueryParams, "{{{baseName}}}", r.{{paramName}}, "{{collectionFormat}}")
parameterAddToHeaderOrQuery(localVarQueryParams, "{{{baseName}}}", r.{{paramName}}, "{{style}}", "{{collectionFormat}}")
{{/isCollectionFormatMulti}}
{{#defaultValue}}} else {
var defaultValue {{{dataType}}} = {{{.}}}
Expand Down Expand Up @@ -245,11 +245,11 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
}
{{#headerParams}}
{{#required}}
parameterAddToHeaderOrQuery(localVarHeaderParams, "{{{baseName}}}", r.{{paramName}}, "{{collectionFormat}}")
parameterAddToHeaderOrQuery(localVarHeaderParams, "{{{baseName}}}", r.{{paramName}}, "{{style}}", "{{collectionFormat}}")
{{/required}}
{{^required}}
if r.{{paramName}} != nil {
parameterAddToHeaderOrQuery(localVarHeaderParams, "{{{baseName}}}", r.{{paramName}}, "{{collectionFormat}}")
parameterAddToHeaderOrQuery(localVarHeaderParams, "{{{baseName}}}", r.{{paramName}}, "{{style}}", "{{collectionFormat}}")
}
{{/required}}
{{/headerParams}}
Expand Down Expand Up @@ -295,7 +295,7 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
{{/isFile}}
{{^isFile}}
{{#required}}
parameterAddToHeaderOrQuery(localVarFormParams, "{{{baseName}}}", r.{{paramName}}, "{{collectionFormat}}")
parameterAddToHeaderOrQuery(localVarFormParams, "{{{baseName}}}", r.{{paramName}}, "{{style}}", "{{collectionFormat}}")
{{/required}}
{{^required}}
{{#isModel}}
Expand All @@ -309,7 +309,7 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
{{/isModel}}
{{^isModel}}
if r.{{paramName}} != nil {
parameterAddToHeaderOrQuery(localVarFormParams, "{{{baseName}}}", r.{{paramName}}, "{{collectionFormat}}")
parameterAddToHeaderOrQuery(localVarFormParams, "{{{baseName}}}", r.{{paramName}}, "{{style}}", "{{collectionFormat}}")
}
{{/isModel}}
{{/required}}
Expand Down
16 changes: 10 additions & 6 deletions modules/openapi-generator/src/main/resources/go/client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func parameterValueToString( obj interface{}, key string ) string {

// parameterAddToHeaderOrQuery adds the provided object to the request header or url query
// supporting deep object syntax
func parameterAddToHeaderOrQuery(headerOrQueryParams interface{}, keyPrefix string, obj interface{}, collectionType string) {
func parameterAddToHeaderOrQuery(headerOrQueryParams interface{}, keyPrefix string, obj interface{}, style string, collectionType string) {
var v = reflect.ValueOf(obj)
var value = ""
if v == reflect.ValueOf(nil) {
Expand All @@ -168,11 +168,11 @@ func parameterAddToHeaderOrQuery(headerOrQueryParams interface{}, keyPrefix stri
if err != nil {
return
}
parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefix, dataMap, collectionType)
parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefix, dataMap, style, collectionType)
return
}
if t, ok := obj.(time.Time); ok {
parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefix, t.Format(time.RFC3339Nano), collectionType)
parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefix, t.Format(time.RFC3339Nano), style, collectionType)
return
}
value = v.Type().String() + " value"
Expand All @@ -184,7 +184,11 @@ func parameterAddToHeaderOrQuery(headerOrQueryParams interface{}, keyPrefix stri
var lenIndValue = indValue.Len()
for i:=0;i<lenIndValue;i++ {
var arrayValue = indValue.Index(i)
parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefix, arrayValue.Interface(), collectionType)
var keyPrefixForCollectionType = keyPrefix
if style == "deepObject" {
keyPrefixForCollectionType = keyPrefix + "[" + strconv.Itoa(i) + "]"
}
parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefixForCollectionType, arrayValue.Interface(), style, collectionType)
}
return

Expand All @@ -196,14 +200,14 @@ func parameterAddToHeaderOrQuery(headerOrQueryParams interface{}, keyPrefix stri
iter := indValue.MapRange()
for iter.Next() {
k,v := iter.Key(), iter.Value()
parameterAddToHeaderOrQuery(headerOrQueryParams, fmt.Sprintf("%s[%s]", keyPrefix, k.String()), v.Interface(), collectionType)
parameterAddToHeaderOrQuery(headerOrQueryParams, fmt.Sprintf("%s[%s]", keyPrefix, k.String()), v.Interface(), style, collectionType)
}
return

case reflect.Interface:
fallthrough
case reflect.Ptr:
parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefix, v.Elem().Interface(), collectionType)
parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefix, v.Elem().Interface(), style, collectionType)
return

case reflect.Int, reflect.Int8, reflect.Int16,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{{#discriminator}}
// checks if the {{classname}} type satisfies the MappedNullable interface at compile time
var _ MappedNullable = &{{classname}}{}
{{/discriminator}}

// {{classname}} {{{description}}}{{^description}}struct for {{{classname}}}{{/description}}
type {{classname}} struct {
{{#anyOf}}
Expand Down Expand Up @@ -73,4 +78,16 @@ func (src *{{classname}}) MarshalJSON() ([]byte, error) {
return nil, nil // no data in anyOf schemas
}

{{#discriminator}}
func (src {{classname}}) ToMap() (map[string]interface{}, error) {
{{#anyOf}}
if src.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} != nil {
return src.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}}.ToMap()
}

{{/anyOf}}
return nil, nil // no data in anyOf schemas
}
{{/discriminator}}

{{>nullable_model}}
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,22 @@ paths:
responses:
'200':
description: OK
/fake/deep_object_anyof_test:
get:
tags:
- fake
operationId: test_query_deep_object_anyof
parameters:
- name: filter
in: query
required: false
style: deepObject
schema:
$ref: '#/components/schemas/FilterAny'
explode: false
responses:
'200':
description: OK
/fake/parameter-name-mapping:
get:
tags:
Expand Down Expand Up @@ -1375,6 +1391,41 @@ components:
type: http
scheme: signature
schemas:
FilterTypeRegex:
type: object
properties:
type:
enum:
- set
- range
type: string
regex:
type: string
required:
- type
FilterTypeRange:
type: object
properties:
type:
enum:
- set
- range
type: string
data:
type: array
items:
type: string
required:
- type
FilterAny:
anyOf:
- $ref: '#/components/schemas/FilterTypeRegex'
- $ref: '#/components/schemas/FilterTypeRange'
discriminator:
mapping:
set: '#/components/schemas/FilterTypeRegex'
range: '#/components/schemas/FilterTypeRange'
propertyName: type
MapWithDateTime:
type: object
additionalProperties:
Expand Down
18 changes: 9 additions & 9 deletions samples/client/echo_api/go-external-refs/api_form.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions samples/client/echo_api/go-external-refs/api_header.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 15 additions & 15 deletions samples/client/echo_api/go-external-refs/api_query.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading