From 249a523fd9396170d62ecd5c58ed7dae9dc7a444 Mon Sep 17 00:00:00 2001 From: Marco Suma Date: Tue, 9 Jul 2024 16:50:11 +0200 Subject: [PATCH] fix: Returns error if the analyzers attribute contains unknown fields. (#2394) * fix: Returns error if the analyzers attribute contains unknown fields. * adds changelog file. * Update .changelog/2394.txt Co-authored-by: Leo Antoli <430982+lantoli@users.noreply.github.com> --------- Co-authored-by: Leo Antoli <430982+lantoli@users.noreply.github.com> --- .changelog/2394.txt | 3 +++ .../searchindex/resource_search_index.go | 10 +++++--- .../searchindex/resource_search_index_test.go | 24 +++++++++++++++++-- 3 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 .changelog/2394.txt diff --git a/.changelog/2394.txt b/.changelog/2394.txt new file mode 100644 index 0000000000..6afb5599ae --- /dev/null +++ b/.changelog/2394.txt @@ -0,0 +1,3 @@ +```release-note:bug +resource/mongodbatlas_search_index: Returns error if the `analyzers` attribute contains unknown fields +``` diff --git a/internal/service/searchindex/resource_search_index.go b/internal/service/searchindex/resource_search_index.go index 72f0d0b783..da544e5b27 100644 --- a/internal/service/searchindex/resource_search_index.go +++ b/internal/service/searchindex/resource_search_index.go @@ -1,6 +1,7 @@ package searchindex import ( + "bytes" "context" "encoding/json" "errors" @@ -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 { @@ -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 diff --git a/internal/service/searchindex/resource_search_index_test.go b/internal/service/searchindex/resource_search_index_test.go index d0edb9cc84..4600a2cb0a 100644 --- a/internal/service/searchindex/resource_search_index_test.go +++ b/internal/service/searchindex/resource_search_index_test.go @@ -3,6 +3,7 @@ package searchindex_test import ( "context" "fmt" + "regexp" "testing" "github.com/hashicorp/terraform-plugin-testing/helper/resource" @@ -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"), + }, }, }) } @@ -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 = ` [ @@ -509,4 +515,18 @@ const ( "similarity": "euclidean" }] ` + + incorrectFormatAnalyzersJSON = ` + [ + { + "wrongField":[ + { + "type":"length", + "min":20, + "max":33 + } + ] + } + ] + ` )