Tanmay Mohapatra 4c163fe4b0
[Go] Fix deepObject serialization that are anyOf (#19090)
* [Go] Fix deepObject serialization that are anyOf

Updates the go client generator to have the generated struct for anyOf types conform to `MappedNullable` interface.
Fixes query params serialization for deepObjects that are of `anyOf` type.

Implements the suggestion in https://github.com/OpenAPITools/openapi-generator/issues/19085

* check parameter style for deepObject serialization

* generate samples for go-echo-external-refs-test

* move test back to http port

* restrict to anyof models with discriminator

* update test

* add some tests

added some tests to `modules/openapi-generator/src/test/resources/3_0/go/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml` and regenerated the samples
2024-08-04 17:43:09 +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)
}