Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
marcosuma marked this conversation as resolved.
Show resolved Hide resolved
```
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()
Comment on lines +570 to +571
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice approach for handling this validation 👍


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"
Copy link
Member

@lantoli lantoli Jul 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think it would be good to also have an acc test or unit tests should be enough in this case?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had that question for myself. the cost is minimum since it doesn't call the API but fails before. The reason why I opted for acc test is because in the end I could re-use an existing one by just adding a further step, but unit test would have worked as well.

Let me know if you have a strong opinion on this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's fine with unit test only

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
}
]
}
]
`
)