Skip to content

Commit

Permalink
bugfix: required fields with null value failed to pass validation (#484)
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-henglu committed Apr 26, 2024
1 parent fedc710 commit 48f26e6
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ ENHANCEMENTS:
BUG FIXES:
- Fix a bug when upgrading from previous provider `azapi_resource` resource will set `tags` for resources that don't have `tags` in the configuration.
- Fix a bug that `azapi_resource` resource cannot handle tags with unknown values.
- Fix a bug that `null` value can't pass the schema validation.
- Fix a bug that `null` string value can't pass the schema validation.
- Fix a bug that required fields which have `null` value can't pass the schema validation.
- Fix a bug that schema validation fails to validate the float number in the body.
- Fix a bug that client certificate authentication doesn't work.
- Fix a bug that auxiliary tenant ids are not passed to the client.
Expand Down
5 changes: 4 additions & 1 deletion internal/azure/types/discriminated_object_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ func (t *DiscriminatedObjectType) Validate(body interface{}, path string) []erro

// check required base properties
for key, value := range t.BaseProperties {
if value.IsRequired() && bodyMap[key] == nil {
if !value.IsRequired() {
continue
}
if _, ok := bodyMap[key]; !ok {
errors = append(errors, utils.ErrorShouldDefine(path+"."+key))
}
}
Expand Down
5 changes: 4 additions & 1 deletion internal/azure/types/object_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ func (t *ObjectType) Validate(body interface{}, path string) []error {

// check properties required in schema, but not in body
for key, value := range t.Properties {
if value.IsRequired() && bodyMap[key] == nil {
if !value.IsRequired() {
continue
}
if _, ok := bodyMap[key]; !ok {
// skip name in body
if path == "" && key == "name" {
continue
Expand Down
108 changes: 108 additions & 0 deletions internal/azure/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,114 @@ func Test_BodyValidation(t *testing.T) {
// the bicep-types-az parses float as integer type and it should be fixed: https://github.com/Azure/bicep-types-az/issues/1404
Error: false,
},
{
Id: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myresourcegroup/providers/Microsoft.Consumption/budgets/mybudget",
ApiVersion: "2021-10-01",
Body: `
{
"properties": {
"amount": 10,
"category": "Cost",
"notifications": {
"notification1": {
"enabled": true,
"operator": "GreaterThanOrEqualTo",
"threshold": 50,
"thresholdType": "Actual",
"contactEmails": [],
"contactGroups": []
}
},
"timeGrain": "Annually",
"timePeriod": {
"endDate": "foo",
"startDate": "bar"
}
}
}
`,
Error: false,
},
{
Id: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myresourcegroup/providers/Microsoft.Consumption/budgets/mybudget",
ApiVersion: "2021-10-01",
Body: `
{
"properties": {
"amount": 10,
"category": "Cost",
"notifications": {
"notification1": {
"enabled": true,
"operator": "GreaterThanOrEqualTo",
"threshold": 50,
"thresholdType": "Actual",
"contactEmails": nil,
"contactGroups": []
}
},
"timeGrain": "Annually",
"timePeriod": {
"endDate": "foo",
"startDate": "bar"
}
}
}
`,
Error: false,
},
{
Id: "/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Subscription/aliases/alias1",
ApiVersion: "2021-10-01",
Body: `
{
"properties": {
"displayName": "My Subscription",
"workload": "Production",
"billingScope": "Shared",
"additionalProperties": {
"managementGroupId": nil,
"tags": {
"key1": "value1",
"key2": "value2"
}
}
}
}`,
Error: false,
},
{
Id: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.App/containerApps/containerApp",
ApiVersion: "2022-03-01",
Body: `
{
"location": "westus",
"properties": {
"configuration": {
"activeRevisionsMode": "Single"
},
"managedEnvironmentId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.App/managedEnvironments/managedEnv1",
"template": {
"containers": [
{
"env": [],
"image": "jackofallops/azure-containerapps-python-acctest:v0.0.1",
"name": "first",
"probes": [],
"resources": {
"cpu": 0.25,
"memory": "0.5Gi"
}
}
],
"scale": {
"maxReplicas": 10
}
}
}
}`,
Error: false,
},
}

for index, data := range testData {
Expand Down

0 comments on commit 48f26e6

Please sign in to comment.