Skip to content

Commit

Permalink
[BUG][GO] use value receiver for JSON marshal (#19962)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
perhallgren and perhallgren authored Nov 27, 2024
1 parent 7052619 commit 8a94fc6
Show file tree
Hide file tree
Showing 20 changed files with 348 additions and 214 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ 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) {
func (src {{classname}}) MarshalJSON() ([]byte, error) {
{{#anyOf}}
if src.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} != nil {
return json.Marshal(&src.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1394,23 +1394,13 @@ components:
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:
Expand All @@ -1425,7 +1415,16 @@ components:
mapping:
set: '#/components/schemas/FilterTypeRegex'
range: '#/components/schemas/FilterTypeRange'
propertyName: type
propertyName: type
properties:
type:
enum:
- set
- range
type: string
date:
type: string
format: date-time
MapWithDateTime:
type: object
additionalProperties:
Expand Down Expand Up @@ -2119,6 +2118,13 @@ components:
type: string
required:
- className
fruitJuice:
type: object
required:
- fruit
properties:
fruit:
$ref: '#/components/schemas/gmFruit'
gmFruit:
properties:
color:
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ docs/Foo.md
docs/FooGetDefaultResponse.md
docs/FormatTest.md
docs/Fruit.md
docs/FruitJuice.md
docs/FruitReq.md
docs/GmFruit.md
docs/HasOnlyReadOnly.md
Expand Down Expand Up @@ -126,6 +127,7 @@ model_filter_type_regex.go
model_foo.go
model_format_test_.go
model_fruit.go
model_fruit_juice.go
model_fruit_req.go
model_gm_fruit.go
model_has_only_read_only.go
Expand Down
1 change: 1 addition & 0 deletions samples/openapi3/client/petstore/go/go-petstore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ Class | Method | HTTP request | Description
- [FooGetDefaultResponse](docs/FooGetDefaultResponse.md)
- [FormatTest](docs/FormatTest.md)
- [Fruit](docs/Fruit.md)
- [FruitJuice](docs/FruitJuice.md)
- [FruitReq](docs/FruitReq.md)
- [GmFruit](docs/GmFruit.md)
- [HasOnlyReadOnly](docs/HasOnlyReadOnly.md)
Expand Down
26 changes: 16 additions & 10 deletions samples/openapi3/client/petstore/go/go-petstore/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1296,23 +1296,13 @@ components:
schemas:
FilterTypeRegex:
properties:
type:
enum:
- set
- range
type: string
regex:
type: string
required:
- type
type: object
FilterTypeRange:
properties:
type:
enum:
- set
- range
type: string
data:
items:
type: string
Expand All @@ -1329,6 +1319,15 @@ components:
set: '#/components/schemas/FilterTypeRegex'
range: '#/components/schemas/FilterTypeRange'
propertyName: type
properties:
type:
enum:
- set
- range
type: string
date:
format: date-time
type: string
MapWithDateTime:
additionalProperties:
items:
Expand Down Expand Up @@ -2101,6 +2100,13 @@ components:
required:
- className
type: object
fruitJuice:
properties:
fruit:
$ref: '#/components/schemas/gmFruit'
required:
- fruit
type: object
gmFruit:
anyOf:
- $ref: '#/components/schemas/apple'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,7 @@ import (
)

func main() {
filter := *openapiclient.NewFilterAny("Type_example") // FilterAny | (optional)
filter := *openapiclient.NewFilterAny() // FilterAny | (optional)

configuration := openapiclient.NewConfiguration()
apiClient := openapiclient.NewAPIClient(configuration)
Expand Down
35 changes: 33 additions & 2 deletions samples/openapi3/client/petstore/go/go-petstore/docs/FilterAny.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Type** | **string** | |
**Type** | Pointer to **string** | | [optional]
**Date** | Pointer to **time.Time** | | [optional]
**Regex** | Pointer to **string** | | [optional]
**Data** | Pointer to **[]string** | | [optional]

## Methods

### NewFilterAny

`func NewFilterAny(type_ string, ) *FilterAny`
`func NewFilterAny() *FilterAny`

NewFilterAny instantiates a new FilterAny object
This constructor will assign default values to properties that have it defined,
Expand Down Expand Up @@ -46,6 +47,36 @@ and a boolean to check if the value has been set.

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Type** | **string** | |
**Data** | Pointer to **[]string** | | [optional]

## Methods

### NewFilterTypeRange

`func NewFilterTypeRange(type_ string, ) *FilterTypeRange`
`func NewFilterTypeRange() *FilterTypeRange`

NewFilterTypeRange instantiates a new FilterTypeRange object
This constructor will assign default values to properties that have it defined,
Expand All @@ -26,26 +25,6 @@ NewFilterTypeRangeWithDefaults instantiates a new FilterTypeRange 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

### 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

`func (o *FilterTypeRange) GetData() []string`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Type** | **string** | |
**Regex** | Pointer to **string** | | [optional]

## Methods

### NewFilterTypeRegex

`func NewFilterTypeRegex(type_ string, ) *FilterTypeRegex`
`func NewFilterTypeRegex() *FilterTypeRegex`

NewFilterTypeRegex instantiates a new FilterTypeRegex object
This constructor will assign default values to properties that have it defined,
Expand All @@ -26,26 +25,6 @@ NewFilterTypeRegexWithDefaults instantiates a new FilterTypeRegex 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

### 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

`func (o *FilterTypeRegex) GetRegex() string`
Expand Down
51 changes: 51 additions & 0 deletions samples/openapi3/client/petstore/go/go-petstore/docs/FruitJuice.md
Original file line number Diff line number Diff line change
@@ -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)


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

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

Loading

0 comments on commit 8a94fc6

Please sign in to comment.