Skip to content
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

Merged

Conversation

perhallgren
Copy link
Contributor

@perhallgren perhallgren commented Oct 24, 2024

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

$ grep -ie "func (\w\+ \*.*) MarshalJSON" ./modules/openapi-generator/src/main/resources/go/*
./modules/openapi-generator/src/main/resources/go/model_anyof.mustache:func (src *{{classname}}) MarshalJSON() ([]byte, error) {

The above command gives an empty output with the proposed change.

Also verified with the test in this gist. In the created tmp folder:

$ java -jar ~/code/OpenAPITools/openapi-generator/modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \
        --input-spec go-json-marshal.yaml \
        --generator-name go \
        --additional-properties "withGoMod=false" \
        --package-name gen \
        --output gen
[main] INFO  o.o.codegen.DefaultGenerator - Generating with dryRun=false
[...]
$ go test .
ok  	github.com/OpenAPITools/bugs	0.004s

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (For Windows users, please run the script in Git BASH)
    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.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@perhallgren perhallgren changed the title fix(go): use non-pointer receiver for JSON marshal [BUG][GO] use non-pointer receiver for JSON marshal Oct 24, 2024
@perhallgren perhallgren changed the title [BUG][GO] use non-pointer receiver for JSON marshal [BUG][GO] use value receiver for JSON marshal Oct 24, 2024
@perhallgren perhallgren force-pushed the fix/json-marshal-required-anyof branch from 34cb2f1 to bfd3b6f Compare October 24, 2024 07:51
@perhallgren perhallgren marked this pull request as ready for review October 24, 2024 08:08
@perhallgren
Copy link
Contributor Author

Pings for technical committee: @antihax @grokify @kemokemo @jirikuncar @ph4r5h4d @lwj5

@daisukixci
Copy link

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) {
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)

@perhallgren perhallgren force-pushed the fix/json-marshal-required-anyof branch 5 times, most recently from d862b8f to f6d3a20 Compare November 25, 2024 15:59
Adds a small schema (FruitJuice) which contains a required gmFruit,
which inherits using AnyOf. This fails to correctly marshal as JSON.
@perhallgren perhallgren force-pushed the fix/json-marshal-required-anyof branch from f6d3a20 to c779969 Compare November 25, 2024 16:12
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.
@perhallgren
Copy link
Contributor Author

Added a test, looks OK!

Failing here with:

    model_test.go:82: 
                Error Trace:    /home/circleci/OpenAPITools/openapi-generator/samples/openapi3/client/petstore/go/model_test.go:82
                Error:          Not equal: 
                                expected: "{\"fruit\":{\"Apple\":null,\"Banana\":{\"lengthCm\":23.4}}}"
                                actual  : "{\"fruit\":{\"lengthCm\":23.4}}"

And green with the fix.

@wing328 wing328 merged commit 8a94fc6 into OpenAPITools:master Nov 27, 2024
16 checks passed
@wing328 wing328 added this to the 7.11.0 milestone Nov 27, 2024
@perhallgren perhallgren deleted the fix/json-marshal-required-anyof branch November 28, 2024 07:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants