Per Hallgren 8a94fc667e
[BUG][GO] use value receiver for JSON marshal (#19962)
* 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 <perhallgren@users.noreply.github.com>
2024-11-27 18:09:11 +08:00

108 lines
2.3 KiB
Go

/*
Test
No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
API version: 1.0.0
*/
// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.
package openapi
import (
"encoding/json"
"fmt"
)
// Object2 struct for Object2
type Object2 struct {
NestedObject1 *NestedObject1
NestedObject2 *NestedObject2
}
// Unmarshal JSON data into any of the pointers in the struct
func (dst *Object2) UnmarshalJSON(data []byte) error {
var err error
// try to unmarshal JSON data into NestedObject1
err = json.Unmarshal(data, &dst.NestedObject1);
if err == nil {
jsonNestedObject1, _ := json.Marshal(dst.NestedObject1)
if string(jsonNestedObject1) == "{}" { // empty struct
dst.NestedObject1 = nil
} else {
return nil // data stored in dst.NestedObject1, return on the first match
}
} else {
dst.NestedObject1 = nil
}
// try to unmarshal JSON data into NestedObject2
err = json.Unmarshal(data, &dst.NestedObject2);
if err == nil {
jsonNestedObject2, _ := json.Marshal(dst.NestedObject2)
if string(jsonNestedObject2) == "{}" { // empty struct
dst.NestedObject2 = nil
} else {
return nil // data stored in dst.NestedObject2, return on the first match
}
} else {
dst.NestedObject2 = nil
}
return fmt.Errorf("data failed to match schemas in anyOf(Object2)")
}
// Marshal data from the first non-nil pointers in the struct to JSON
func (src Object2) MarshalJSON() ([]byte, error) {
if src.NestedObject1 != nil {
return json.Marshal(&src.NestedObject1)
}
if src.NestedObject2 != nil {
return json.Marshal(&src.NestedObject2)
}
return nil, nil // no data in anyOf schemas
}
type NullableObject2 struct {
value *Object2
isSet bool
}
func (v NullableObject2) Get() *Object2 {
return v.value
}
func (v *NullableObject2) Set(val *Object2) {
v.value = val
v.isSet = true
}
func (v NullableObject2) IsSet() bool {
return v.isSet
}
func (v *NullableObject2) Unset() {
v.value = nil
v.isSet = false
}
func NewNullableObject2(val *Object2) *NullableObject2 {
return &NullableObject2{value: val, isSet: true}
}
func (v NullableObject2) MarshalJSON() ([]byte, error) {
return json.Marshal(v.value)
}
func (v *NullableObject2) UnmarshalJSON(src []byte) error {
v.isSet = true
return json.Unmarshal(src, &v.value)
}