Skip to content

Commit

Permalink
Merge pull request #32169 from hashicorp/b-api_gateway_rest_api_crash
Browse files Browse the repository at this point in the history
aws_api_gateway_rest_api: fix crash on `binary_media_types` attribute
  • Loading branch information
johnsonaj authored Jun 22, 2023
2 parents 42fc4c0 + 621b1b3 commit aef1cc6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .changelog/32169.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_api_gateway_rest_api: Fix crash when `binary_media_types` is `null`
```
42 changes: 25 additions & 17 deletions internal/service/apigateway/rest_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))),
})
}
}
}
}
Expand Down Expand Up @@ -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)),
})
}
}
}

Expand Down

0 comments on commit aef1cc6

Please sign in to comment.