diff --git a/.generator/schemas/v2/openapi.yaml b/.generator/schemas/v2/openapi.yaml index b8661d93ff7..14b2d035b57 100644 --- a/.generator/schemas/v2/openapi.yaml +++ b/.generator/schemas/v2/openapi.yaml @@ -19700,11 +19700,12 @@ components: - id type: object DeploymentRulesOptions: - description: Options for deployment rule response representing either faulty - deployment detection or monitor options. - oneOf: + anyOf: - $ref: '#/components/schemas/DeploymentRuleOptionsFaultyDeploymentDetection' - $ref: '#/components/schemas/DeploymentRuleOptionsMonitor' + description: Options for deployment rule response representing either faulty + deployment detection or monitor options. The actual type is determined by + the parent's 'type' field. DetachCaseRequest: description: Request for detaching security findings from their case. properties: diff --git a/api/datadogV2/model_create_deployment_rule_params_data_attributes.go b/api/datadogV2/model_create_deployment_rule_params_data_attributes.go index 279e3dcbe34..42e12fa1ce0 100644 --- a/api/datadogV2/model_create_deployment_rule_params_data_attributes.go +++ b/api/datadogV2/model_create_deployment_rule_params_data_attributes.go @@ -16,7 +16,7 @@ type CreateDeploymentRuleParamsDataAttributes struct { DryRun *bool `json:"dry_run,omitempty"` // The name of the deployment rule. Name string `json:"name"` - // Options for deployment rule response representing either faulty deployment detection or monitor options. + // Options for deployment rule response representing either faulty deployment detection or monitor options. The actual type is determined by the parent's 'type' field. Options DeploymentRulesOptions `json:"options"` // The type of the deployment rule (faulty_deployment_detection or monitor). Type string `json:"type"` @@ -191,8 +191,13 @@ func (o *CreateDeploymentRuleParamsDataAttributes) UnmarshalJSON(bytes []byte) ( } else { return err } + + hasInvalidField := false o.DryRun = all.DryRun o.Name = *all.Name + if all.Options.UnparsedObject != nil && o.UnparsedObject == nil { + hasInvalidField = true + } o.Options = *all.Options o.Type = *all.Type @@ -200,5 +205,9 @@ func (o *CreateDeploymentRuleParamsDataAttributes) UnmarshalJSON(bytes []byte) ( o.AdditionalProperties = additionalProperties } + if hasInvalidField { + return datadog.Unmarshal(bytes, &o.UnparsedObject) + } + return nil } diff --git a/api/datadogV2/model_deployment_rule_response_data_attributes.go b/api/datadogV2/model_deployment_rule_response_data_attributes.go index 9343aa9c467..dc2d8b33d42 100644 --- a/api/datadogV2/model_deployment_rule_response_data_attributes.go +++ b/api/datadogV2/model_deployment_rule_response_data_attributes.go @@ -23,7 +23,7 @@ type DeploymentRuleResponseDataAttributes struct { GateId string `json:"gate_id"` // The name of the deployment rule. Name string `json:"name"` - // Options for deployment rule response representing either faulty deployment detection or monitor options. + // Options for deployment rule response representing either faulty deployment detection or monitor options. The actual type is determined by the parent's 'type' field. Options DeploymentRulesOptions `json:"options"` // The type of the deployment rule. Type DeploymentRuleResponseDataAttributesType `json:"type"` @@ -364,6 +364,9 @@ func (o *DeploymentRuleResponseDataAttributes) UnmarshalJSON(bytes []byte) (err o.DryRun = *all.DryRun o.GateId = *all.GateId o.Name = *all.Name + if all.Options.UnparsedObject != nil && o.UnparsedObject == nil { + hasInvalidField = true + } o.Options = *all.Options if !all.Type.IsValid() { hasInvalidField = true diff --git a/api/datadogV2/model_deployment_rules_options.go b/api/datadogV2/model_deployment_rules_options.go index 4e68cb24cbc..2ba4cfa548b 100644 --- a/api/datadogV2/model_deployment_rules_options.go +++ b/api/datadogV2/model_deployment_rules_options.go @@ -8,98 +8,60 @@ import ( "github.com/DataDog/datadog-api-client-go/v2/api/datadog" ) -// DeploymentRulesOptions - Options for deployment rule response representing either faulty deployment detection or monitor options. +// DeploymentRulesOptions Options for deployment rule response representing either faulty deployment detection or monitor options. The actual type is determined by the parent's 'type' field. type DeploymentRulesOptions struct { - DeploymentRuleOptionsFaultyDeploymentDetection *DeploymentRuleOptionsFaultyDeploymentDetection - DeploymentRuleOptionsMonitor *DeploymentRuleOptionsMonitor - // UnparsedObject contains the raw value of the object if there was an error when deserializing into the struct - UnparsedObject interface{} + UnparsedObject map[string]interface{} `json:"-"` + AdditionalProperties map[string]interface{} `json:"-"` } -// DeploymentRuleOptionsFaultyDeploymentDetectionAsDeploymentRulesOptions is a convenience function that returns DeploymentRuleOptionsFaultyDeploymentDetection wrapped in DeploymentRulesOptions. -func DeploymentRuleOptionsFaultyDeploymentDetectionAsDeploymentRulesOptions(v *DeploymentRuleOptionsFaultyDeploymentDetection) DeploymentRulesOptions { - return DeploymentRulesOptions{DeploymentRuleOptionsFaultyDeploymentDetection: v} +// NewDeploymentRulesOptions instantiates a new DeploymentRulesOptions 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 NewDeploymentRulesOptions() *DeploymentRulesOptions { + this := DeploymentRulesOptions{} + return &this } -// DeploymentRuleOptionsMonitorAsDeploymentRulesOptions is a convenience function that returns DeploymentRuleOptionsMonitor wrapped in DeploymentRulesOptions. -func DeploymentRuleOptionsMonitorAsDeploymentRulesOptions(v *DeploymentRuleOptionsMonitor) DeploymentRulesOptions { - return DeploymentRulesOptions{DeploymentRuleOptionsMonitor: v} +// NewDeploymentRulesOptionsWithDefaults instantiates a new DeploymentRulesOptions 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 NewDeploymentRulesOptionsWithDefaults() *DeploymentRulesOptions { + this := DeploymentRulesOptions{} + return &this } -// UnmarshalJSON turns data into one of the pointers in the struct. -func (obj *DeploymentRulesOptions) UnmarshalJSON(data []byte) error { - var err error - match := 0 - // try to unmarshal data into DeploymentRuleOptionsFaultyDeploymentDetection - err = datadog.Unmarshal(data, &obj.DeploymentRuleOptionsFaultyDeploymentDetection) - if err == nil { - if obj.DeploymentRuleOptionsFaultyDeploymentDetection != nil && obj.DeploymentRuleOptionsFaultyDeploymentDetection.UnparsedObject == nil { - jsonDeploymentRuleOptionsFaultyDeploymentDetection, _ := datadog.Marshal(obj.DeploymentRuleOptionsFaultyDeploymentDetection) - if string(jsonDeploymentRuleOptionsFaultyDeploymentDetection) == "{}" && string(data) != "{}" { // empty struct - obj.DeploymentRuleOptionsFaultyDeploymentDetection = nil - } else { - match++ - } - } else { - obj.DeploymentRuleOptionsFaultyDeploymentDetection = nil - } - } else { - obj.DeploymentRuleOptionsFaultyDeploymentDetection = nil - } - - // try to unmarshal data into DeploymentRuleOptionsMonitor - err = datadog.Unmarshal(data, &obj.DeploymentRuleOptionsMonitor) - if err == nil { - if obj.DeploymentRuleOptionsMonitor != nil && obj.DeploymentRuleOptionsMonitor.UnparsedObject == nil { - jsonDeploymentRuleOptionsMonitor, _ := datadog.Marshal(obj.DeploymentRuleOptionsMonitor) - if string(jsonDeploymentRuleOptionsMonitor) == "{}" { // empty struct - obj.DeploymentRuleOptionsMonitor = nil - } else { - match++ - } - } else { - obj.DeploymentRuleOptionsMonitor = nil - } - } else { - obj.DeploymentRuleOptionsMonitor = nil +// MarshalJSON serializes the struct using spec logic. +func (o DeploymentRulesOptions) MarshalJSON() ([]byte, error) { + toSerialize := map[string]interface{}{} + if o.UnparsedObject != nil { + return datadog.Marshal(o.UnparsedObject) } - if match != 1 { // more than 1 match - // reset to nil - obj.DeploymentRuleOptionsFaultyDeploymentDetection = nil - obj.DeploymentRuleOptionsMonitor = nil - return datadog.Unmarshal(data, &obj.UnparsedObject) + for key, value := range o.AdditionalProperties { + toSerialize[key] = value } - return nil // exactly one match + return datadog.Marshal(toSerialize) } -// MarshalJSON turns data from the first non-nil pointers in the struct to JSON. -func (obj DeploymentRulesOptions) MarshalJSON() ([]byte, error) { - if obj.DeploymentRuleOptionsFaultyDeploymentDetection != nil { - return datadog.Marshal(&obj.DeploymentRuleOptionsFaultyDeploymentDetection) - } - - if obj.DeploymentRuleOptionsMonitor != nil { - return datadog.Marshal(&obj.DeploymentRuleOptionsMonitor) - } - - if obj.UnparsedObject != nil { - return datadog.Marshal(obj.UnparsedObject) +// UnmarshalJSON deserializes the given payload. +func (o *DeploymentRulesOptions) UnmarshalJSON(bytes []byte) (err error) { + all := struct { + }{} + if err = datadog.Unmarshal(bytes, &all); err != nil { + return datadog.Unmarshal(bytes, &o.UnparsedObject) } - return nil, nil // no data in oneOf schemas -} - -// GetActualInstance returns the actual instance. -func (obj *DeploymentRulesOptions) GetActualInstance() interface{} { - if obj.DeploymentRuleOptionsFaultyDeploymentDetection != nil { - return obj.DeploymentRuleOptionsFaultyDeploymentDetection + additionalProperties := make(map[string]interface{}) + if err = datadog.Unmarshal(bytes, &additionalProperties); err == nil { + datadog.DeleteKeys(additionalProperties, &[]string{}) + } else { + return err } - if obj.DeploymentRuleOptionsMonitor != nil { - return obj.DeploymentRuleOptionsMonitor + if len(additionalProperties) > 0 { + o.AdditionalProperties = additionalProperties } - // all schemas are nil return nil } diff --git a/api/datadogV2/model_update_deployment_rule_params_data_attributes.go b/api/datadogV2/model_update_deployment_rule_params_data_attributes.go index e5f292188f5..c1c8cda7814 100644 --- a/api/datadogV2/model_update_deployment_rule_params_data_attributes.go +++ b/api/datadogV2/model_update_deployment_rule_params_data_attributes.go @@ -16,7 +16,7 @@ type UpdateDeploymentRuleParamsDataAttributes struct { DryRun bool `json:"dry_run"` // The name of the deployment rule. Name string `json:"name"` - // Options for deployment rule response representing either faulty deployment detection or monitor options. + // Options for deployment rule response representing either faulty deployment detection or monitor options. The actual type is determined by the parent's 'type' field. Options DeploymentRulesOptions `json:"options"` // UnparsedObject contains the raw value of the object if there was an error when deserializing into the struct UnparsedObject map[string]interface{} `json:"-"` @@ -153,13 +153,22 @@ func (o *UpdateDeploymentRuleParamsDataAttributes) UnmarshalJSON(bytes []byte) ( } else { return err } + + hasInvalidField := false o.DryRun = *all.DryRun o.Name = *all.Name + if all.Options.UnparsedObject != nil && o.UnparsedObject == nil { + hasInvalidField = true + } o.Options = *all.Options if len(additionalProperties) > 0 { o.AdditionalProperties = additionalProperties } + if hasInvalidField { + return datadog.Unmarshal(bytes, &o.UnparsedObject) + } + return nil } diff --git a/examples/v2/deployment-gates/CreateDeploymentRule.go b/examples/v2/deployment-gates/CreateDeploymentRule.go index 823efec8338..7fa4ab299e8 100644 --- a/examples/v2/deployment-gates/CreateDeploymentRule.go +++ b/examples/v2/deployment-gates/CreateDeploymentRule.go @@ -19,13 +19,10 @@ func main() { body := datadogV2.CreateDeploymentRuleParams{ Data: &datadogV2.CreateDeploymentRuleParamsData{ Attributes: datadogV2.CreateDeploymentRuleParamsDataAttributes{ - DryRun: datadog.PtrBool(false), - Name: "My deployment rule", - Options: datadogV2.DeploymentRulesOptions{ - DeploymentRuleOptionsFaultyDeploymentDetection: &datadogV2.DeploymentRuleOptionsFaultyDeploymentDetection{ - ExcludedResources: []string{}, - }}, - Type: "faulty_deployment_detection", + DryRun: datadog.PtrBool(false), + Name: "My deployment rule", + Options: datadogV2.DeploymentRulesOptions{}, + Type: "faulty_deployment_detection", }, Type: datadogV2.DEPLOYMENTRULEDATATYPE_DEPLOYMENT_RULE, }, diff --git a/examples/v2/deployment-gates/UpdateDeploymentRule.go b/examples/v2/deployment-gates/UpdateDeploymentRule.go index 3c12e3d682e..d112806cceb 100644 --- a/examples/v2/deployment-gates/UpdateDeploymentRule.go +++ b/examples/v2/deployment-gates/UpdateDeploymentRule.go @@ -22,12 +22,9 @@ func main() { body := datadogV2.UpdateDeploymentRuleParams{ Data: datadogV2.UpdateDeploymentRuleParamsData{ Attributes: datadogV2.UpdateDeploymentRuleParamsDataAttributes{ - DryRun: false, - Name: "Updated deployment rule", - Options: datadogV2.DeploymentRulesOptions{ - DeploymentRuleOptionsFaultyDeploymentDetection: &datadogV2.DeploymentRuleOptionsFaultyDeploymentDetection{ - ExcludedResources: []string{}, - }}, + DryRun: false, + Name: "Updated deployment rule", + Options: datadogV2.DeploymentRulesOptions{}, }, Type: datadogV2.DEPLOYMENTRULEDATATYPE_DEPLOYMENT_RULE, }, diff --git a/tests/scenarios/features/v2/deployment_gates.feature b/tests/scenarios/features/v2/deployment_gates.feature index cb0c5a1262d..7f9dd2ada60 100644 --- a/tests/scenarios/features/v2/deployment_gates.feature +++ b/tests/scenarios/features/v2/deployment_gates.feature @@ -49,7 +49,7 @@ Feature: Deployment Gates Given operation "CreateDeploymentRule" enabled And new "CreateDeploymentRule" request And request contains "gate_id" parameter from "REPLACE.ME" - And body with value {"data": {"attributes": {"dry_run": false, "name": "My deployment rule", "options": {"duration": 3600, "excluded_resources": ["resource1", "resource2"]}, "type": "faulty_deployment_detection"}, "type": "deployment_rule"}} + And body with value {"data": {"attributes": {"dry_run": false, "name": "My deployment rule", "type": "faulty_deployment_detection"}, "type": "deployment_rule"}} When the request is sent Then the response status is 400 Bad request. @@ -279,7 +279,7 @@ Feature: Deployment Gates And new "UpdateDeploymentRule" request And request contains "gate_id" parameter from "REPLACE.ME" And request contains "id" parameter from "REPLACE.ME" - And body with value {"data": {"attributes": {"dry_run": false, "name": "Updated deployment rule", "options": {"duration": 3600, "excluded_resources": ["resource1", "resource2"]}}, "type": "deployment_rule"}} + And body with value {"data": {"attributes": {"dry_run": false, "name": "Updated deployment rule"}, "type": "deployment_rule"}} When the request is sent Then the response status is 400 Bad request. @@ -289,7 +289,7 @@ Feature: Deployment Gates And new "UpdateDeploymentRule" request And request contains "gate_id" parameter from "REPLACE.ME" And request contains "id" parameter from "REPLACE.ME" - And body with value {"data": {"attributes": {"dry_run": false, "name": "Updated deployment rule", "options": {"duration": 3600, "excluded_resources": ["resource1", "resource2"]}}, "type": "deployment_rule"}} + And body with value {"data": {"attributes": {"dry_run": false, "name": "Updated deployment rule"}, "type": "deployment_rule"}} When the request is sent Then the response status is 404 Deployment rule not found.