From 1c0d6d84d8a2c9af12a91f3a09a39c19a0f6872e Mon Sep 17 00:00:00 2001 From: William Cheng Date: Wed, 10 Aug 2022 16:26:10 +0800 Subject: [PATCH] [Go] Fix missing import for array/map of file (#13143) * fix missing import for array/map of file in go client * add new files --- .../codegen/languages/AbstractGoCodegen.java | 9 +- ...odels-for-testing-with-http-signature.yaml | 10 ++ .../go/go-petstore/.openapi-generator/FILES | 2 + .../client/petstore/go/go-petstore/README.md | 1 + .../petstore/go/go-petstore/api/openapi.yaml | 10 ++ .../go/go-petstore/docs/MapOfFileTest.md | 56 +++++++ .../go/go-petstore/model_map_of_file_test_.go | 142 ++++++++++++++++++ samples/openapi3/client/petstore/go/go.mod | 2 +- samples/openapi3/client/petstore/go/go.sum | 2 + 9 files changed, 229 insertions(+), 5 deletions(-) create mode 100644 samples/openapi3/client/petstore/go/go-petstore/docs/MapOfFileTest.md create mode 100644 samples/openapi3/client/petstore/go/go-petstore/model_map_of_file_test_.go diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java index edbfb173c90..a5d83023039 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java @@ -625,13 +625,14 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege boolean addedOSImport = false; for (ModelMap m : objs.getModels()) { CodegenModel model = m.getModel(); - for (CodegenProperty param : model.vars) { - if (!addedTimeImport - && ("time.Time".equals(param.dataType) || ("[]time.Time".equals(param.dataType)))) { + for (CodegenProperty cp : model.vars) { + if (!addedTimeImport && ("time.Time".equals(cp.dataType) || + (cp.items != null && "time.Time".equals(cp.items.dataType)))) { imports.add(createMapping("import", "time")); addedTimeImport = true; } - if (!addedOSImport && "*os.File".equals(param.baseType)) { + if (!addedOSImport && ("*os.File".equals(cp.dataType) || + (cp.items != null && "*os.File".equals(cp.items.dataType)))) { imports.add(createMapping("import", "os")); addedOSImport = true; } 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 f0e46727b25..ca69eadb7fa 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 @@ -2007,3 +2007,13 @@ components: 'dup-prop': description: A discriminator value type: string + MapOfFileTest: + description: test map of file in a property + type: object + properties: + 'prop_test': + description: a property to test map of file + type: object + additionalProperties: + type: string + format: binary 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 1caa92c7023..257af3a4dcd 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/.openapi-generator/FILES +++ b/samples/openapi3/client/petstore/go/go-petstore/.openapi-generator/FILES @@ -51,6 +51,7 @@ docs/HasOnlyReadOnly.md docs/HealthCheckResult.md docs/List.md docs/Mammal.md +docs/MapOfFileTest.md docs/MapTest.md docs/MixedPropertiesAndAdditionalPropertiesClass.md docs/Model200Response.md @@ -120,6 +121,7 @@ model_has_only_read_only.go model_health_check_result.go model_list.go model_mammal.go +model_map_of_file_test_.go model_map_test_.go model_mixed_properties_and_additional_properties_class.go model_name.go diff --git a/samples/openapi3/client/petstore/go/go-petstore/README.md b/samples/openapi3/client/petstore/go/go-petstore/README.md index 219c8a8340d..97fe7b65428 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/README.md +++ b/samples/openapi3/client/petstore/go/go-petstore/README.md @@ -157,6 +157,7 @@ Class | Method | HTTP request | Description - [HealthCheckResult](docs/HealthCheckResult.md) - [List](docs/List.md) - [Mammal](docs/Mammal.md) + - [MapOfFileTest](docs/MapOfFileTest.md) - [MapTest](docs/MapTest.md) - [MixedPropertiesAndAdditionalPropertiesClass](docs/MixedPropertiesAndAdditionalPropertiesClass.md) - [Model200Response](docs/Model200Response.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 4a71828549c..a55cbf5727e 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/api/openapi.yaml +++ b/samples/openapi3/client/petstore/go/go-petstore/api/openapi.yaml @@ -1968,6 +1968,16 @@ components: required: - dup-prop type: object + MapOfFileTest: + description: test map of file in a property + properties: + prop_test: + additionalProperties: + format: binary + type: string + description: a property to test map of file + type: object + type: object _foo_get_default_response: example: string: diff --git a/samples/openapi3/client/petstore/go/go-petstore/docs/MapOfFileTest.md b/samples/openapi3/client/petstore/go/go-petstore/docs/MapOfFileTest.md new file mode 100644 index 00000000000..245df44f8db --- /dev/null +++ b/samples/openapi3/client/petstore/go/go-petstore/docs/MapOfFileTest.md @@ -0,0 +1,56 @@ +# MapOfFileTest + +## Properties + +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**PropTest** | Pointer to **map[string]*os.File** | a property to test map of file | [optional] + +## Methods + +### NewMapOfFileTest + +`func NewMapOfFileTest() *MapOfFileTest` + +NewMapOfFileTest instantiates a new MapOfFileTest 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 + +### NewMapOfFileTestWithDefaults + +`func NewMapOfFileTestWithDefaults() *MapOfFileTest` + +NewMapOfFileTestWithDefaults instantiates a new MapOfFileTest 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 + +### GetPropTest + +`func (o *MapOfFileTest) GetPropTest() map[string]*os.File` + +GetPropTest returns the PropTest field if non-nil, zero value otherwise. + +### GetPropTestOk + +`func (o *MapOfFileTest) GetPropTestOk() (*map[string]*os.File, bool)` + +GetPropTestOk returns a tuple with the PropTest field if it's non-nil, zero value otherwise +and a boolean to check if the value has been set. + +### SetPropTest + +`func (o *MapOfFileTest) SetPropTest(v map[string]*os.File)` + +SetPropTest sets PropTest field to given value. + +### HasPropTest + +`func (o *MapOfFileTest) HasPropTest() bool` + +HasPropTest returns a boolean if a field has been 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_map_of_file_test_.go b/samples/openapi3/client/petstore/go/go-petstore/model_map_of_file_test_.go new file mode 100644 index 00000000000..89690bd72c4 --- /dev/null +++ b/samples/openapi3/client/petstore/go/go-petstore/model_map_of_file_test_.go @@ -0,0 +1,142 @@ +/* +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" + "os" +) + +// MapOfFileTest test map of file in a property +type MapOfFileTest struct { + // a property to test map of file + PropTest *map[string]*os.File `json:"prop_test,omitempty"` + AdditionalProperties map[string]interface{} +} + +type _MapOfFileTest MapOfFileTest + +// NewMapOfFileTest instantiates a new MapOfFileTest 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 +func NewMapOfFileTest() *MapOfFileTest { + this := MapOfFileTest{} + return &this +} + +// NewMapOfFileTestWithDefaults instantiates a new MapOfFileTest 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 +func NewMapOfFileTestWithDefaults() *MapOfFileTest { + this := MapOfFileTest{} + return &this +} + +// GetPropTest returns the PropTest field value if set, zero value otherwise. +func (o *MapOfFileTest) GetPropTest() map[string]*os.File { + if o == nil || o.PropTest == nil { + var ret map[string]*os.File + return ret + } + return *o.PropTest +} + +// GetPropTestOk returns a tuple with the PropTest field value if set, nil otherwise +// and a boolean to check if the value has been set. +func (o *MapOfFileTest) GetPropTestOk() (*map[string]*os.File, bool) { + if o == nil || o.PropTest == nil { + return nil, false + } + return o.PropTest, true +} + +// HasPropTest returns a boolean if a field has been set. +func (o *MapOfFileTest) HasPropTest() bool { + if o != nil && o.PropTest != nil { + return true + } + + return false +} + +// SetPropTest gets a reference to the given map[string]*os.File and assigns it to the PropTest field. +func (o *MapOfFileTest) SetPropTest(v map[string]*os.File) { + o.PropTest = &v +} + +func (o MapOfFileTest) MarshalJSON() ([]byte, error) { + toSerialize := map[string]interface{}{} + if o.PropTest != nil { + toSerialize["prop_test"] = o.PropTest + } + + for key, value := range o.AdditionalProperties { + toSerialize[key] = value + } + + return json.Marshal(toSerialize) +} + +func (o *MapOfFileTest) UnmarshalJSON(bytes []byte) (err error) { + varMapOfFileTest := _MapOfFileTest{} + + if err = json.Unmarshal(bytes, &varMapOfFileTest); err == nil { + *o = MapOfFileTest(varMapOfFileTest) + } + + additionalProperties := make(map[string]interface{}) + + if err = json.Unmarshal(bytes, &additionalProperties); err == nil { + delete(additionalProperties, "prop_test") + o.AdditionalProperties = additionalProperties + } + + return err +} + +type NullableMapOfFileTest struct { + value *MapOfFileTest + isSet bool +} + +func (v NullableMapOfFileTest) Get() *MapOfFileTest { + return v.value +} + +func (v *NullableMapOfFileTest) Set(val *MapOfFileTest) { + v.value = val + v.isSet = true +} + +func (v NullableMapOfFileTest) IsSet() bool { + return v.isSet +} + +func (v *NullableMapOfFileTest) Unset() { + v.value = nil + v.isSet = false +} + +func NewNullableMapOfFileTest(val *MapOfFileTest) *NullableMapOfFileTest { + return &NullableMapOfFileTest{value: val, isSet: true} +} + +func (v NullableMapOfFileTest) MarshalJSON() ([]byte, error) { + return json.Marshal(v.value) +} + +func (v *NullableMapOfFileTest) UnmarshalJSON(src []byte) error { + v.isSet = true + return json.Unmarshal(src, &v.value) +} + + diff --git a/samples/openapi3/client/petstore/go/go.mod b/samples/openapi3/client/petstore/go/go.mod index 32f361842fa..65bc808250c 100644 --- a/samples/openapi3/client/petstore/go/go.mod +++ b/samples/openapi3/client/petstore/go/go.mod @@ -7,6 +7,6 @@ replace go-petstore => ./go-petstore require ( github.com/stretchr/testify v1.8.0 go-petstore v0.0.0-00010101000000-000000000000 - golang.org/x/net v0.0.0-20220805013720-a33c5aa5df48 // indirect + golang.org/x/net v0.0.0-20220809184613-07c6da5e1ced // indirect golang.org/x/oauth2 v0.0.0-20220808172628-8227340efae7 ) diff --git a/samples/openapi3/client/petstore/go/go.sum b/samples/openapi3/client/petstore/go/go.sum index 1152769b258..b6dca751f39 100644 --- a/samples/openapi3/client/petstore/go/go.sum +++ b/samples/openapi3/client/petstore/go/go.sum @@ -301,6 +301,8 @@ golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e h1:TsQ7F31D3bUCLeqPT0u+yjp1g golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.0.0-20220805013720-a33c5aa5df48 h1:N9Vc/rorQUDes6B9CNdIxAn5jODGj2wzfrei2x4wNj4= golang.org/x/net v0.0.0-20220805013720-a33c5aa5df48/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= +golang.org/x/net v0.0.0-20220809184613-07c6da5e1ced h1:3dYNDff0VT5xj+mbj2XucFst9WKk6PdGOrb9n+SbIvw= +golang.org/x/net v0.0.0-20220809184613-07c6da5e1ced/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=