Skip to content

Commit 48f26e6

Browse files
authored
bugfix: required fields with null value failed to pass validation (#484)
1 parent fedc710 commit 48f26e6

File tree

4 files changed

+118
-3
lines changed

4 files changed

+118
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ ENHANCEMENTS:
99
BUG FIXES:
1010
- Fix a bug when upgrading from previous provider `azapi_resource` resource will set `tags` for resources that don't have `tags` in the configuration.
1111
- Fix a bug that `azapi_resource` resource cannot handle tags with unknown values.
12-
- Fix a bug that `null` value can't pass the schema validation.
12+
- Fix a bug that `null` string value can't pass the schema validation.
13+
- Fix a bug that required fields which have `null` value can't pass the schema validation.
1314
- Fix a bug that schema validation fails to validate the float number in the body.
1415
- Fix a bug that client certificate authentication doesn't work.
1516
- Fix a bug that auxiliary tenant ids are not passed to the client.

internal/azure/types/discriminated_object_type.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ func (t *DiscriminatedObjectType) Validate(body interface{}, path string) []erro
9494

9595
// check required base properties
9696
for key, value := range t.BaseProperties {
97-
if value.IsRequired() && bodyMap[key] == nil {
97+
if !value.IsRequired() {
98+
continue
99+
}
100+
if _, ok := bodyMap[key]; !ok {
98101
errors = append(errors, utils.ErrorShouldDefine(path+"."+key))
99102
}
100103
}

internal/azure/types/object_type.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ func (t *ObjectType) Validate(body interface{}, path string) []error {
8585

8686
// check properties required in schema, but not in body
8787
for key, value := range t.Properties {
88-
if value.IsRequired() && bodyMap[key] == nil {
88+
if !value.IsRequired() {
89+
continue
90+
}
91+
if _, ok := bodyMap[key]; !ok {
8992
// skip name in body
9093
if path == "" && key == "name" {
9194
continue

internal/azure/validator_test.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,114 @@ func Test_BodyValidation(t *testing.T) {
192192
// the bicep-types-az parses float as integer type and it should be fixed: https://github.com/Azure/bicep-types-az/issues/1404
193193
Error: false,
194194
},
195+
{
196+
Id: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myresourcegroup/providers/Microsoft.Consumption/budgets/mybudget",
197+
ApiVersion: "2021-10-01",
198+
Body: `
199+
{
200+
"properties": {
201+
"amount": 10,
202+
"category": "Cost",
203+
"notifications": {
204+
"notification1": {
205+
"enabled": true,
206+
"operator": "GreaterThanOrEqualTo",
207+
"threshold": 50,
208+
"thresholdType": "Actual",
209+
"contactEmails": [],
210+
"contactGroups": []
211+
}
212+
},
213+
"timeGrain": "Annually",
214+
"timePeriod": {
215+
"endDate": "foo",
216+
"startDate": "bar"
217+
}
218+
}
219+
}
220+
`,
221+
Error: false,
222+
},
223+
{
224+
Id: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myresourcegroup/providers/Microsoft.Consumption/budgets/mybudget",
225+
ApiVersion: "2021-10-01",
226+
Body: `
227+
{
228+
"properties": {
229+
"amount": 10,
230+
"category": "Cost",
231+
"notifications": {
232+
"notification1": {
233+
"enabled": true,
234+
"operator": "GreaterThanOrEqualTo",
235+
"threshold": 50,
236+
"thresholdType": "Actual",
237+
"contactEmails": nil,
238+
"contactGroups": []
239+
}
240+
},
241+
"timeGrain": "Annually",
242+
"timePeriod": {
243+
"endDate": "foo",
244+
"startDate": "bar"
245+
}
246+
}
247+
}
248+
`,
249+
Error: false,
250+
},
251+
{
252+
Id: "/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Subscription/aliases/alias1",
253+
ApiVersion: "2021-10-01",
254+
Body: `
255+
{
256+
"properties": {
257+
"displayName": "My Subscription",
258+
"workload": "Production",
259+
"billingScope": "Shared",
260+
"additionalProperties": {
261+
"managementGroupId": nil,
262+
"tags": {
263+
"key1": "value1",
264+
"key2": "value2"
265+
}
266+
}
267+
}
268+
}`,
269+
Error: false,
270+
},
271+
{
272+
Id: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.App/containerApps/containerApp",
273+
ApiVersion: "2022-03-01",
274+
Body: `
275+
{
276+
"location": "westus",
277+
"properties": {
278+
"configuration": {
279+
"activeRevisionsMode": "Single"
280+
},
281+
"managedEnvironmentId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.App/managedEnvironments/managedEnv1",
282+
"template": {
283+
"containers": [
284+
{
285+
"env": [],
286+
"image": "jackofallops/azure-containerapps-python-acctest:v0.0.1",
287+
"name": "first",
288+
"probes": [],
289+
"resources": {
290+
"cpu": 0.25,
291+
"memory": "0.5Gi"
292+
}
293+
}
294+
],
295+
"scale": {
296+
"maxReplicas": 10
297+
}
298+
}
299+
}
300+
}`,
301+
Error: false,
302+
},
195303
}
196304

197305
for index, data := range testData {

0 commit comments

Comments
 (0)