Skip to content

Commit 38be9c4

Browse files
authored
fix: Doesn't send analyzers field in search_index if absent in the definition (#2994)
* analyzers not sent to Atlas if empty * changelog * unit tests
1 parent 196a474 commit 38be9c4

File tree

4 files changed

+88
-4
lines changed

4 files changed

+88
-4
lines changed

.changelog/2994.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
resource/mongodbatlas_search_index: Don't send empty `analyzers` attribute to Atlas
3+
```

internal/service/searchindex/model_search_index.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ func unmarshalSearchIndexFields(str string) ([]map[string]any, diag.Diagnostics)
7272
return fields, nil
7373
}
7474

75-
func unmarshalSearchIndexAnalyzersFields(str string) ([]admin.AtlasSearchAnalyzer, diag.Diagnostics) {
75+
func UnmarshalSearchIndexAnalyzersFields(str string) ([]admin.AtlasSearchAnalyzer, diag.Diagnostics) {
7676
fields := []admin.AtlasSearchAnalyzer{}
7777
if str == "" {
78-
return fields, nil
78+
return nil, nil // don't send analyzers field to Atlas if empty
7979
}
8080
dec := json.NewDecoder(bytes.NewReader([]byte(str)))
8181
dec.DisallowUnknownFields()
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package searchindex_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/searchindex"
7+
"github.com/stretchr/testify/assert"
8+
"go.mongodb.org/atlas-sdk/v20241113004/admin"
9+
)
10+
11+
func TestUnmarshalSearchIndexAnalyzersFields(t *testing.T) {
12+
tc := map[string]struct {
13+
input string
14+
expected []admin.AtlasSearchAnalyzer
15+
expectedHasErrors bool
16+
}{
17+
"empty string returns nil not empty slice": {
18+
input: "",
19+
expected: nil,
20+
},
21+
"valid input": {
22+
input: `
23+
[{
24+
"name": "index_analyzer_test_name",
25+
"charFilters": [{
26+
"type": "mapping",
27+
"mappings": {"\\" : "/"}
28+
}],
29+
"tokenizer": {
30+
"type": "nGram",
31+
"minGram": 2,
32+
"maxGram": 5
33+
},
34+
"tokenFilters": [{
35+
"type": "length",
36+
"min": 20,
37+
"max": 33
38+
}]
39+
}]
40+
`,
41+
expected: []admin.AtlasSearchAnalyzer{
42+
{
43+
Name: "index_analyzer_test_name",
44+
CharFilters: &[]any{
45+
map[string]any{
46+
"type": "mapping",
47+
"mappings": map[string]any{
48+
"\\": "/",
49+
},
50+
},
51+
},
52+
Tokenizer: map[string]any{
53+
"type": "nGram",
54+
"minGram": float64(2),
55+
"maxGram": float64(5),
56+
},
57+
TokenFilters: &[]any{
58+
map[string]any{
59+
"type": "length",
60+
"min": float64(20),
61+
"max": float64(33),
62+
},
63+
},
64+
},
65+
},
66+
},
67+
"invalid input": {
68+
input: "{bad json format",
69+
expectedHasErrors: true,
70+
},
71+
}
72+
for name, tc := range tc {
73+
t.Run(name, func(t *testing.T) {
74+
actual, diags := searchindex.UnmarshalSearchIndexAnalyzersFields(tc.input)
75+
assert.Equal(t, tc.expectedHasErrors, diags.HasError())
76+
if !diags.HasError() {
77+
assert.Equal(t, tc.expected, actual)
78+
}
79+
})
80+
}
81+
}

internal/service/searchindex/resource_search_index.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.
221221
}
222222

223223
if d.HasChange("analyzers") {
224-
analyzers, err := unmarshalSearchIndexAnalyzersFields(d.Get("analyzers").(string))
224+
analyzers, err := UnmarshalSearchIndexAnalyzersFields(d.Get("analyzers").(string))
225225
if err != nil {
226226
return err
227227
}
@@ -417,7 +417,7 @@ func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.
417417
}
418418
searchIndexRequest.Definition.Fields = conversion.ToAnySlicePointer(&fields)
419419
} else {
420-
analyzers, err := unmarshalSearchIndexAnalyzersFields(d.Get("analyzers").(string))
420+
analyzers, err := UnmarshalSearchIndexAnalyzersFields(d.Get("analyzers").(string))
421421
if err != nil {
422422
return err
423423
}

0 commit comments

Comments
 (0)