[BUG][GO] use value receiver for JSON marshal#19962
Merged
wing328 merged 2 commits intoOpenAPITools:masterfrom Nov 27, 2024
Merged
[BUG][GO] use value receiver for JSON marshal#19962wing328 merged 2 commits intoOpenAPITools:masterfrom
wing328 merged 2 commits intoOpenAPITools:masterfrom
Conversation
6 tasks
34cb2f1 to
bfd3b6f
Compare
Contributor
Author
|
Fixed worked on my spec, thank you for it! |
daisukixci
approved these changes
Oct 30, 2024
wing328
reviewed
Oct 30, 2024
|
|
||
| // 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) { |
Member
There was a problem hiding this comment.
thanks for the PR
do we need to apply the same fix model_oneof.mustache (oneOf template) as well?
Contributor
Author
There was a problem hiding this comment.
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.
Member
There was a problem hiding this comment.
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.
Contributor
Author
|
Added a test, looks OK! Failing here with: And green with the fix. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
MarshalJSONon 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.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)