[BUG][GO] use value receiver for JSON marshal (#19962)

* chore(go): add failing test for JSON marshalling

Adds a small schema (FruitJuice) which contains a required gmFruit,
which inherits using AnyOf. This fails to correctly marshal as JSON.

* fix(go): use non-pointer receiver for JSON marshal

In the case of a required anyOf property, JSON marshalling would has
been incorrect.

Required properties are not nullable, and thus always use value
receivers. For the single case of anyOf models, a pointer receiver was
used for MarshalJSON. All other instances of json marshalling use value
receivers.

This change is simply to use a value receiver instead of a pointer
receiver in the template for `MarshalJSON` on anyOf models.

---------

Co-authored-by: Per Hallgren <perhallgren@users.noreply.github.com>
This commit is contained in:
Per Hallgren 2024-11-27 11:09:11 +01:00 committed by GitHub
parent 705261978d
commit 8a94fc667e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
20 changed files with 348 additions and 214 deletions

View File

@ -68,7 +68,7 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
} }
// Marshal data from the first non-nil pointers in the struct to JSON // Marshal data from the first non-nil pointers in the struct to JSON
func (src *{{classname}}) MarshalJSON() ([]byte, error) { func (src {{classname}}) MarshalJSON() ([]byte, error) {
{{#anyOf}} {{#anyOf}}
if src.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} != nil { if src.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} != nil {
return json.Marshal(&src.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}}) return json.Marshal(&src.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}})

View File

@ -1394,11 +1394,6 @@ components:
FilterTypeRegex: FilterTypeRegex:
type: object type: object
properties: properties:
type:
enum:
- set
- range
type: string
regex: regex:
type: string type: string
required: required:
@ -1406,11 +1401,6 @@ components:
FilterTypeRange: FilterTypeRange:
type: object type: object
properties: properties:
type:
enum:
- set
- range
type: string
data: data:
type: array type: array
items: items:
@ -1425,7 +1415,16 @@ components:
mapping: mapping:
set: '#/components/schemas/FilterTypeRegex' set: '#/components/schemas/FilterTypeRegex'
range: '#/components/schemas/FilterTypeRange' range: '#/components/schemas/FilterTypeRange'
propertyName: type propertyName: type
properties:
type:
enum:
- set
- range
type: string
date:
type: string
format: date-time
MapWithDateTime: MapWithDateTime:
type: object type: object
additionalProperties: additionalProperties:
@ -2119,6 +2118,13 @@ components:
type: string type: string
required: required:
- className - className
fruitJuice:
type: object
required:
- fruit
properties:
fruit:
$ref: '#/components/schemas/gmFruit'
gmFruit: gmFruit:
properties: properties:
color: color:

View File

@ -55,7 +55,7 @@ func (dst *Object2) UnmarshalJSON(data []byte) error {
} }
// Marshal data from the first non-nil pointers in the struct to JSON // Marshal data from the first non-nil pointers in the struct to JSON
func (src *Object2) MarshalJSON() ([]byte, error) { func (src Object2) MarshalJSON() ([]byte, error) {
if src.NestedObject1 != nil { if src.NestedObject1 != nil {
return json.Marshal(&src.NestedObject1) return json.Marshal(&src.NestedObject1)
} }

View File

@ -47,6 +47,7 @@ docs/Foo.md
docs/FooGetDefaultResponse.md docs/FooGetDefaultResponse.md
docs/FormatTest.md docs/FormatTest.md
docs/Fruit.md docs/Fruit.md
docs/FruitJuice.md
docs/FruitReq.md docs/FruitReq.md
docs/GmFruit.md docs/GmFruit.md
docs/HasOnlyReadOnly.md docs/HasOnlyReadOnly.md
@ -126,6 +127,7 @@ model_filter_type_regex.go
model_foo.go model_foo.go
model_format_test_.go model_format_test_.go
model_fruit.go model_fruit.go
model_fruit_juice.go
model_fruit_req.go model_fruit_req.go
model_gm_fruit.go model_gm_fruit.go
model_has_only_read_only.go model_has_only_read_only.go

View File

@ -161,6 +161,7 @@ Class | Method | HTTP request | Description
- [FooGetDefaultResponse](docs/FooGetDefaultResponse.md) - [FooGetDefaultResponse](docs/FooGetDefaultResponse.md)
- [FormatTest](docs/FormatTest.md) - [FormatTest](docs/FormatTest.md)
- [Fruit](docs/Fruit.md) - [Fruit](docs/Fruit.md)
- [FruitJuice](docs/FruitJuice.md)
- [FruitReq](docs/FruitReq.md) - [FruitReq](docs/FruitReq.md)
- [GmFruit](docs/GmFruit.md) - [GmFruit](docs/GmFruit.md)
- [HasOnlyReadOnly](docs/HasOnlyReadOnly.md) - [HasOnlyReadOnly](docs/HasOnlyReadOnly.md)

View File

@ -1296,11 +1296,6 @@ components:
schemas: schemas:
FilterTypeRegex: FilterTypeRegex:
properties: properties:
type:
enum:
- set
- range
type: string
regex: regex:
type: string type: string
required: required:
@ -1308,11 +1303,6 @@ components:
type: object type: object
FilterTypeRange: FilterTypeRange:
properties: properties:
type:
enum:
- set
- range
type: string
data: data:
items: items:
type: string type: string
@ -1329,6 +1319,15 @@ components:
set: '#/components/schemas/FilterTypeRegex' set: '#/components/schemas/FilterTypeRegex'
range: '#/components/schemas/FilterTypeRange' range: '#/components/schemas/FilterTypeRange'
propertyName: type propertyName: type
properties:
type:
enum:
- set
- range
type: string
date:
format: date-time
type: string
MapWithDateTime: MapWithDateTime:
additionalProperties: additionalProperties:
items: items:
@ -2101,6 +2100,13 @@ components:
required: required:
- className - className
type: object type: object
fruitJuice:
properties:
fruit:
$ref: '#/components/schemas/gmFruit'
required:
- fruit
type: object
gmFruit: gmFruit:
anyOf: anyOf:
- $ref: '#/components/schemas/apple' - $ref: '#/components/schemas/apple'

View File

@ -1197,7 +1197,7 @@ import (
) )
func main() { func main() {
filter := *openapiclient.NewFilterAny("Type_example") // FilterAny | (optional) filter := *openapiclient.NewFilterAny() // FilterAny | (optional)
configuration := openapiclient.NewConfiguration() configuration := openapiclient.NewConfiguration()
apiClient := openapiclient.NewAPIClient(configuration) apiClient := openapiclient.NewAPIClient(configuration)

View File

@ -4,7 +4,8 @@
Name | Type | Description | Notes Name | Type | Description | Notes
------------ | ------------- | ------------- | ------------- ------------ | ------------- | ------------- | -------------
**Type** | **string** | | **Type** | Pointer to **string** | | [optional]
**Date** | Pointer to **time.Time** | | [optional]
**Regex** | Pointer to **string** | | [optional] **Regex** | Pointer to **string** | | [optional]
**Data** | Pointer to **[]string** | | [optional] **Data** | Pointer to **[]string** | | [optional]
@ -12,7 +13,7 @@ Name | Type | Description | Notes
### NewFilterAny ### NewFilterAny
`func NewFilterAny(type_ string, ) *FilterAny` `func NewFilterAny() *FilterAny`
NewFilterAny instantiates a new FilterAny object NewFilterAny instantiates a new FilterAny object
This constructor will assign default values to properties that have it defined, This constructor will assign default values to properties that have it defined,
@ -46,6 +47,36 @@ and a boolean to check if the value has been set.
SetType sets Type field to given value. SetType sets Type field to given value.
### HasType
`func (o *FilterAny) HasType() bool`
HasType returns a boolean if a field has been set.
### GetDate
`func (o *FilterAny) GetDate() time.Time`
GetDate returns the Date field if non-nil, zero value otherwise.
### GetDateOk
`func (o *FilterAny) GetDateOk() (*time.Time, bool)`
GetDateOk returns a tuple with the Date field if it's non-nil, zero value otherwise
and a boolean to check if the value has been set.
### SetDate
`func (o *FilterAny) SetDate(v time.Time)`
SetDate sets Date field to given value.
### HasDate
`func (o *FilterAny) HasDate() bool`
HasDate returns a boolean if a field has been set.
### GetRegex ### GetRegex

View File

@ -4,14 +4,13 @@
Name | Type | Description | Notes Name | Type | Description | Notes
------------ | ------------- | ------------- | ------------- ------------ | ------------- | ------------- | -------------
**Type** | **string** | |
**Data** | Pointer to **[]string** | | [optional] **Data** | Pointer to **[]string** | | [optional]
## Methods ## Methods
### NewFilterTypeRange ### NewFilterTypeRange
`func NewFilterTypeRange(type_ string, ) *FilterTypeRange` `func NewFilterTypeRange() *FilterTypeRange`
NewFilterTypeRange instantiates a new FilterTypeRange object NewFilterTypeRange instantiates a new FilterTypeRange object
This constructor will assign default values to properties that have it defined, This constructor will assign default values to properties that have it defined,
@ -26,26 +25,6 @@ NewFilterTypeRangeWithDefaults instantiates a new FilterTypeRange object
This constructor will only assign default values to properties that have it defined, 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 but it doesn't guarantee that properties required by API are set
### GetType
`func (o *FilterTypeRange) GetType() string`
GetType returns the Type field if non-nil, zero value otherwise.
### GetTypeOk
`func (o *FilterTypeRange) GetTypeOk() (*string, bool)`
GetTypeOk returns a tuple with the Type field if it's non-nil, zero value otherwise
and a boolean to check if the value has been set.
### SetType
`func (o *FilterTypeRange) SetType(v string)`
SetType sets Type field to given value.
### GetData ### GetData
`func (o *FilterTypeRange) GetData() []string` `func (o *FilterTypeRange) GetData() []string`

View File

@ -4,14 +4,13 @@
Name | Type | Description | Notes Name | Type | Description | Notes
------------ | ------------- | ------------- | ------------- ------------ | ------------- | ------------- | -------------
**Type** | **string** | |
**Regex** | Pointer to **string** | | [optional] **Regex** | Pointer to **string** | | [optional]
## Methods ## Methods
### NewFilterTypeRegex ### NewFilterTypeRegex
`func NewFilterTypeRegex(type_ string, ) *FilterTypeRegex` `func NewFilterTypeRegex() *FilterTypeRegex`
NewFilterTypeRegex instantiates a new FilterTypeRegex object NewFilterTypeRegex instantiates a new FilterTypeRegex object
This constructor will assign default values to properties that have it defined, This constructor will assign default values to properties that have it defined,
@ -26,26 +25,6 @@ NewFilterTypeRegexWithDefaults instantiates a new FilterTypeRegex object
This constructor will only assign default values to properties that have it defined, 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 but it doesn't guarantee that properties required by API are set
### GetType
`func (o *FilterTypeRegex) GetType() string`
GetType returns the Type field if non-nil, zero value otherwise.
### GetTypeOk
`func (o *FilterTypeRegex) GetTypeOk() (*string, bool)`
GetTypeOk returns a tuple with the Type field if it's non-nil, zero value otherwise
and a boolean to check if the value has been set.
### SetType
`func (o *FilterTypeRegex) SetType(v string)`
SetType sets Type field to given value.
### GetRegex ### GetRegex
`func (o *FilterTypeRegex) GetRegex() string` `func (o *FilterTypeRegex) GetRegex() string`

View File

@ -0,0 +1,51 @@
# FruitJuice
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Fruit** | [**GmFruit**](GmFruit.md) | |
## Methods
### NewFruitJuice
`func NewFruitJuice(fruit GmFruit, ) *FruitJuice`
NewFruitJuice instantiates a new FruitJuice 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
### NewFruitJuiceWithDefaults
`func NewFruitJuiceWithDefaults() *FruitJuice`
NewFruitJuiceWithDefaults instantiates a new FruitJuice 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
### GetFruit
`func (o *FruitJuice) GetFruit() GmFruit`
GetFruit returns the Fruit field if non-nil, zero value otherwise.
### GetFruitOk
`func (o *FruitJuice) GetFruitOk() (*GmFruit, bool)`
GetFruitOk returns a tuple with the Fruit field if it's non-nil, zero value otherwise
and a boolean to check if the value has been set.
### SetFruit
`func (o *FruitJuice) SetFruit(v GmFruit)`
SetFruit sets Fruit field to given value.
[[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

@ -69,7 +69,7 @@ func (dst *AnyOfPrimitiveType) UnmarshalJSON(data []byte) error {
} }
// Marshal data from the first non-nil pointers in the struct to JSON // Marshal data from the first non-nil pointers in the struct to JSON
func (src *AnyOfPrimitiveType) MarshalJSON() ([]byte, error) { func (src AnyOfPrimitiveType) MarshalJSON() ([]byte, error) {
if src.OneOfPrimitiveTypeChild != nil { if src.OneOfPrimitiveTypeChild != nil {
return json.Marshal(&src.OneOfPrimitiveTypeChild) return json.Marshal(&src.OneOfPrimitiveTypeChild)
} }

View File

@ -128,7 +128,7 @@ func (dst *FilterAny) UnmarshalJSON(data []byte) error {
} }
// Marshal data from the first non-nil pointers in the struct to JSON // Marshal data from the first non-nil pointers in the struct to JSON
func (src *FilterAny) MarshalJSON() ([]byte, error) { func (src FilterAny) MarshalJSON() ([]byte, error) {
if src.FilterTypeRange != nil { if src.FilterTypeRange != nil {
return json.Marshal(&src.FilterTypeRange) return json.Marshal(&src.FilterTypeRange)
} }

View File

@ -12,7 +12,6 @@ package petstore
import ( import (
"encoding/json" "encoding/json"
"fmt"
) )
// checks if the FilterTypeRange type satisfies the MappedNullable interface at compile time // checks if the FilterTypeRange type satisfies the MappedNullable interface at compile time
@ -20,7 +19,6 @@ var _ MappedNullable = &FilterTypeRange{}
// FilterTypeRange struct for FilterTypeRange // FilterTypeRange struct for FilterTypeRange
type FilterTypeRange struct { type FilterTypeRange struct {
Type string `json:"type"`
Data []string `json:"data,omitempty"` Data []string `json:"data,omitempty"`
AdditionalProperties map[string]interface{} AdditionalProperties map[string]interface{}
} }
@ -31,9 +29,8 @@ type _FilterTypeRange FilterTypeRange
// This constructor will assign default values to properties that have it defined, // 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 // and makes sure properties required by API are set, but the set of arguments
// will change when the set of required properties is changed // will change when the set of required properties is changed
func NewFilterTypeRange(type_ string) *FilterTypeRange { func NewFilterTypeRange() *FilterTypeRange {
this := FilterTypeRange{} this := FilterTypeRange{}
this.Type = type_
return &this return &this
} }
@ -45,31 +42,6 @@ func NewFilterTypeRangeWithDefaults() *FilterTypeRange {
return &this return &this
} }
// GetType returns the Type field value
func (o *FilterTypeRange) GetType() string {
if o == nil {
var ret string
return ret
}
return o.Type
}
// GetTypeOk returns a tuple with the Type field value
// and a boolean to check if the value has been set.
func (o *FilterTypeRange) GetTypeOk() (*string, bool) {
if o == nil {
return nil, false
}
return &o.Type, true
}
// SetType sets field value
func (o *FilterTypeRange) SetType(v string) {
o.Type = v
}
// GetData returns the Data field value if set, zero value otherwise. // GetData returns the Data field value if set, zero value otherwise.
func (o *FilterTypeRange) GetData() []string { func (o *FilterTypeRange) GetData() []string {
if o == nil || IsNil(o.Data) { if o == nil || IsNil(o.Data) {
@ -112,7 +84,6 @@ func (o FilterTypeRange) MarshalJSON() ([]byte, error) {
func (o FilterTypeRange) ToMap() (map[string]interface{}, error) { func (o FilterTypeRange) ToMap() (map[string]interface{}, error) {
toSerialize := map[string]interface{}{} toSerialize := map[string]interface{}{}
toSerialize["type"] = o.Type
if !IsNil(o.Data) { if !IsNil(o.Data) {
toSerialize["data"] = o.Data toSerialize["data"] = o.Data
} }
@ -125,44 +96,6 @@ func (o FilterTypeRange) ToMap() (map[string]interface{}, error) {
} }
func (o *FilterTypeRange) UnmarshalJSON(data []byte) (err error) { func (o *FilterTypeRange) UnmarshalJSON(data []byte) (err error) {
// This validates that all required properties are included in the JSON object
// by unmarshalling the object into a generic map with string keys and checking
// that every required field exists as a key in the generic map.
requiredProperties := []string{
"type",
}
// defaultValueFuncMap captures the default values for required properties.
// These values are used when required properties are missing from the payload.
defaultValueFuncMap := map[string]func() interface{} {
}
var defaultValueApplied bool
allProperties := make(map[string]interface{})
err = json.Unmarshal(data, &allProperties)
if err != nil {
return err;
}
for _, requiredProperty := range(requiredProperties) {
if value, exists := allProperties[requiredProperty]; !exists || value == "" {
if _, ok := defaultValueFuncMap[requiredProperty]; ok {
allProperties[requiredProperty] = defaultValueFuncMap[requiredProperty]()
defaultValueApplied = true
}
}
if value, exists := allProperties[requiredProperty]; !exists || value == ""{
return fmt.Errorf("no value given for required property %v", requiredProperty)
}
}
if defaultValueApplied {
data, err = json.Marshal(allProperties)
if err != nil{
return err
}
}
varFilterTypeRange := _FilterTypeRange{} varFilterTypeRange := _FilterTypeRange{}
err = json.Unmarshal(data, &varFilterTypeRange) err = json.Unmarshal(data, &varFilterTypeRange)
@ -176,7 +109,6 @@ func (o *FilterTypeRange) UnmarshalJSON(data []byte) (err error) {
additionalProperties := make(map[string]interface{}) additionalProperties := make(map[string]interface{})
if err = json.Unmarshal(data, &additionalProperties); err == nil { if err = json.Unmarshal(data, &additionalProperties); err == nil {
delete(additionalProperties, "type")
delete(additionalProperties, "data") delete(additionalProperties, "data")
o.AdditionalProperties = additionalProperties o.AdditionalProperties = additionalProperties
} }

View File

@ -12,7 +12,6 @@ package petstore
import ( import (
"encoding/json" "encoding/json"
"fmt"
) )
// checks if the FilterTypeRegex type satisfies the MappedNullable interface at compile time // checks if the FilterTypeRegex type satisfies the MappedNullable interface at compile time
@ -20,7 +19,6 @@ var _ MappedNullable = &FilterTypeRegex{}
// FilterTypeRegex struct for FilterTypeRegex // FilterTypeRegex struct for FilterTypeRegex
type FilterTypeRegex struct { type FilterTypeRegex struct {
Type string `json:"type"`
Regex *string `json:"regex,omitempty"` Regex *string `json:"regex,omitempty"`
AdditionalProperties map[string]interface{} AdditionalProperties map[string]interface{}
} }
@ -31,9 +29,8 @@ type _FilterTypeRegex FilterTypeRegex
// This constructor will assign default values to properties that have it defined, // 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 // and makes sure properties required by API are set, but the set of arguments
// will change when the set of required properties is changed // will change when the set of required properties is changed
func NewFilterTypeRegex(type_ string) *FilterTypeRegex { func NewFilterTypeRegex() *FilterTypeRegex {
this := FilterTypeRegex{} this := FilterTypeRegex{}
this.Type = type_
return &this return &this
} }
@ -45,31 +42,6 @@ func NewFilterTypeRegexWithDefaults() *FilterTypeRegex {
return &this return &this
} }
// GetType returns the Type field value
func (o *FilterTypeRegex) GetType() string {
if o == nil {
var ret string
return ret
}
return o.Type
}
// GetTypeOk returns a tuple with the Type field value
// and a boolean to check if the value has been set.
func (o *FilterTypeRegex) GetTypeOk() (*string, bool) {
if o == nil {
return nil, false
}
return &o.Type, true
}
// SetType sets field value
func (o *FilterTypeRegex) SetType(v string) {
o.Type = v
}
// GetRegex returns the Regex field value if set, zero value otherwise. // GetRegex returns the Regex field value if set, zero value otherwise.
func (o *FilterTypeRegex) GetRegex() string { func (o *FilterTypeRegex) GetRegex() string {
if o == nil || IsNil(o.Regex) { if o == nil || IsNil(o.Regex) {
@ -112,7 +84,6 @@ func (o FilterTypeRegex) MarshalJSON() ([]byte, error) {
func (o FilterTypeRegex) ToMap() (map[string]interface{}, error) { func (o FilterTypeRegex) ToMap() (map[string]interface{}, error) {
toSerialize := map[string]interface{}{} toSerialize := map[string]interface{}{}
toSerialize["type"] = o.Type
if !IsNil(o.Regex) { if !IsNil(o.Regex) {
toSerialize["regex"] = o.Regex toSerialize["regex"] = o.Regex
} }
@ -125,44 +96,6 @@ func (o FilterTypeRegex) ToMap() (map[string]interface{}, error) {
} }
func (o *FilterTypeRegex) UnmarshalJSON(data []byte) (err error) { func (o *FilterTypeRegex) UnmarshalJSON(data []byte) (err error) {
// This validates that all required properties are included in the JSON object
// by unmarshalling the object into a generic map with string keys and checking
// that every required field exists as a key in the generic map.
requiredProperties := []string{
"type",
}
// defaultValueFuncMap captures the default values for required properties.
// These values are used when required properties are missing from the payload.
defaultValueFuncMap := map[string]func() interface{} {
}
var defaultValueApplied bool
allProperties := make(map[string]interface{})
err = json.Unmarshal(data, &allProperties)
if err != nil {
return err;
}
for _, requiredProperty := range(requiredProperties) {
if value, exists := allProperties[requiredProperty]; !exists || value == "" {
if _, ok := defaultValueFuncMap[requiredProperty]; ok {
allProperties[requiredProperty] = defaultValueFuncMap[requiredProperty]()
defaultValueApplied = true
}
}
if value, exists := allProperties[requiredProperty]; !exists || value == ""{
return fmt.Errorf("no value given for required property %v", requiredProperty)
}
}
if defaultValueApplied {
data, err = json.Marshal(allProperties)
if err != nil{
return err
}
}
varFilterTypeRegex := _FilterTypeRegex{} varFilterTypeRegex := _FilterTypeRegex{}
err = json.Unmarshal(data, &varFilterTypeRegex) err = json.Unmarshal(data, &varFilterTypeRegex)
@ -176,7 +109,6 @@ func (o *FilterTypeRegex) UnmarshalJSON(data []byte) (err error) {
additionalProperties := make(map[string]interface{}) additionalProperties := make(map[string]interface{})
if err = json.Unmarshal(data, &additionalProperties); err == nil { if err = json.Unmarshal(data, &additionalProperties); err == nil {
delete(additionalProperties, "type")
delete(additionalProperties, "regex") delete(additionalProperties, "regex")
o.AdditionalProperties = additionalProperties o.AdditionalProperties = additionalProperties
} }

View File

@ -0,0 +1,186 @@
/*
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"
)
// checks if the FruitJuice type satisfies the MappedNullable interface at compile time
var _ MappedNullable = &FruitJuice{}
// FruitJuice struct for FruitJuice
type FruitJuice struct {
Fruit GmFruit `json:"fruit"`
AdditionalProperties map[string]interface{}
}
type _FruitJuice FruitJuice
// NewFruitJuice instantiates a new FruitJuice 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
func NewFruitJuice(fruit GmFruit) *FruitJuice {
this := FruitJuice{}
this.Fruit = fruit
return &this
}
// NewFruitJuiceWithDefaults instantiates a new FruitJuice 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
func NewFruitJuiceWithDefaults() *FruitJuice {
this := FruitJuice{}
return &this
}
// GetFruit returns the Fruit field value
func (o *FruitJuice) GetFruit() GmFruit {
if o == nil {
var ret GmFruit
return ret
}
return o.Fruit
}
// GetFruitOk returns a tuple with the Fruit field value
// and a boolean to check if the value has been set.
func (o *FruitJuice) GetFruitOk() (*GmFruit, bool) {
if o == nil {
return nil, false
}
return &o.Fruit, true
}
// SetFruit sets field value
func (o *FruitJuice) SetFruit(v GmFruit) {
o.Fruit = v
}
func (o FruitJuice) MarshalJSON() ([]byte, error) {
toSerialize,err := o.ToMap()
if err != nil {
return []byte{}, err
}
return json.Marshal(toSerialize)
}
func (o FruitJuice) ToMap() (map[string]interface{}, error) {
toSerialize := map[string]interface{}{}
toSerialize["fruit"] = o.Fruit
for key, value := range o.AdditionalProperties {
toSerialize[key] = value
}
return toSerialize, nil
}
func (o *FruitJuice) UnmarshalJSON(data []byte) (err error) {
// This validates that all required properties are included in the JSON object
// by unmarshalling the object into a generic map with string keys and checking
// that every required field exists as a key in the generic map.
requiredProperties := []string{
"fruit",
}
// defaultValueFuncMap captures the default values for required properties.
// These values are used when required properties are missing from the payload.
defaultValueFuncMap := map[string]func() interface{} {
}
var defaultValueApplied bool
allProperties := make(map[string]interface{})
err = json.Unmarshal(data, &allProperties)
if err != nil {
return err;
}
for _, requiredProperty := range(requiredProperties) {
if value, exists := allProperties[requiredProperty]; !exists || value == "" {
if _, ok := defaultValueFuncMap[requiredProperty]; ok {
allProperties[requiredProperty] = defaultValueFuncMap[requiredProperty]()
defaultValueApplied = true
}
}
if value, exists := allProperties[requiredProperty]; !exists || value == ""{
return fmt.Errorf("no value given for required property %v", requiredProperty)
}
}
if defaultValueApplied {
data, err = json.Marshal(allProperties)
if err != nil{
return err
}
}
varFruitJuice := _FruitJuice{}
err = json.Unmarshal(data, &varFruitJuice)
if err != nil {
return err
}
*o = FruitJuice(varFruitJuice)
additionalProperties := make(map[string]interface{})
if err = json.Unmarshal(data, &additionalProperties); err == nil {
delete(additionalProperties, "fruit")
o.AdditionalProperties = additionalProperties
}
return err
}
type NullableFruitJuice struct {
value *FruitJuice
isSet bool
}
func (v NullableFruitJuice) Get() *FruitJuice {
return v.value
}
func (v *NullableFruitJuice) Set(val *FruitJuice) {
v.value = val
v.isSet = true
}
func (v NullableFruitJuice) IsSet() bool {
return v.isSet
}
func (v *NullableFruitJuice) Unset() {
v.value = nil
v.isSet = false
}
func NewNullableFruitJuice(val *FruitJuice) *NullableFruitJuice {
return &NullableFruitJuice{value: val, isSet: true}
}
func (v NullableFruitJuice) MarshalJSON() ([]byte, error) {
return json.Marshal(v.value)
}
func (v *NullableFruitJuice) UnmarshalJSON(src []byte) error {
v.isSet = true
return json.Unmarshal(src, &v.value)
}

View File

@ -55,7 +55,7 @@ func (dst *GmFruit) UnmarshalJSON(data []byte) error {
} }
// Marshal data from the first non-nil pointers in the struct to JSON // Marshal data from the first non-nil pointers in the struct to JSON
func (src *GmFruit) MarshalJSON() ([]byte, error) { func (src GmFruit) MarshalJSON() ([]byte, error) {
if src.Apple != nil { if src.Apple != nil {
return json.Marshal(&src.Apple) return json.Marshal(&src.Apple)
} }

View File

@ -6,10 +6,10 @@ replace go-petstore => ./go-petstore
require ( require (
cloud.google.com/go/compute v1.20.1 // indirect cloud.google.com/go/compute v1.20.1 // indirect
github.com/stretchr/testify v1.9.0 github.com/stretchr/testify v1.10.0
go-petstore v0.0.0-00010101000000-000000000000 go-petstore v0.0.0-00010101000000-000000000000
golang.org/x/net v0.27.0 // indirect golang.org/x/net v0.31.0 // indirect
golang.org/x/oauth2 v0.21.0 golang.org/x/oauth2 v0.24.0
google.golang.org/protobuf v1.31.0 // indirect google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/validator.v2 v2.0.1 // indirect gopkg.in/validator.v2 v2.0.1 // indirect
) )

View File

@ -859,6 +859,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@ -898,6 +900,7 @@ golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1m
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
@ -1040,6 +1043,8 @@ golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo=
golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@ -1087,6 +1092,8 @@ golang.org/x/oauth2 v0.20.0 h1:4mQdhULixXKP1rwYBW0vAijoXnkTG0BLCDRzfe1idMo=
golang.org/x/oauth2 v0.20.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= golang.org/x/oauth2 v0.20.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs= golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs=
golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/oauth2 v0.24.0 h1:KTBBxWqUa0ykRPLtV69rRto9TLXcqYkeswu48x/gvNE=
golang.org/x/oauth2 v0.24.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@ -1107,6 +1114,7 @@ golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@ -1192,6 +1200,7 @@ golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE= golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
@ -1209,6 +1218,7 @@ golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY= golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4= golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4=
golang.org/x/term v0.26.0/go.mod h1:Si5m1o57C5nBNQo5z1iq+XDijt21BDBDp2bK0QI8e3E=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@ -1230,6 +1240,7 @@ golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=

View File

@ -1,7 +1,9 @@
package main package main
import ( import (
"bytes"
"encoding/json" "encoding/json"
"strings"
"testing" "testing"
sw "go-petstore" sw "go-petstore"
@ -63,3 +65,19 @@ func TestRequiredFieldsAreValidated(t *testing.T) {
assert.ErrorContains(err, expected, "Pet should return error when missing required fields") assert.ErrorContains(err, expected, "Pet should return error when missing required fields")
} }
func TestRequiredAnyOfMarshalling(t *testing.T) {
// Given
bodyBuf := &bytes.Buffer{}
bananaLengthCm := float32(23.4)
req := &sw.FruitJuice{Fruit: sw.GmFruit{
Banana: &sw.Banana{LengthCm: &bananaLengthCm},
}}
// When
err := json.NewEncoder(bodyBuf).Encode(req)
// Then
assert.Nil(t, err)
assert.Equal(t, strings.TrimSpace(bodyBuf.String()), "{\"fruit\":{\"lengthCm\":23.4}}")
}