-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
[BUG][GO] use value receiver for JSON marshal #19962
[BUG][GO] use value receiver for JSON marshal #19962
Conversation
34cb2f1
to
bfd3b6f
Compare
Fixed worked on my spec, thank you for it! |
@@ -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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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)
d862b8f
to
f6d3a20
Compare
Adds a small schema (FruitJuice) which contains a required gmFruit, which inherits using AnyOf. This fails to correctly marshal as JSON.
f6d3a20
to
c779969
Compare
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.
Added a test, looks OK! Failing here with:
And green with the fix. |
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.Testing
On master, we can check that this is the only occurance of pointer receiver for json marshalling with
The above command gives an empty output with the proposed change.
Also verified with the test in this gist. In the created tmp folder:
PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*
.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master
(upcoming7.x.0
minor release - breaking changes with fallbacks),8.0.x
(breaking changes without fallbacks)