Skip to content

Commit

Permalink
[Go][Experimental] Support additionalProperties (#6525)
Browse files Browse the repository at this point in the history
* support additonal properties in go

* support additonal properties marshal in go exp

* update go samples
  • Loading branch information
wing328 authored Jun 4, 2020
1 parent b2305d7 commit a6bf956
Show file tree
Hide file tree
Showing 101 changed files with 259 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,12 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
{{>nullable_model}}
{{#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}}
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,7 @@ components:
type: string
banana:
type: object
additionalProperties: true
properties:
lengthCm:
type: number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,4 @@ func (v *NullableModel200Response) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -111,3 +136,4 @@ func (v *NullableAdditionalPropertiesAnyType) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ func (v *NullableAdditionalPropertiesArray) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ func (v *NullableAdditionalPropertiesBoolean) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -471,3 +471,4 @@ func (v *NullableAdditionalPropertiesClass) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ func (v *NullableAdditionalPropertiesInteger) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ func (v *NullableAdditionalPropertiesNumber) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ func (v *NullableAdditionalPropertiesObject) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ func (v *NullableAdditionalPropertiesString) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,4 @@ func (v *NullableAnimal) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,4 @@ func (v *NullableApiResponse) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ func (v *NullableArrayOfArrayOfNumberOnly) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ func (v *NullableArrayOfNumberOnly) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,4 @@ func (v *NullableArrayTest) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,4 @@ func (v *NullableBigCat) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ func (v *NullableBigCatAllOf) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,4 @@ func (v *NullableCapitalization) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,4 @@ func (v *NullableCat) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ func (v *NullableCatAllOf) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,4 @@ func (v *NullableCategory) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ func (v *NullableClassModel) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ func (v *NullableClient) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,4 @@ func (v *NullableDog) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ func (v *NullableDogAllOf) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,4 @@ func (v *NullableEnumArrays) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,4 @@ func (v *NullableEnumTest) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,4 @@ func (v *NullableFile) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,4 @@ func (v *NullableFileSchemaTestClass) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -553,3 +553,4 @@ func (v *NullableFormatTest) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,4 @@ func (v *NullableHasOnlyReadOnly) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ func (v *NullableList) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,4 @@ func (v *NullableMapTest) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,4 @@ func (v *NullableMixedPropertiesAndAdditionalPropertiesClass) UnmarshalJSON(src
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,4 @@ func (v *NullableName) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ func (v *NullableNumberOnly) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,4 @@ func (v *NullableOrder) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,4 @@ func (v *NullableOuterComposite) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,4 @@ func (v *NullablePet) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,4 @@ func (v *NullableReadOnlyFirst) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ func (v *NullableReturn) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ func (v *NullableSpecialModelName) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,4 @@ func (v *NullableTag) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,4 @@ func (v *NullableTypeHolderDefault) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,4 @@ func (v *NullableTypeHolderExample) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,4 @@ func (v *NullableUser) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -1119,3 +1119,4 @@ func (v *NullableXmlItem) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -1925,6 +1925,7 @@ components:
type: string
type: object
banana:
additionalProperties: true
properties:
lengthCm:
type: number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,4 @@ func (v *NullableModel200Response) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ func (v *NullableSpecialModelName) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,4 @@ func (v *NullableAdditionalPropertiesClass) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,4 @@ func (v *NullableAnimal) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,4 @@ func (v *NullableApiResponse) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ func (v *NullableApple) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,4 @@ func (v *NullableAppleReq) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ func (v *NullableArrayOfArrayOfNumberOnly) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}


Loading

0 comments on commit a6bf956

Please sign in to comment.