Skip to content

Commit

Permalink
fix: return error when trying to import empty objects
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kai687 committed Feb 10, 2025
1 parent 76fd5a8 commit 9d6ff33
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 6 additions & 4 deletions pkg/cmd/objects/import/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@ func runImportCmd(opts *ImportOptions) error {
err := fmt.Errorf("failed to parse JSON object on line %d: %s", count, err)
return err
}
// The API client doesn't support this flag since v4
// The API always automatically generates objectIDs
// if you pass in records without them
// Implement it here to avoid breaking changes

if len(record) == 0 {
return fmt.Errorf("empty object on line %d", count)
}
// The API always automatically generates objectIDs for this operation
// The v3 API clients implemented this option, but not v4, so we'll implement it here
if !opts.AutoObjectIDs {
if _, ok := record["objectID"]; !ok {
return fmt.Errorf("missing objectID on line %d", count)
Expand Down
6 changes: 6 additions & 0 deletions pkg/cmd/objects/import/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ func Test_runImportCmd(t *testing.T) {
cli: fmt.Sprintf("foo -F '%s'", tmpFile),
wantOut: "✓ Successfully imported 1 objects to foo in",
},
{
name: "empty object",
cli: "foo -F -",
stdin: `{}`,
wantErr: "empty object on line 0",
},
{
name: "missing objectID",
cli: "foo -F -",
Expand Down

0 comments on commit 9d6ff33

Please sign in to comment.