From 5f2979c4340dd2a8c0d1553bee2ed5d7c3f5c7ac Mon Sep 17 00:00:00 2001 From: Slavek Kabrda Date: Wed, 20 May 2020 11:29:03 +0200 Subject: [PATCH] [go-experimental] Ensure that all oneOf/anyOf models have their Nullable defined (#6363) --- .../go-experimental/model_anyof.mustache | 2 ++ .../go-experimental/model_oneof.mustache | 2 ++ .../go-experimental/model_simple.mustache | 36 +------------------ .../go-experimental/nullable_model.mustache | 35 ++++++++++++++++++ .../go-petstore/model_fruit.go | 36 +++++++++++++++++++ .../go-petstore/model_fruit_req.go | 36 +++++++++++++++++++ .../go-petstore/model_gm_fruit.go | 36 +++++++++++++++++++ .../go-petstore/model_mammal.go | 36 +++++++++++++++++++ 8 files changed, 184 insertions(+), 35 deletions(-) create mode 100644 modules/openapi-generator/src/main/resources/go-experimental/nullable_model.mustache diff --git a/modules/openapi-generator/src/main/resources/go-experimental/model_anyof.mustache b/modules/openapi-generator/src/main/resources/go-experimental/model_anyof.mustache index 204970de29d..86cda3bc747 100644 --- a/modules/openapi-generator/src/main/resources/go-experimental/model_anyof.mustache +++ b/modules/openapi-generator/src/main/resources/go-experimental/model_anyof.mustache @@ -43,3 +43,5 @@ func (src *{{classname}}) MarshalJSON() ([]byte, error) { {{/anyOf}} return nil, nil // no data in anyOf schemas } + +{{>nullable_model}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/go-experimental/model_oneof.mustache b/modules/openapi-generator/src/main/resources/go-experimental/model_oneof.mustache index 53ee65aac2d..c0c498db14e 100644 --- a/modules/openapi-generator/src/main/resources/go-experimental/model_oneof.mustache +++ b/modules/openapi-generator/src/main/resources/go-experimental/model_oneof.mustache @@ -75,3 +75,5 @@ func (obj *{{classname}}) GetActualInstance() (interface{}) { // all schemas are nil return nil } + +{{>nullable_model}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/go-experimental/model_simple.mustache b/modules/openapi-generator/src/main/resources/go-experimental/model_simple.mustache index 0a06b4a2743..1078787c86d 100644 --- a/modules/openapi-generator/src/main/resources/go-experimental/model_simple.mustache +++ b/modules/openapi-generator/src/main/resources/go-experimental/model_simple.mustache @@ -249,38 +249,4 @@ func (o {{classname}}) MarshalJSON() ([]byte, error) { return json.Marshal(toSerialize) } -type Nullable{{{classname}}} struct { - value *{{{classname}}} - isSet bool -} - -func (v Nullable{{classname}}) Get() *{{classname}} { - return v.value -} - -func (v *Nullable{{classname}}) Set(val *{{classname}}) { - v.value = val - v.isSet = true -} - -func (v Nullable{{classname}}) IsSet() bool { - return v.isSet -} - -func (v *Nullable{{classname}}) Unset() { - v.value = nil - v.isSet = false -} - -func NewNullable{{classname}}(val *{{classname}}) *Nullable{{classname}} { - return &Nullable{{classname}}{value: val, isSet: true} -} - -func (v Nullable{{{classname}}}) MarshalJSON() ([]byte, error) { - return json.Marshal(v.value) -} - -func (v *Nullable{{{classname}}}) UnmarshalJSON(src []byte) error { - v.isSet = true - return json.Unmarshal(src, &v.value) -} +{{>nullable_model}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/go-experimental/nullable_model.mustache b/modules/openapi-generator/src/main/resources/go-experimental/nullable_model.mustache new file mode 100644 index 00000000000..20d35769130 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/go-experimental/nullable_model.mustache @@ -0,0 +1,35 @@ +type Nullable{{{classname}}} struct { + value *{{{classname}}} + isSet bool +} + +func (v Nullable{{classname}}) Get() *{{classname}} { + return v.value +} + +func (v *Nullable{{classname}}) Set(val *{{classname}}) { + v.value = val + v.isSet = true +} + +func (v Nullable{{classname}}) IsSet() bool { + return v.isSet +} + +func (v *Nullable{{classname}}) Unset() { + v.value = nil + v.isSet = false +} + +func NewNullable{{classname}}(val *{{classname}}) *Nullable{{classname}} { + return &Nullable{{classname}}{value: val, isSet: true} +} + +func (v Nullable{{{classname}}}) MarshalJSON() ([]byte, error) { + return json.Marshal(v.value) +} + +func (v *Nullable{{{classname}}}) UnmarshalJSON(src []byte) error { + v.isSet = true + return json.Unmarshal(src, &v.value) +} diff --git a/samples/openapi3/client/petstore/go-experimental/go-petstore/model_fruit.go b/samples/openapi3/client/petstore/go-experimental/go-petstore/model_fruit.go index 5863b9c588b..b50054da7ff 100644 --- a/samples/openapi3/client/petstore/go-experimental/go-petstore/model_fruit.go +++ b/samples/openapi3/client/petstore/go-experimental/go-petstore/model_fruit.go @@ -101,3 +101,39 @@ func (obj *Fruit) GetActualInstance() (interface{}) { return nil } +type NullableFruit struct { + value *Fruit + isSet bool +} + +func (v NullableFruit) Get() *Fruit { + return v.value +} + +func (v *NullableFruit) Set(val *Fruit) { + v.value = val + v.isSet = true +} + +func (v NullableFruit) IsSet() bool { + return v.isSet +} + +func (v *NullableFruit) Unset() { + v.value = nil + v.isSet = false +} + +func NewNullableFruit(val *Fruit) *NullableFruit { + return &NullableFruit{value: val, isSet: true} +} + +func (v NullableFruit) MarshalJSON() ([]byte, error) { + return json.Marshal(v.value) +} + +func (v *NullableFruit) UnmarshalJSON(src []byte) error { + v.isSet = true + return json.Unmarshal(src, &v.value) +} + diff --git a/samples/openapi3/client/petstore/go-experimental/go-petstore/model_fruit_req.go b/samples/openapi3/client/petstore/go-experimental/go-petstore/model_fruit_req.go index c1ee08e0881..41e7c2a5a18 100644 --- a/samples/openapi3/client/petstore/go-experimental/go-petstore/model_fruit_req.go +++ b/samples/openapi3/client/petstore/go-experimental/go-petstore/model_fruit_req.go @@ -101,3 +101,39 @@ func (obj *FruitReq) GetActualInstance() (interface{}) { return nil } +type NullableFruitReq struct { + value *FruitReq + isSet bool +} + +func (v NullableFruitReq) Get() *FruitReq { + return v.value +} + +func (v *NullableFruitReq) Set(val *FruitReq) { + v.value = val + v.isSet = true +} + +func (v NullableFruitReq) IsSet() bool { + return v.isSet +} + +func (v *NullableFruitReq) Unset() { + v.value = nil + v.isSet = false +} + +func NewNullableFruitReq(val *FruitReq) *NullableFruitReq { + return &NullableFruitReq{value: val, isSet: true} +} + +func (v NullableFruitReq) MarshalJSON() ([]byte, error) { + return json.Marshal(v.value) +} + +func (v *NullableFruitReq) UnmarshalJSON(src []byte) error { + v.isSet = true + return json.Unmarshal(src, &v.value) +} + diff --git a/samples/openapi3/client/petstore/go-experimental/go-petstore/model_gm_fruit.go b/samples/openapi3/client/petstore/go-experimental/go-petstore/model_gm_fruit.go index 997688ee143..03baf438d13 100644 --- a/samples/openapi3/client/petstore/go-experimental/go-petstore/model_gm_fruit.go +++ b/samples/openapi3/client/petstore/go-experimental/go-petstore/model_gm_fruit.go @@ -65,3 +65,39 @@ func (src *GmFruit) MarshalJSON() ([]byte, error) { return nil, nil // no data in anyOf schemas } +type NullableGmFruit struct { + value *GmFruit + isSet bool +} + +func (v NullableGmFruit) Get() *GmFruit { + return v.value +} + +func (v *NullableGmFruit) Set(val *GmFruit) { + v.value = val + v.isSet = true +} + +func (v NullableGmFruit) IsSet() bool { + return v.isSet +} + +func (v *NullableGmFruit) Unset() { + v.value = nil + v.isSet = false +} + +func NewNullableGmFruit(val *GmFruit) *NullableGmFruit { + return &NullableGmFruit{value: val, isSet: true} +} + +func (v NullableGmFruit) MarshalJSON() ([]byte, error) { + return json.Marshal(v.value) +} + +func (v *NullableGmFruit) UnmarshalJSON(src []byte) error { + v.isSet = true + return json.Unmarshal(src, &v.value) +} + diff --git a/samples/openapi3/client/petstore/go-experimental/go-petstore/model_mammal.go b/samples/openapi3/client/petstore/go-experimental/go-petstore/model_mammal.go index b540a411a4f..77e8ee3e97c 100644 --- a/samples/openapi3/client/petstore/go-experimental/go-petstore/model_mammal.go +++ b/samples/openapi3/client/petstore/go-experimental/go-petstore/model_mammal.go @@ -101,3 +101,39 @@ func (obj *Mammal) GetActualInstance() (interface{}) { return nil } +type NullableMammal struct { + value *Mammal + isSet bool +} + +func (v NullableMammal) Get() *Mammal { + return v.value +} + +func (v *NullableMammal) Set(val *Mammal) { + v.value = val + v.isSet = true +} + +func (v NullableMammal) IsSet() bool { + return v.isSet +} + +func (v *NullableMammal) Unset() { + v.value = nil + v.isSet = false +} + +func NewNullableMammal(val *Mammal) *NullableMammal { + return &NullableMammal{value: val, isSet: true} +} + +func (v NullableMammal) MarshalJSON() ([]byte, error) { + return json.Marshal(v.value) +} + +func (v *NullableMammal) UnmarshalJSON(src []byte) error { + v.isSet = true + return json.Unmarshal(src, &v.value) +} +