Skip to content

Commit b351d0b

Browse files
authored
tftypes: Clarified ValueFromJSON error messaging with object attribute key issues (#214)
Reference: #211
1 parent 71dfab6 commit b351d0b

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

.changelog/214.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
tftypes: Clarified `ValueFromJSON` error messaging with object attribute key issues
3+
```

tftypes/value_json.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -453,15 +453,15 @@ func jsonUnmarshalObject(buf []byte, attrTypes map[string]Type, p *AttributePath
453453

454454
vals := map[string]Value{}
455455
for dec.More() {
456-
innerPath := p.WithElementKeyValue(NewValue(String, UnknownValue))
457456
tok, err := dec.Token()
458457
if err != nil {
459-
return Value{}, innerPath.NewErrorf("error reading token: %w", err)
458+
return Value{}, p.NewErrorf("error reading object attribute key token: %w", err)
460459
}
461460
key, ok := tok.(string)
462461
if !ok {
463-
return Value{}, innerPath.NewErrorf("object attribute key was %T, not string", tok)
462+
return Value{}, p.NewErrorf("object attribute key was %T with value %v, not string", tok, tok)
464463
}
464+
innerPath := p.WithAttributeName(key)
465465
attrType, ok := attrTypes[key]
466466
if !ok {
467467
if opts.IgnoreUndefinedAttributes {
@@ -472,7 +472,6 @@ func jsonUnmarshalObject(buf []byte, attrTypes map[string]Type, p *AttributePath
472472

473473
return Value{}, innerPath.NewErrorf("unsupported attribute %q", key)
474474
}
475-
innerPath = p.WithAttributeName(key)
476475

477476
var rawVal json.RawMessage
478477
err = dec.Decode(&rawVal)

tftypes/value_json_test.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tftypes
22

33
import (
4+
"fmt"
45
"math/big"
56
"testing"
67

@@ -10,9 +11,10 @@ import (
1011
func TestValueFromJSON(t *testing.T) {
1112
t.Parallel()
1213
type testCase struct {
13-
value Value
14-
typ Type
15-
json string
14+
value Value
15+
typ Type
16+
json string
17+
expectedError error
1618
}
1719
tests := map[string]testCase{
1820
// Primitives
@@ -211,6 +213,30 @@ func TestValueFromJSON(t *testing.T) {
211213
},
212214
json: `{}`,
213215
},
216+
"object-attribute-key-token-error": {
217+
value: Value{},
218+
typ: Object{
219+
AttributeTypes: map[string]Type{},
220+
},
221+
json: `{{}}`,
222+
expectedError: AttributePathError{
223+
Path: NewAttributePath(),
224+
err: fmt.Errorf("error reading object attribute key token: invalid character '{'"),
225+
},
226+
},
227+
"object-attribute-key-missing-error": {
228+
value: Value{},
229+
typ: Object{
230+
AttributeTypes: map[string]Type{
231+
"test": String,
232+
},
233+
},
234+
json: `{"not-test": "test-value"}`,
235+
expectedError: AttributePathError{
236+
Path: NewAttributePath().WithAttributeName("not-test"),
237+
err: fmt.Errorf("unsupported attribute \"not-test\""),
238+
},
239+
},
214240
"object-of-bool_number": {
215241
value: NewValue(Object{
216242
AttributeTypes: map[string]Type{
@@ -371,8 +397,8 @@ func TestValueFromJSON(t *testing.T) {
371397
t.Run(name, func(t *testing.T) {
372398
t.Parallel()
373399
val, err := ValueFromJSON([]byte(test.json), test.typ)
374-
if err != nil {
375-
t.Fatalf("unexpected error unmarshaling: %s", err)
400+
if diff := cmp.Diff(test.expectedError, err); diff != "" {
401+
t.Errorf("unexpected error difference: %s", diff)
376402
}
377403
if diff := cmp.Diff(test.value, val); diff != "" {
378404
t.Errorf("Unexpected results (-wanted +got): %s", diff)

0 commit comments

Comments
 (0)