[Golang] Introduce 'lambda.type-to-name' in models_anyof.mustache (#18912)

* Update model_anyof.mustache

* add tests for anyof in go client

---------

Co-authored-by: Martin Lakov <85170913+martinlakov@users.noreply.github.com>
This commit is contained in:
William Cheng 2024-06-13 16:01:58 +08:00 committed by GitHub
parent 3abe2512c5
commit 1765efc1ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 204 additions and 10 deletions

View File

@ -1,7 +1,7 @@
// {{classname}} {{{description}}}{{^description}}struct for {{{classname}}}{{/description}}
type {{classname}} struct {
{{#anyOf}}
{{{.}}} *{{{.}}}
{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} *{{{.}}}
{{/anyOf}}
}
@ -45,17 +45,17 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
{{/mappedModels}}
{{/discriminator}}
{{#anyOf}}
// try to unmarshal JSON data into {{{.}}}
err = json.Unmarshal(data, &dst.{{{.}}});
// try to unmarshal JSON data into {{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}}
err = json.Unmarshal(data, &dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}});
if err == nil {
json{{{.}}}, _ := json.Marshal(dst.{{{.}}})
if string(json{{{.}}}) == "{}" { // empty struct
dst.{{{.}}} = nil
json{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}}, _ := json.Marshal(dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}})
if string(json{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}}) == "{}" { // empty struct
dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} = nil
} else {
return nil // data stored in dst.{{{.}}}, return on the first match
return nil // data stored in dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}}, return on the first match
}
} else {
dst.{{{.}}} = nil
dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} = nil
}
{{/anyOf}}
@ -65,8 +65,8 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
// Marshal data from the first non-nil pointers in the struct to JSON
func (src *{{classname}}) MarshalJSON() ([]byte, error) {
{{#anyOf}}
if src.{{{.}}} != nil {
return json.Marshal(&src.{{{.}}})
if src.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} != nil {
return json.Marshal(&src.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}})
}
{{/anyOf}}

View File

@ -2125,6 +2125,12 @@ components:
type: array
items:
type: string
AnyOfPrimitiveType:
anyOf:
- $ref: '#/components/schemas/OneOfPrimitiveTypeChild'
- type: integer
format: int32
- $ref: '#/components/schemas/OneOfArrayOfString'
# `dup-prop` is defined in both parent and child models
# but not identical: optional vs required
DuplicatedPropChild:

View File

@ -15,6 +15,7 @@ docs/AdditionalPropertiesClass.md
docs/AllOfPrimitiveTypes.md
docs/Animal.md
docs/AnotherFakeAPI.md
docs/AnyOfPrimitiveType.md
docs/ApiResponse.md
docs/Apple.md
docs/AppleReq.md
@ -91,6 +92,7 @@ model__special_model_name_.go
model_additional_properties_class.go
model_all_of_primitive_types.go
model_animal.go
model_any_of_primitive_type.go
model_api_response.go
model_apple.go
model_apple_req.go

View File

@ -130,6 +130,7 @@ Class | Method | HTTP request | Description
- [AdditionalPropertiesClass](docs/AdditionalPropertiesClass.md)
- [AllOfPrimitiveTypes](docs/AllOfPrimitiveTypes.md)
- [Animal](docs/Animal.md)
- [AnyOfPrimitiveType](docs/AnyOfPrimitiveType.md)
- [ApiResponse](docs/ApiResponse.md)
- [Apple](docs/Apple.md)
- [AppleReq](docs/AppleReq.md)

View File

@ -2113,6 +2113,12 @@ components:
items:
type: string
type: array
AnyOfPrimitiveType:
anyOf:
- $ref: '#/components/schemas/OneOfPrimitiveTypeChild'
- format: int32
type: integer
- $ref: '#/components/schemas/OneOfArrayOfString'
DuplicatedPropChild:
allOf:
- $ref: '#/components/schemas/DuplicatedPropParent'

View File

@ -0,0 +1,56 @@
# AnyOfPrimitiveType
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Name** | Pointer to **string** | | [optional]
## Methods
### NewAnyOfPrimitiveType
`func NewAnyOfPrimitiveType() *AnyOfPrimitiveType`
NewAnyOfPrimitiveType instantiates a new AnyOfPrimitiveType object
This constructor will assign default values to properties that have it defined,
and makes sure properties required by API are set, but the set of arguments
will change when the set of required properties is changed
### NewAnyOfPrimitiveTypeWithDefaults
`func NewAnyOfPrimitiveTypeWithDefaults() *AnyOfPrimitiveType`
NewAnyOfPrimitiveTypeWithDefaults instantiates a new AnyOfPrimitiveType object
This constructor will only assign default values to properties that have it defined,
but it doesn't guarantee that properties required by API are set
### GetName
`func (o *AnyOfPrimitiveType) GetName() string`
GetName returns the Name field if non-nil, zero value otherwise.
### GetNameOk
`func (o *AnyOfPrimitiveType) GetNameOk() (*string, bool)`
GetNameOk returns a tuple with the Name field if it's non-nil, zero value otherwise
and a boolean to check if the value has been set.
### SetName
`func (o *AnyOfPrimitiveType) SetName(v string)`
SetName sets Name field to given value.
### HasName
`func (o *AnyOfPrimitiveType) HasName() bool`
HasName returns a boolean if a field has been set.
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

View File

@ -0,0 +1,123 @@
/*
OpenAPI Petstore
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
API version: 1.0.0
*/
// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.
package petstore
import (
"encoding/json"
"fmt"
)
// AnyOfPrimitiveType struct for AnyOfPrimitiveType
type AnyOfPrimitiveType struct {
OneOfPrimitiveTypeChild *OneOfPrimitiveTypeChild
ArrayOfString *[]string
Int32 *int32
}
// Unmarshal JSON data into any of the pointers in the struct
func (dst *AnyOfPrimitiveType) UnmarshalJSON(data []byte) error {
var err error
// try to unmarshal JSON data into OneOfPrimitiveTypeChild
err = json.Unmarshal(data, &dst.OneOfPrimitiveTypeChild);
if err == nil {
jsonOneOfPrimitiveTypeChild, _ := json.Marshal(dst.OneOfPrimitiveTypeChild)
if string(jsonOneOfPrimitiveTypeChild) == "{}" { // empty struct
dst.OneOfPrimitiveTypeChild = nil
} else {
return nil // data stored in dst.OneOfPrimitiveTypeChild, return on the first match
}
} else {
dst.OneOfPrimitiveTypeChild = nil
}
// try to unmarshal JSON data into ArrayOfString
err = json.Unmarshal(data, &dst.ArrayOfString);
if err == nil {
jsonArrayOfString, _ := json.Marshal(dst.ArrayOfString)
if string(jsonArrayOfString) == "{}" { // empty struct
dst.ArrayOfString = nil
} else {
return nil // data stored in dst.ArrayOfString, return on the first match
}
} else {
dst.ArrayOfString = nil
}
// try to unmarshal JSON data into Int32
err = json.Unmarshal(data, &dst.Int32);
if err == nil {
jsonInt32, _ := json.Marshal(dst.Int32)
if string(jsonInt32) == "{}" { // empty struct
dst.Int32 = nil
} else {
return nil // data stored in dst.Int32, return on the first match
}
} else {
dst.Int32 = nil
}
return fmt.Errorf("data failed to match schemas in anyOf(AnyOfPrimitiveType)")
}
// Marshal data from the first non-nil pointers in the struct to JSON
func (src *AnyOfPrimitiveType) MarshalJSON() ([]byte, error) {
if src.OneOfPrimitiveTypeChild != nil {
return json.Marshal(&src.OneOfPrimitiveTypeChild)
}
if src.ArrayOfString != nil {
return json.Marshal(&src.ArrayOfString)
}
if src.Int32 != nil {
return json.Marshal(&src.Int32)
}
return nil, nil // no data in anyOf schemas
}
type NullableAnyOfPrimitiveType struct {
value *AnyOfPrimitiveType
isSet bool
}
func (v NullableAnyOfPrimitiveType) Get() *AnyOfPrimitiveType {
return v.value
}
func (v *NullableAnyOfPrimitiveType) Set(val *AnyOfPrimitiveType) {
v.value = val
v.isSet = true
}
func (v NullableAnyOfPrimitiveType) IsSet() bool {
return v.isSet
}
func (v *NullableAnyOfPrimitiveType) Unset() {
v.value = nil
v.isSet = false
}
func NewNullableAnyOfPrimitiveType(val *AnyOfPrimitiveType) *NullableAnyOfPrimitiveType {
return &NullableAnyOfPrimitiveType{value: val, isSet: true}
}
func (v NullableAnyOfPrimitiveType) MarshalJSON() ([]byte, error) {
return json.Marshal(v.value)
}
func (v *NullableAnyOfPrimitiveType) UnmarshalJSON(src []byte) error {
v.isSet = true
return json.Unmarshal(src, &v.value)
}