Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for deserializing additionalProperties in GO client #1951

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
8 changes: 4 additions & 4 deletions .apigentools-info
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"spec_versions": {
"v1": {
"apigentools_version": "1.6.4",
"regenerated": "2023-04-20 22:10:31.283833",
"spec_repo_commit": "24cbf678"
"regenerated": "2023-04-21 13:30:20.165282",
"spec_repo_commit": "63967d49"
},
"v2": {
"apigentools_version": "1.6.4",
"regenerated": "2023-04-20 22:10:31.297102",
"spec_repo_commit": "24cbf678"
"regenerated": "2023-04-21 13:30:20.178029",
"spec_repo_commit": "63967d49"
}
}
}
2 changes: 2 additions & 0 deletions .generator/src/generator/templates/model.j2
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package {{ package_name }}
import (
"encoding/json"
"fmt"

"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
{#imports
"{{import}}"
#}
Expand Down
128 changes: 16 additions & 112 deletions .generator/src/generator/templates/model_simple.j2
Original file line number Diff line number Diff line change
Expand Up @@ -318,118 +318,6 @@ toSerialize["{{ attr }}"] = o.{{ propertyName }}
return json.Marshal(toSerialize)
}

{#
{%- if model.get("additionalProperties", True) %}
func (o *{{ name }}) UnmarshalJSON(bytes []byte) (err error) {
{{#parent}}
{{^isMap}}
type {{ propertyName }}WithoutEmbeddedStruct struct {
{{#vars}}
{{^-first}}
{{/-first}}
{{#description}}
// {{{.}}}
{{/description}}
{%- if spec.deprecated %}
// Deprecated
{%- endif %}
{{ propertyName }} {% if attr not in model.get("required", []) %}{% if not isNullable %}{% if not isAnyType %}*{% endif %}{%- endif %}{% endif %}{{ dataType }} `json:"{{ attr }}{% if attr not in model.get("required", []) %},omitempty{% endif %}"{{#withXml}} xml:"{{ attr }}{{#isXmlAttribute}},attr{{/isXmlAttribute}}"{{/withXml}}{{#vendorExtensions.x-go-custom-tag}} {{{.}}}{{/vendorExtensions.x-go-custom-tag}}`
{{/vars}}
}

var{{ propertyName }}WithoutEmbeddedStruct := {{ propertyName }}WithoutEmbeddedStruct{}

err = json.Unmarshal(bytes, &var{{ propertyName }}WithoutEmbeddedStruct)
if err == nil {
var{{ propertyName }} := _{{ propertyName }}{}
{{#vars}}
var{{ propertyName }}.{{ propertyName }} = var{{ propertyName }}WithoutEmbeddedStruct.{{ propertyName }}
{{/vars}}
*o = {{ propertyName }}(var{{ propertyName }})
} else {
return err
}

var{{ propertyName }} := _{{ propertyName }}{}

err = json.Unmarshal(bytes, &var{{ propertyName }})
if err == nil {
o.{{{parent}}} = var{{ propertyName }}.{{{parent}}}
} else {
return err
}

additionalProperties := make(map[string]interface{})

if err = json.Unmarshal(bytes, &additionalProperties); err == nil {
{{#vars}}
delete(additionalProperties, "{{{ attr }}}")
{{/vars}}

// remove fields from embedded structs
reflect{{{parent}}} := reflect.ValueOf(o.{{{parent}}})
for i := 0; i < reflect{{{parent}}}.Type().NumField(); i++ {
t := reflect{{{parent}}}.Type().Field(i)

if jsonTag := t.Tag.Get("json"); jsonTag != "" {
fieldName := ""
if commaIdx := strings.Index(jsonTag, ","); commaIdx > 0 {
fieldName = jsonTag[:commaIdx]
} else {
fieldName = jsonTag
}
if fieldName != "AdditionalProperties" {
delete(additionalProperties, fieldName)
}
}
}

o.AdditionalProperties = additionalProperties
}

return err
{{/isMap}}
{{#isMap}}
var{{ propertyName }} := _{{ propertyName }}{}

if err = json.Unmarshal(bytes, &var{{ propertyName }}); err == nil {
*o = {{ propertyName }}(var{{ propertyName }})
}

additionalProperties := make(map[string]interface{})

if err = json.Unmarshal(bytes, &additionalProperties); err == nil {
{{#vars}}
delete(additionalProperties, "{{{ attr }}}")
{{/vars}}
o.AdditionalProperties = additionalProperties
}

return err
{{/isMap}}
{{/parent}}
{{^parent}}
var{{ propertyName }} := _{{ propertyName }}{}

if err = json.Unmarshal(bytes, &var{{ propertyName }}); err == nil {
*o = {{ propertyName }}(var{{ propertyName }})
}

additionalProperties := make(map[string]interface{})

if err = json.Unmarshal(bytes, &additionalProperties); err == nil {
{{#vars}}
delete(additionalProperties, "{{{ attr }}}")
{{/vars}}
o.AdditionalProperties = additionalProperties
}

return err
{{/parent}}
}

{{/isAdditionalPropertiesTrue}}
#}
// UnmarshalJSON deserializes the given payload.
{%- if model.type == "array" %}
func (o *{{ name }}) UnmarshalJSON(bytes []byte) (err error) {
Expand Down Expand Up @@ -483,6 +371,16 @@ func (o *{{ name }}) UnmarshalJSON(bytes []byte) (err error) {
o.UnparsedObject = raw
return nil
}

{%- if model.get("additionalProperties", True) %}
additionalProperties := make(map[string]{{ get_type(model.get("additionalProperties", True)) }})
if err = json.Unmarshal(bytes, &additionalProperties); err == nil {
datadog.DeleteKeys(additionalProperties, &[]string{ {% for attr, schema in model.get("properties", {}).items()%}"{{ attr }}", {% endfor %} })
} else {
return err
}
{%- endif %}

{#- check enums #}
{%- for attr, spec in model.get("properties", {}).items() if spec.enum is defined %}
{%- set propertyName = attr|attribute_name %}
Expand Down Expand Up @@ -510,6 +408,12 @@ func (o *{{ name }}) UnmarshalJSON(bytes []byte) (err error) {
{%- endif %}
o.{{ propertyName }} = all.{{ propertyName }}
{%- endfor %}
{%- if model.get("additionalProperties", True) %}
if len(additionalProperties) > 0 {
o.AdditionalProperties = additionalProperties
}
{%- endif %}

return nil
}

Expand Down
7 changes: 7 additions & 0 deletions .generator/src/generator/templates/utils.j2
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,13 @@ func (v *NullableTime) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

// DeleteKeys helper method to delete keys from a map
func DeleteKeys[V any](obj map[string]V, keysToDelete *[]string) {
for _, s := range *keysToDelete {
delete(obj, s)
}
}

// ContainsUnparsedObject returns true if the given data contains an unparsed object from the API.
func ContainsUnparsedObject(i interface{}) (bool, interface{}) {
v := reflect.ValueOf(i)
Expand Down
7 changes: 7 additions & 0 deletions api/datadog/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,13 @@ func (v *NullableTime) UnmarshalJSON(src []byte) error {
return json.Unmarshal(src, &v.value)
}

// DeleteKeys helper method to delete keys from a map
func DeleteKeys[V any](obj map[string]V, keysToDelete *[]string) {
for _, s := range *keysToDelete {
delete(obj, s)
}
}

// ContainsUnparsedObject returns true if the given data contains an unparsed object from the API.
func ContainsUnparsedObject(i interface{}) (bool, interface{}) {
v := reflect.ValueOf(i)
Expand Down
12 changes: 12 additions & 0 deletions api/datadogV1/model_add_signal_to_incident_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package datadogV1
import (
"encoding/json"
"fmt"

"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
)

// AddSignalToIncidentRequest Attributes describing which incident to add the signal to.
Expand Down Expand Up @@ -166,8 +168,18 @@ func (o *AddSignalToIncidentRequest) UnmarshalJSON(bytes []byte) (err error) {
o.UnparsedObject = raw
return nil
}
additionalProperties := make(map[string]interface{})
if err = json.Unmarshal(bytes, &additionalProperties); err == nil {
datadog.DeleteKeys(additionalProperties, &[]string{"add_to_signal_timeline", "incident_id", "version"})
} else {
return err
}
o.AddToSignalTimeline = all.AddToSignalTimeline
o.IncidentId = all.IncidentId
o.Version = all.Version
if len(additionalProperties) > 0 {
o.AdditionalProperties = additionalProperties
}

return nil
}
12 changes: 12 additions & 0 deletions api/datadogV1/model_alert_graph_widget_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package datadogV1
import (
"encoding/json"
"fmt"

"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
)

// AlertGraphWidgetDefinition Alert graphs are timeseries graphs showing the current status of any monitor defined on your system.
Expand Down Expand Up @@ -300,6 +302,12 @@ func (o *AlertGraphWidgetDefinition) UnmarshalJSON(bytes []byte) (err error) {
o.UnparsedObject = raw
return nil
}
additionalProperties := make(map[string]interface{})
if err = json.Unmarshal(bytes, &additionalProperties); err == nil {
datadog.DeleteKeys(additionalProperties, &[]string{"alert_id", "time", "title", "title_align", "title_size", "type", "viz_type"})
} else {
return err
}
if v := all.TitleAlign; v != nil && !v.IsValid() {
err = json.Unmarshal(bytes, &raw)
if err != nil {
Expand Down Expand Up @@ -338,5 +346,9 @@ func (o *AlertGraphWidgetDefinition) UnmarshalJSON(bytes []byte) (err error) {
o.TitleSize = all.TitleSize
o.Type = all.Type
o.VizType = all.VizType
if len(additionalProperties) > 0 {
o.AdditionalProperties = additionalProperties
}

return nil
}
12 changes: 12 additions & 0 deletions api/datadogV1/model_alert_value_widget_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package datadogV1
import (
"encoding/json"
"fmt"

"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
)

// AlertValueWidgetDefinition Alert values are query values showing the current value of the metric in any monitor defined on your system.
Expand Down Expand Up @@ -336,6 +338,12 @@ func (o *AlertValueWidgetDefinition) UnmarshalJSON(bytes []byte) (err error) {
o.UnparsedObject = raw
return nil
}
additionalProperties := make(map[string]interface{})
if err = json.Unmarshal(bytes, &additionalProperties); err == nil {
datadog.DeleteKeys(additionalProperties, &[]string{"alert_id", "precision", "text_align", "title", "title_align", "title_size", "type", "unit"})
} else {
return err
}
if v := all.TextAlign; v != nil && !v.IsValid() {
err = json.Unmarshal(bytes, &raw)
if err != nil {
Expand Down Expand Up @@ -368,5 +376,9 @@ func (o *AlertValueWidgetDefinition) UnmarshalJSON(bytes []byte) (err error) {
o.TitleSize = all.TitleSize
o.Type = all.Type
o.Unit = all.Unit
if len(additionalProperties) > 0 {
o.AdditionalProperties = additionalProperties
}

return nil
}
12 changes: 12 additions & 0 deletions api/datadogV1/model_api_error_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package datadogV1
import (
"encoding/json"
"fmt"

"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
)

// APIErrorResponse Error response object.
Expand Down Expand Up @@ -98,6 +100,16 @@ func (o *APIErrorResponse) UnmarshalJSON(bytes []byte) (err error) {
o.UnparsedObject = raw
return nil
}
additionalProperties := make(map[string]interface{})
if err = json.Unmarshal(bytes, &additionalProperties); err == nil {
datadog.DeleteKeys(additionalProperties, &[]string{"errors"})
} else {
return err
}
o.Errors = all.Errors
if len(additionalProperties) > 0 {
o.AdditionalProperties = additionalProperties
}

return nil
}
12 changes: 12 additions & 0 deletions api/datadogV1/model_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package datadogV1

import (
"encoding/json"

"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
)

// ApiKey Datadog API key.
Expand Down Expand Up @@ -195,9 +197,19 @@ func (o *ApiKey) UnmarshalJSON(bytes []byte) (err error) {
o.UnparsedObject = raw
return nil
}
additionalProperties := make(map[string]interface{})
if err = json.Unmarshal(bytes, &additionalProperties); err == nil {
datadog.DeleteKeys(additionalProperties, &[]string{"created", "created_by", "key", "name"})
} else {
return err
}
o.Created = all.Created
o.CreatedBy = all.CreatedBy
o.Key = all.Key
o.Name = all.Name
if len(additionalProperties) > 0 {
o.AdditionalProperties = additionalProperties
}

return nil
}
12 changes: 12 additions & 0 deletions api/datadogV1/model_api_key_list_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package datadogV1

import (
"encoding/json"

"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
)

// ApiKeyListResponse List of API and application keys available for a given organization.
Expand Down Expand Up @@ -93,6 +95,16 @@ func (o *ApiKeyListResponse) UnmarshalJSON(bytes []byte) (err error) {
o.UnparsedObject = raw
return nil
}
additionalProperties := make(map[string]interface{})
if err = json.Unmarshal(bytes, &additionalProperties); err == nil {
datadog.DeleteKeys(additionalProperties, &[]string{"api_keys"})
} else {
return err
}
o.ApiKeys = all.ApiKeys
if len(additionalProperties) > 0 {
o.AdditionalProperties = additionalProperties
}

return nil
}
Loading