diff --git a/.changelog/32169.txt b/.changelog/32169.txt new file mode 100644 index 000000000000..2abe2d405d20 --- /dev/null +++ b/.changelog/32169.txt @@ -0,0 +1,3 @@ +```release-note:bug +resource/aws_api_gateway_rest_api: Fix crash when `binary_media_types` is `null` +``` \ No newline at end of file diff --git a/internal/service/apigateway/rest_api.go b/internal/service/apigateway/rest_api.go index d64f9b68981c..bca2efcdbf49 100644 --- a/internal/service/apigateway/rest_api.go +++ b/internal/service/apigateway/rest_api.go @@ -395,19 +395,23 @@ func resourceRestAPIUpdate(ctx context.Context, d *schema.ResourceData, meta int // Remove every binary media types. Simpler to remove and add new ones, // since there are no replacings. for _, v := range old { - operations = append(operations, &apigateway.PatchOperation{ - Op: aws.String(apigateway.OpRemove), - Path: aws.String(fmt.Sprintf("/%s/%s", prefix, escapeJSONPointer(v.(string)))), - }) + if e, ok := v.(string); ok { + operations = append(operations, &apigateway.PatchOperation{ + Op: aws.String(apigateway.OpRemove), + Path: aws.String(fmt.Sprintf("/%s/%s", prefix, escapeJSONPointer(e))), + }) + } } // Handle additions if len(new) > 0 { for _, v := range new { - operations = append(operations, &apigateway.PatchOperation{ - Op: aws.String(apigateway.OpAdd), - Path: aws.String(fmt.Sprintf("/%s/%s", prefix, escapeJSONPointer(v.(string)))), - }) + if e, ok := v.(string); ok { + operations = append(operations, &apigateway.PatchOperation{ + Op: aws.String(apigateway.OpAdd), + Path: aws.String(fmt.Sprintf("/%s/%s", prefix, escapeJSONPointer(e))), + }) + } } } } @@ -626,18 +630,22 @@ func resourceRestAPIWithBodyUpdateOperations(d *schema.ResourceData, output *api } if v, ok := d.GetOk("binary_media_types"); ok && len(v.([]interface{})) > 0 { - for _, elem := range aws.StringValueSlice(output.BinaryMediaTypes) { - operations = append(operations, &apigateway.PatchOperation{ - Op: aws.String(apigateway.OpRemove), - Path: aws.String("/binaryMediaTypes/" + escapeJSONPointer(elem)), - }) + if len(output.BinaryMediaTypes) > 0 { + for _, elem := range aws.StringValueSlice(output.BinaryMediaTypes) { + operations = append(operations, &apigateway.PatchOperation{ + Op: aws.String(apigateway.OpRemove), + Path: aws.String("/binaryMediaTypes/" + escapeJSONPointer(elem)), + }) + } } for _, elem := range v.([]interface{}) { - operations = append(operations, &apigateway.PatchOperation{ - Op: aws.String(apigateway.OpAdd), - Path: aws.String("/binaryMediaTypes/" + escapeJSONPointer(elem.(string))), - }) + if el, ok := elem.(string); ok { + operations = append(operations, &apigateway.PatchOperation{ + Op: aws.String(apigateway.OpAdd), + Path: aws.String("/binaryMediaTypes/" + escapeJSONPointer(el)), + }) + } } }