[Go][Experimental] Support additionalProperties (#6525)

* support additonal properties in go

* support additonal properties marshal in go exp

* update go samples
This commit is contained in:
William Cheng 2020-06-04 11:33:21 +08:00 committed by GitHub
parent b2305d731c
commit a6bf956df5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
101 changed files with 259 additions and 3 deletions

View File

@ -222,9 +222,12 @@ public class GoClientExperimentalCodegen extends GoClientCodegen {
if (model.anyOf != null && !model.anyOf.isEmpty()) {
imports.add(createMapping("import", "fmt"));
}
// add x-additional-properties
if ("map[string]map[string]interface{}".equals(model.parent)) {
model.vendorExtensions.put("x-additional-properties", true);
}
}
}
return objs;
}

View File

@ -13,8 +13,15 @@ type {{classname}} struct {
{{/description}}
{{name}} {{^required}}{{^isNullable}}*{{/isNullable}}{{/required}}{{{dataType}}} `json:"{{baseName}}{{^required}},omitempty{{/required}}"{{#withXml}} xml:"{{baseName}}{{#isXmlAttribute}},attr{{/isXmlAttribute}}"{{/withXml}}{{#vendorExtensions.x-go-custom-tag}} {{{.}}}{{/vendorExtensions.x-go-custom-tag}}`
{{/vars}}
{{#vendorExtensions.x-additional-properties}}
AdditionalProperties map[string]interface{}
{{/vendorExtensions.x-additional-properties}}
}
{{#vendorExtensions.x-additional-properties}}
type _{{{classname}}} {{{classname}}}
{{/vendorExtensions.x-additional-properties}}
// New{{classname}} instantiates a new {{classname}} 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
@ -246,7 +253,35 @@ func (o {{classname}}) MarshalJSON() ([]byte, error) {
}
{{/isNullable}}
{{/vars}}
{{#vendorExtensions.x-additional-properties}}
for key, value := range o.AdditionalProperties {
toSerialize[key] = value
}
{{/vendorExtensions.x-additional-properties}}
return json.Marshal(toSerialize)
}
{{#vendorExtensions.x-additional-properties}}
func (o *{{{classname}}}) UnmarshalJSON(bytes []byte) (err error) {
var{{{classname}}} := _{{{classname}}}{}
if err = json.Unmarshal(bytes, &var{{{classname}}}); err == nil {
*o = {{{classname}}}(var{{{classname}}})
}
additionalProperties := make(map[string]interface{})
if err = json.Unmarshal(bytes, &additionalProperties); err == nil {
{{#vars}}
delete(additionalProperties, "{{{baseName}}}")
{{/vars}}
o.AdditionalProperties = additionalProperties
}
return err
}
{{/vendorExtensions.x-additional-properties}}
{{>nullable_model}}

View File

@ -1810,6 +1810,7 @@ components:
type: string
banana:
type: object
additionalProperties: true
properties:
lengthCm:
type: number

View File

@ -147,3 +147,4 @@ func (v *NullableModel200Response) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -16,8 +16,11 @@ import (
// AdditionalPropertiesAnyType struct for AdditionalPropertiesAnyType
type AdditionalPropertiesAnyType struct {
Name *string `json:"name,omitempty"`
AdditionalProperties map[string]interface{}
}
type _AdditionalPropertiesAnyType AdditionalPropertiesAnyType
// NewAdditionalPropertiesAnyType instantiates a new AdditionalPropertiesAnyType 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
@ -72,9 +75,31 @@ func (o AdditionalPropertiesAnyType) MarshalJSON() ([]byte, error) {
if o.Name != nil {
toSerialize["name"] = o.Name
}
for key, value := range o.AdditionalProperties {
toSerialize[key] = value
}
return json.Marshal(toSerialize)
}
func (o *AdditionalPropertiesAnyType) UnmarshalJSON(bytes []byte) (err error) {
varAdditionalPropertiesAnyType := _AdditionalPropertiesAnyType{}
if err = json.Unmarshal(bytes, &varAdditionalPropertiesAnyType); err == nil {
*o = AdditionalPropertiesAnyType(varAdditionalPropertiesAnyType)
}
additionalProperties := make(map[string]interface{})
if err = json.Unmarshal(bytes, &additionalProperties); err == nil {
delete(additionalProperties, "name")
o.AdditionalProperties = additionalProperties
}
return err
}
type NullableAdditionalPropertiesAnyType struct {
value *AdditionalPropertiesAnyType
isSet bool
@ -111,3 +136,4 @@ func (v *NullableAdditionalPropertiesAnyType) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableAdditionalPropertiesArray) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableAdditionalPropertiesBoolean) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -471,3 +471,4 @@ func (v *NullableAdditionalPropertiesClass) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableAdditionalPropertiesInteger) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableAdditionalPropertiesNumber) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableAdditionalPropertiesObject) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableAdditionalPropertiesString) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -144,3 +144,4 @@ func (v *NullableAnimal) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -183,3 +183,4 @@ func (v *NullableApiResponse) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableArrayOfArrayOfNumberOnly) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableArrayOfNumberOnly) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -183,3 +183,4 @@ func (v *NullableArrayTest) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -120,3 +120,4 @@ func (v *NullableBigCat) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableBigCatAllOf) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -292,3 +292,4 @@ func (v *NullableCapitalization) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -120,3 +120,4 @@ func (v *NullableCat) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableCatAllOf) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -142,3 +142,4 @@ func (v *NullableCategory) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableClassModel) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableClient) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -120,3 +120,4 @@ func (v *NullableDog) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableDogAllOf) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -147,3 +147,4 @@ func (v *NullableEnumArrays) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -248,3 +248,4 @@ func (v *NullableEnumTest) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -112,3 +112,4 @@ func (v *NullableFile) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -147,3 +147,4 @@ func (v *NullableFileSchemaTestClass) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -553,3 +553,4 @@ func (v *NullableFormatTest) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -147,3 +147,4 @@ func (v *NullableHasOnlyReadOnly) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableList) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -219,3 +219,4 @@ func (v *NullableMapTest) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -184,3 +184,4 @@ func (v *NullableMixedPropertiesAndAdditionalPropertiesClass) UnmarshalJSON(src
return json.Unmarshal(src, &v.value)
}

View File

@ -212,3 +212,4 @@ func (v *NullableName) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableNumberOnly) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -297,3 +297,4 @@ func (v *NullableOrder) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -183,3 +183,4 @@ func (v *NullableOuterComposite) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -278,3 +278,4 @@ func (v *NullablePet) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -147,3 +147,4 @@ func (v *NullableReadOnlyFirst) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableReturn) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableSpecialModelName) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -147,3 +147,4 @@ func (v *NullableTag) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -224,3 +224,4 @@ func (v *NullableTypeHolderDefault) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -249,3 +249,4 @@ func (v *NullableTypeHolderExample) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -364,3 +364,4 @@ func (v *NullableUser) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -1119,3 +1119,4 @@ func (v *NullableXmlItem) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -1925,6 +1925,7 @@ components:
type: string
type: object
banana:
additionalProperties: true
properties:
lengthCm:
type: number

View File

@ -147,3 +147,4 @@ func (v *NullableModel200Response) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableSpecialModelName) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -147,3 +147,4 @@ func (v *NullableAdditionalPropertiesClass) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -144,3 +144,4 @@ func (v *NullableAnimal) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -183,3 +183,4 @@ func (v *NullableApiResponse) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableApple) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -140,3 +140,4 @@ func (v *NullableAppleReq) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableArrayOfArrayOfNumberOnly) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableArrayOfNumberOnly) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -183,3 +183,4 @@ func (v *NullableArrayTest) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -16,8 +16,11 @@ import (
// Banana struct for Banana
type Banana struct {
LengthCm *float32 `json:"lengthCm,omitempty"`
AdditionalProperties map[string]interface{}
}
type _Banana Banana
// NewBanana instantiates a new Banana 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
@ -72,9 +75,31 @@ func (o Banana) MarshalJSON() ([]byte, error) {
if o.LengthCm != nil {
toSerialize["lengthCm"] = o.LengthCm
}
for key, value := range o.AdditionalProperties {
toSerialize[key] = value
}
return json.Marshal(toSerialize)
}
func (o *Banana) UnmarshalJSON(bytes []byte) (err error) {
varBanana := _Banana{}
if err = json.Unmarshal(bytes, &varBanana); err == nil {
*o = Banana(varBanana)
}
additionalProperties := make(map[string]interface{})
if err = json.Unmarshal(bytes, &additionalProperties); err == nil {
delete(additionalProperties, "lengthCm")
o.AdditionalProperties = additionalProperties
}
return err
}
type NullableBanana struct {
value *Banana
isSet bool
@ -111,3 +136,4 @@ func (v *NullableBanana) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -140,3 +140,4 @@ func (v *NullableBananaReq) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -292,3 +292,4 @@ func (v *NullableCapitalization) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -120,3 +120,4 @@ func (v *NullableCat) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableCatAllOf) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -142,3 +142,4 @@ func (v *NullableCategory) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableClassModel) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableClient) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -120,3 +120,4 @@ func (v *NullableDog) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableDogAllOf) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -147,3 +147,4 @@ func (v *NullableEnumArrays) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -374,3 +374,4 @@ func (v *NullableEnumTest) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -112,3 +112,4 @@ func (v *NullableFile) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -147,3 +147,4 @@ func (v *NullableFileSchemaTestClass) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -115,3 +115,4 @@ func (v *NullableFoo) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -591,3 +591,4 @@ func (v *NullableFormatTest) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -147,3 +147,4 @@ func (v *NullableHasOnlyReadOnly) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -121,3 +121,4 @@ func (v *NullableHealthCheckResult) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -149,3 +149,4 @@ func (v *NullableInlineObject) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -150,3 +150,4 @@ func (v *NullableInlineObject1) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -153,3 +153,4 @@ func (v *NullableInlineObject2) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -567,3 +567,4 @@ func (v *NullableInlineObject3) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -135,3 +135,4 @@ func (v *NullableInlineObject4) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -143,3 +143,4 @@ func (v *NullableInlineObject5) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableInlineResponseDefault) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableList) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -219,3 +219,4 @@ func (v *NullableMapTest) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -184,3 +184,4 @@ func (v *NullableMixedPropertiesAndAdditionalPropertiesClass) UnmarshalJSON(src
return json.Unmarshal(src, &v.value)
}

View File

@ -212,3 +212,4 @@ func (v *NullableName) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -28,8 +28,11 @@ type NullableClass struct {
ObjectNullableProp map[string]map[string]interface{} `json:"object_nullable_prop,omitempty"`
ObjectAndItemsNullableProp map[string]map[string]interface{} `json:"object_and_items_nullable_prop,omitempty"`
ObjectItemsNullable *map[string]map[string]interface{} `json:"object_items_nullable,omitempty"`
AdditionalProperties map[string]interface{}
}
type _NullableClass NullableClass
// NewNullableClass instantiates a new NullableClass 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
@ -533,9 +536,42 @@ func (o NullableClass) MarshalJSON() ([]byte, error) {
if o.ObjectItemsNullable != nil {
toSerialize["object_items_nullable"] = o.ObjectItemsNullable
}
for key, value := range o.AdditionalProperties {
toSerialize[key] = value
}
return json.Marshal(toSerialize)
}
func (o *NullableClass) UnmarshalJSON(bytes []byte) (err error) {
varNullableClass := _NullableClass{}
if err = json.Unmarshal(bytes, &varNullableClass); err == nil {
*o = NullableClass(varNullableClass)
}
additionalProperties := make(map[string]interface{})
if err = json.Unmarshal(bytes, &additionalProperties); err == nil {
delete(additionalProperties, "integer_prop")
delete(additionalProperties, "number_prop")
delete(additionalProperties, "boolean_prop")
delete(additionalProperties, "string_prop")
delete(additionalProperties, "date_prop")
delete(additionalProperties, "datetime_prop")
delete(additionalProperties, "array_nullable_prop")
delete(additionalProperties, "array_and_items_nullable_prop")
delete(additionalProperties, "array_items_nullable")
delete(additionalProperties, "object_nullable_prop")
delete(additionalProperties, "object_and_items_nullable_prop")
delete(additionalProperties, "object_items_nullable")
o.AdditionalProperties = additionalProperties
}
return err
}
type NullableNullableClass struct {
value *NullableClass
isSet bool
@ -572,3 +608,4 @@ func (v *NullableNullableClass) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableNumberOnly) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -297,3 +297,4 @@ func (v *NullableOrder) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -183,3 +183,4 @@ func (v *NullableOuterComposite) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -278,3 +278,4 @@ func (v *NullablePet) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -147,3 +147,4 @@ func (v *NullableReadOnlyFirst) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -111,3 +111,4 @@ func (v *NullableReturn) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -147,3 +147,4 @@ func (v *NullableTag) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -515,3 +515,4 @@ func (v *NullableUser) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -176,3 +176,4 @@ func (v *NullableWhale) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

View File

@ -140,3 +140,4 @@ func (v *NullableZebra) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

Some files were not shown because too many files have changed in this diff Show More