Skip to content

Commit

Permalink
fix: Returns error if the analyzers attribute contains unknown fields. (
Browse files Browse the repository at this point in the history
#2394)

* fix: Returns error if the analyzers attribute contains unknown fields.

* adds changelog file.

* Update .changelog/2394.txt

Co-authored-by: Leo Antoli <[email protected]>

---------

Co-authored-by: Leo Antoli <[email protected]>
  • Loading branch information
marcosuma and lantoli authored Jul 9, 2024
1 parent 76ffb69 commit 249a523
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .changelog/2394.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/mongodbatlas_search_index: Returns error if the `analyzers` attribute contains unknown fields
```
10 changes: 7 additions & 3 deletions internal/service/searchindex/resource_search_index.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package searchindex

import (
"bytes"
"context"
"encoding/json"
"errors"
Expand Down Expand Up @@ -387,8 +388,8 @@ func flattenSearchIndexSynonyms(synonyms []admin.SearchSynonymMappingDefinition)
}

func marshalSearchIndex(fields any) (string, error) {
bytes, err := json.Marshal(fields)
return string(bytes), err
respBytes, err := json.Marshal(fields)
return string(respBytes), err
}

func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
Expand Down Expand Up @@ -566,7 +567,10 @@ func unmarshalSearchIndexAnalyzersFields(str string) ([]admin.AtlasSearchAnalyze
if str == "" {
return fields, nil
}
if err := json.Unmarshal([]byte(str), &fields); err != nil {
dec := json.NewDecoder(bytes.NewReader([]byte(str)))
dec.DisallowUnknownFields()

if err := dec.Decode(&fields); err != nil {
return nil, diag.Errorf("cannot unmarshal search index attribute `analyzers` because it has an incorrect format")
}
return fields, nil
Expand Down
24 changes: 22 additions & 2 deletions internal/service/searchindex/resource_search_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package searchindex_test
import (
"context"
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand Down Expand Up @@ -114,6 +115,10 @@ func TestAccSearchIndex_updatedToEmptyAnalyzers(t *testing.T) {
Config: configAdditional(projectID, indexName, databaseName, clusterName, ""),
Check: checkAdditionalAnalyzers(projectID, indexName, databaseName, clusterName, false),
},
{
Config: configAdditional(projectID, indexName, databaseName, clusterName, incorrectFormatAnalyzersTF),
ExpectError: regexp.MustCompile("cannot unmarshal search index attribute `analyzers` because it has an incorrect format"),
},
},
})
}
Expand Down Expand Up @@ -437,8 +442,9 @@ const (
with = true
without = false

analyzersTF = "\nanalyzers = <<-EOF\n" + analyzersJSON + "\nEOF\n"
mappingsFieldsTF = "\nmappings_fields = <<-EOF\n" + mappingsFieldsJSON + "\nEOF\n"
analyzersTF = "\nanalyzers = <<-EOF\n" + analyzersJSON + "\nEOF\n"
incorrectFormatAnalyzersTF = "\nanalyzers = <<-EOF\n" + incorrectFormatAnalyzersJSON + "\nEOF\n"
mappingsFieldsTF = "\nmappings_fields = <<-EOF\n" + mappingsFieldsJSON + "\nEOF\n"

analyzersJSON = `
[
Expand Down Expand Up @@ -509,4 +515,18 @@ const (
"similarity": "euclidean"
}]
`

incorrectFormatAnalyzersJSON = `
[
{
"wrongField":[
{
"type":"length",
"min":20,
"max":33
}
]
}
]
`
)

0 comments on commit 249a523

Please sign in to comment.