Skip to content

Commit 58c0974

Browse files
authored
Fix panic on empty --json flag value (#5498)
## Why Found during a full-repo review of the CLI. Passing an empty value to `--json` (for example `databricks jobs create --json=`, or `--json "$VAR"` where the shell variable is unset) crashed the CLI with an index out of range panic instead of a usable error message. ## Changes Before, an empty `--json` value panicked; now it fails with a clean validation error. `JsonFlag.Set` in `libs/flags/json_flag.go` indexed `v[0]` to check for the `@file` prefix without first checking the length. It now rejects empty input up front, and cobra surfaces it as `invalid argument "" for "--json" flag: expected inline JSON or @path/to/file, got an empty string`. ## Test plan - [x] New unit test `TestJsonFlagEmptyValue` covering the empty value case (panics without the fix, passes with it) - [x] `go test ./libs/flags` passes - [x] `./task fmt-q`, `./task lint-q`, and `./task checks` pass This pull request and its description were written by Isaac.
1 parent 2a71127 commit 58c0974

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

libs/flags/json_flag.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package flags
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"os"
78
"reflect"
@@ -23,6 +24,9 @@ func (j *JsonFlag) String() string {
2324

2425
// TODO: Command.MarkFlagFilename()
2526
func (j *JsonFlag) Set(v string) error {
27+
if v == "" {
28+
return errors.New("expected inline JSON or @path/to/file, got an empty string")
29+
}
2630
// Load request from file if it starts with '@' (like curl).
2731
if v[0] != '@' {
2832
j.raw = []byte(v)

libs/flags/json_flag_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ func TestJsonFlagEmpty(t *testing.T) {
2424
assert.Nil(t, request)
2525
}
2626

27+
func TestJsonFlagEmptyValue(t *testing.T) {
28+
var body JsonFlag
29+
30+
err := body.Set("")
31+
assert.EqualError(t, err, "expected inline JSON or @path/to/file, got an empty string")
32+
}
33+
2734
func TestJsonFlagInline(t *testing.T) {
2835
var body JsonFlag
2936

0 commit comments

Comments
 (0)