Skip to content

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

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

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
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the PR

do we need to apply the same fix model_oneof.mustache (oneOf template) as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it already uses a value receiver. I searched for other occurances using this:

$ grep -ie "func (\w\+ \*.*) MarshalJSON" ./modules/openapi-generator/src/main/resources/go/*

Which only gives a hit in model_anyof.mustache.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the confirmation.

can you please add a test or 2 in samples/openapi3/client/petstore/go/model_test.go?

you may want to update modules/openapi-generator/src/test/resources/3_0/go/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml with new test schemas (e.g. anyOf)

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