Skip to content

Commit aef1cc6

Browse files
authored
Merge pull request #32169 from hashicorp/b-api_gateway_rest_api_crash
aws_api_gateway_rest_api: fix crash on `binary_media_types` attribute
2 parents 42fc4c0 + 621b1b3 commit aef1cc6

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

.changelog/32169.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
resource/aws_api_gateway_rest_api: Fix crash when `binary_media_types` is `null`
3+
```

internal/service/apigateway/rest_api.go

+25-17
Original file line numberDiff line numberDiff line change
@@ -395,19 +395,23 @@ func resourceRestAPIUpdate(ctx context.Context, d *schema.ResourceData, meta int
395395
// Remove every binary media types. Simpler to remove and add new ones,
396396
// since there are no replacings.
397397
for _, v := range old {
398-
operations = append(operations, &apigateway.PatchOperation{
399-
Op: aws.String(apigateway.OpRemove),
400-
Path: aws.String(fmt.Sprintf("/%s/%s", prefix, escapeJSONPointer(v.(string)))),
401-
})
398+
if e, ok := v.(string); ok {
399+
operations = append(operations, &apigateway.PatchOperation{
400+
Op: aws.String(apigateway.OpRemove),
401+
Path: aws.String(fmt.Sprintf("/%s/%s", prefix, escapeJSONPointer(e))),
402+
})
403+
}
402404
}
403405

404406
// Handle additions
405407
if len(new) > 0 {
406408
for _, v := range new {
407-
operations = append(operations, &apigateway.PatchOperation{
408-
Op: aws.String(apigateway.OpAdd),
409-
Path: aws.String(fmt.Sprintf("/%s/%s", prefix, escapeJSONPointer(v.(string)))),
410-
})
409+
if e, ok := v.(string); ok {
410+
operations = append(operations, &apigateway.PatchOperation{
411+
Op: aws.String(apigateway.OpAdd),
412+
Path: aws.String(fmt.Sprintf("/%s/%s", prefix, escapeJSONPointer(e))),
413+
})
414+
}
411415
}
412416
}
413417
}
@@ -626,18 +630,22 @@ func resourceRestAPIWithBodyUpdateOperations(d *schema.ResourceData, output *api
626630
}
627631

628632
if v, ok := d.GetOk("binary_media_types"); ok && len(v.([]interface{})) > 0 {
629-
for _, elem := range aws.StringValueSlice(output.BinaryMediaTypes) {
630-
operations = append(operations, &apigateway.PatchOperation{
631-
Op: aws.String(apigateway.OpRemove),
632-
Path: aws.String("/binaryMediaTypes/" + escapeJSONPointer(elem)),
633-
})
633+
if len(output.BinaryMediaTypes) > 0 {
634+
for _, elem := range aws.StringValueSlice(output.BinaryMediaTypes) {
635+
operations = append(operations, &apigateway.PatchOperation{
636+
Op: aws.String(apigateway.OpRemove),
637+
Path: aws.String("/binaryMediaTypes/" + escapeJSONPointer(elem)),
638+
})
639+
}
634640
}
635641

636642
for _, elem := range v.([]interface{}) {
637-
operations = append(operations, &apigateway.PatchOperation{
638-
Op: aws.String(apigateway.OpAdd),
639-
Path: aws.String("/binaryMediaTypes/" + escapeJSONPointer(elem.(string))),
640-
})
643+
if el, ok := elem.(string); ok {
644+
operations = append(operations, &apigateway.PatchOperation{
645+
Op: aws.String(apigateway.OpAdd),
646+
Path: aws.String("/binaryMediaTypes/" + escapeJSONPointer(el)),
647+
})
648+
}
641649
}
642650
}
643651

0 commit comments

Comments
 (0)