From 4874a0bce76df9cbb000cd088e843f2ccd98fe99 Mon Sep 17 00:00:00 2001 From: Stephen Zapp <62268791+sazapp-spectra@users.noreply.github.com> Date: Wed, 31 Jul 2024 03:16:24 -0600 Subject: [PATCH] [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` --- .../main/resources/go/model_oneof.mustache | 4 +- ...odels-for-testing-with-http-signature.yaml | 6 + .../go/go-petstore/.openapi-generator/FILES | 2 + .../client/petstore/go/go-petstore/README.md | 1 + .../petstore/go/go-petstore/api/openapi.yaml | 6 + .../go-petstore/docs/OneOfWithComplexType.md | 30 ++++ .../model_one_of_with_complex_type.go | 157 ++++++++++++++++++ 7 files changed, 204 insertions(+), 2 deletions(-) create mode 100644 samples/openapi3/client/petstore/go/go-petstore/docs/OneOfWithComplexType.md create mode 100644 samples/openapi3/client/petstore/go/go-petstore/model_one_of_with_complex_type.go diff --git a/modules/openapi-generator/src/main/resources/go/model_oneof.mustache b/modules/openapi-generator/src/main/resources/go/model_oneof.mustache index 3e9ee103887..e62c5e82d2b 100644 --- a/modules/openapi-generator/src/main/resources/go/model_oneof.mustache +++ b/modules/openapi-generator/src/main/resources/go/model_oneof.mustache @@ -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++ diff --git a/modules/openapi-generator/src/test/resources/3_0/go/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml b/modules/openapi-generator/src/test/resources/3_0/go/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml index 3f1e3e9c67a..e708225c472 100644 --- a/modules/openapi-generator/src/test/resources/3_0/go/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/go/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml @@ -2219,3 +2219,9 @@ components: properties: id: type: integer + OneOfWithComplexType: + oneOf: + - type: string + - type: array + items: + type: string diff --git a/samples/openapi3/client/petstore/go/go-petstore/.openapi-generator/FILES b/samples/openapi3/client/petstore/go/go-petstore/.openapi-generator/FILES index 1c45323b412..69d53e4d6b0 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/.openapi-generator/FILES +++ b/samples/openapi3/client/petstore/go/go-petstore/.openapi-generator/FILES @@ -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 diff --git a/samples/openapi3/client/petstore/go/go-petstore/README.md b/samples/openapi3/client/petstore/go/go-petstore/README.md index c8bd1c0f834..d8d4b1b5334 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/README.md +++ b/samples/openapi3/client/petstore/go/go-petstore/README.md @@ -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) diff --git a/samples/openapi3/client/petstore/go/go-petstore/api/openapi.yaml b/samples/openapi3/client/petstore/go/go-petstore/api/openapi.yaml index c63b467c807..7c600f3f726 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/api/openapi.yaml +++ b/samples/openapi3/client/petstore/go/go-petstore/api/openapi.yaml @@ -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: diff --git a/samples/openapi3/client/petstore/go/go-petstore/docs/OneOfWithComplexType.md b/samples/openapi3/client/petstore/go/go-petstore/docs/OneOfWithComplexType.md new file mode 100644 index 00000000000..5288368ec7f --- /dev/null +++ b/samples/openapi3/client/petstore/go/go-petstore/docs/OneOfWithComplexType.md @@ -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) + + diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_one_of_with_complex_type.go b/samples/openapi3/client/petstore/go/go-petstore/model_one_of_with_complex_type.go new file mode 100644 index 00000000000..d945fbd2e78 --- /dev/null +++ b/samples/openapi3/client/petstore/go/go-petstore/model_one_of_with_complex_type.go @@ -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) +} + +