[GO] fix generated variable names when no discriminator is used with 'oneOf' (#19183)

* fix oneOf var names when no discriminator is used

* add OneOfWithComplexType to Go petstore schema

- an array of strings variant should end up with the same valid name
whether `useOneOfDiscriminatorLookup` is `true` or `false`
This commit is contained in:
Stephen Zapp 2024-07-31 03:16:24 -06:00 committed by GitHub
parent 4bf8e4a6ff
commit 4874a0bce7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 204 additions and 2 deletions

View File

@ -58,8 +58,8 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
// try to unmarshal data into {{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}}
err = json.Unmarshal(data, &dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}})
if err == nil {
json{{{.}}}, _ := json.Marshal(dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}})
if string(json{{{.}}}) == "{}" { // empty struct
json{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}}, _ := json.Marshal(dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}})
if string(json{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}}) == "{}" { // empty struct
dst.{{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}} = nil
} else {
match++

View File

@ -2219,3 +2219,9 @@ components:
properties:
id:
type: integer
OneOfWithComplexType:
oneOf:
- type: string
- type: array
items:
type: string

View File

@ -64,6 +64,7 @@ docs/NumberOnly.md
docs/OneOfPrimitiveType.md
docs/OneOfPrimitiveTypeChild.md
docs/OneOfPrimitiveTypes.md
docs/OneOfWithComplexType.md
docs/Order.md
docs/OuterComposite.md
docs/OuterEnum.md
@ -138,6 +139,7 @@ model_number_only.go
model_one_of_primitive_type.go
model_one_of_primitive_type_child.go
model_one_of_primitive_types.go
model_one_of_with_complex_type.go
model_order.go
model_outer_composite.go
model_outer_enum.go

View File

@ -177,6 +177,7 @@ Class | Method | HTTP request | Description
- [OneOfPrimitiveType](docs/OneOfPrimitiveType.md)
- [OneOfPrimitiveTypeChild](docs/OneOfPrimitiveTypeChild.md)
- [OneOfPrimitiveTypes](docs/OneOfPrimitiveTypes.md)
- [OneOfWithComplexType](docs/OneOfWithComplexType.md)
- [Order](docs/Order.md)
- [OuterComposite](docs/OuterComposite.md)
- [OuterEnum](docs/OuterEnum.md)

View File

@ -2198,6 +2198,12 @@ components:
id:
type: integer
type: object
OneOfWithComplexType:
oneOf:
- type: string
- items:
type: string
type: array
_foo_get_default_response:
example:
string:

View File

@ -0,0 +1,30 @@
# OneOfWithComplexType
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
## Methods
### NewOneOfWithComplexType
`func NewOneOfWithComplexType() *OneOfWithComplexType`
NewOneOfWithComplexType instantiates a new OneOfWithComplexType object
This constructor will assign default values to properties that have it defined,
and makes sure properties required by API are set, but the set of arguments
will change when the set of required properties is changed
### NewOneOfWithComplexTypeWithDefaults
`func NewOneOfWithComplexTypeWithDefaults() *OneOfWithComplexType`
NewOneOfWithComplexTypeWithDefaults instantiates a new OneOfWithComplexType object
This constructor will only assign default values to properties that have it defined,
but it doesn't guarantee that properties required by API are set
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

View File

@ -0,0 +1,157 @@
/*
OpenAPI Petstore
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
API version: 1.0.0
*/
// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.
package petstore
import (
"encoding/json"
"gopkg.in/validator.v2"
"fmt"
)
// OneOfWithComplexType - struct for OneOfWithComplexType
type OneOfWithComplexType struct {
ArrayOfString *[]string
String *string
}
// []stringAsOneOfWithComplexType is a convenience function that returns []string wrapped in OneOfWithComplexType
func ArrayOfStringAsOneOfWithComplexType(v *[]string) OneOfWithComplexType {
return OneOfWithComplexType{
ArrayOfString: v,
}
}
// stringAsOneOfWithComplexType is a convenience function that returns string wrapped in OneOfWithComplexType
func StringAsOneOfWithComplexType(v *string) OneOfWithComplexType {
return OneOfWithComplexType{
String: v,
}
}
// Unmarshal JSON data into one of the pointers in the struct
func (dst *OneOfWithComplexType) UnmarshalJSON(data []byte) error {
var err error
match := 0
// try to unmarshal data into ArrayOfString
err = newStrictDecoder(data).Decode(&dst.ArrayOfString)
if err == nil {
jsonArrayOfString, _ := json.Marshal(dst.ArrayOfString)
if string(jsonArrayOfString) == "{}" { // empty struct
dst.ArrayOfString = nil
} else {
if err = validator.Validate(dst.ArrayOfString); err != nil {
dst.ArrayOfString = nil
} else {
match++
}
}
} else {
dst.ArrayOfString = nil
}
// try to unmarshal data into String
err = newStrictDecoder(data).Decode(&dst.String)
if err == nil {
jsonString, _ := json.Marshal(dst.String)
if string(jsonString) == "{}" { // empty struct
dst.String = nil
} else {
if err = validator.Validate(dst.String); err != nil {
dst.String = nil
} else {
match++
}
}
} else {
dst.String = nil
}
if match > 1 { // more than 1 match
// reset to nil
dst.ArrayOfString = nil
dst.String = nil
return fmt.Errorf("data matches more than one schema in oneOf(OneOfWithComplexType)")
} else if match == 1 {
return nil // exactly one match
} else { // no match
return fmt.Errorf("data failed to match schemas in oneOf(OneOfWithComplexType)")
}
}
// Marshal data from the first non-nil pointers in the struct to JSON
func (src OneOfWithComplexType) MarshalJSON() ([]byte, error) {
if src.ArrayOfString != nil {
return json.Marshal(&src.ArrayOfString)
}
if src.String != nil {
return json.Marshal(&src.String)
}
return nil, nil // no data in oneOf schemas
}
// Get the actual instance
func (obj *OneOfWithComplexType) GetActualInstance() (interface{}) {
if obj == nil {
return nil
}
if obj.ArrayOfString != nil {
return obj.ArrayOfString
}
if obj.String != nil {
return obj.String
}
// all schemas are nil
return nil
}
type NullableOneOfWithComplexType struct {
value *OneOfWithComplexType
isSet bool
}
func (v NullableOneOfWithComplexType) Get() *OneOfWithComplexType {
return v.value
}
func (v *NullableOneOfWithComplexType) Set(val *OneOfWithComplexType) {
v.value = val
v.isSet = true
}
func (v NullableOneOfWithComplexType) IsSet() bool {
return v.isSet
}
func (v *NullableOneOfWithComplexType) Unset() {
v.value = nil
v.isSet = false
}
func NewNullableOneOfWithComplexType(val *OneOfWithComplexType) *NullableOneOfWithComplexType {
return &NullableOneOfWithComplexType{value: val, isSet: true}
}
func (v NullableOneOfWithComplexType) MarshalJSON() ([]byte, error) {
return json.Marshal(v.value)
}
func (v *NullableOneOfWithComplexType) UnmarshalJSON(src []byte) error {
v.isSet = true
return json.Unmarshal(src, &v.value)
}