Skip to content

Commit 9d9dfb1

Browse files
committed
fix: return error when trying to import empty objects
echo '{}' | algolia objects import -F- --auto-generate... used to create an empty record with just the objectID. I think it's more reasonable to expect it to return an error instead, as it's likely a user error.
1 parent 1227971 commit 9d9dfb1

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

pkg/cmd/objects/import/import.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,12 @@ func runImportCmd(opts *ImportOptions) error {
103103
err := fmt.Errorf("failed to parse JSON object on line %d: %s", count, err)
104104
return err
105105
}
106-
// The API client doesn't support this flag since v4
107-
// The API always automatically generates objectIDs
108-
// if you pass in records without them
109-
// Implement it here to avoid breaking changes
106+
107+
if len(record) == 0 {
108+
return fmt.Errorf("empty object on line %d", count)
109+
}
110+
// The API always automatically generates objectIDs for this operation
111+
// The v3 API clients implemented this option, but not v4, so we'll implement it here
110112
if !opts.AutoObjectIDs {
111113
if _, ok := record["objectID"]; !ok {
112114
return fmt.Errorf("missing objectID on line %d", count)

pkg/cmd/objects/import/import_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ func Test_runImportCmd(t *testing.T) {
3737
cli: fmt.Sprintf("foo -F '%s'", tmpFile),
3838
wantOut: "✓ Successfully imported 1 objects to foo in",
3939
},
40+
{
41+
name: "empty object",
42+
cli: "foo -F -",
43+
stdin: `{}`,
44+
wantErr: "empty object on line 0",
45+
},
4046
{
4147
name: "missing objectID",
4248
cli: "foo -F -",

0 commit comments

Comments
 (0)