Fix StringBool to handle numeric JSON values
Some Kanboard versions return is_active and similar fields as numeric 0/1 instead of string "0"/"1". Update UnmarshalJSON to try string, then number, then bool parsing.
This commit is contained in:
parent
8aa611d3d7
commit
59252a3f63
3 changed files with 55 additions and 9 deletions
|
|
@ -25,6 +25,9 @@ func TestStringBool_UnmarshalJSON(t *testing.T) {
|
|||
{"string 0", `"0"`, false},
|
||||
{"string true", `"true"`, true},
|
||||
{"string false", `"false"`, false},
|
||||
{"number 1", `1`, true},
|
||||
{"number 0", `0`, false},
|
||||
{"number non-zero", `42`, true},
|
||||
{"bool true", `true`, true},
|
||||
{"bool false", `false`, false},
|
||||
}
|
||||
|
|
@ -133,6 +136,40 @@ func TestProject_UnmarshalJSON(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestProject_UnmarshalJSON_NumericBool(t *testing.T) {
|
||||
// Some Kanboard versions return numeric booleans instead of strings
|
||||
jsonData := `{
|
||||
"id": 1,
|
||||
"name": "Test Project",
|
||||
"description": "A test project",
|
||||
"is_active": 1,
|
||||
"token": "abc123",
|
||||
"last_modified": 1609459200,
|
||||
"is_public": 0,
|
||||
"is_private": 1,
|
||||
"owner_id": 42,
|
||||
"priority_default": 2
|
||||
}`
|
||||
|
||||
var project Project
|
||||
if err := json.Unmarshal([]byte(jsonData), &project); err != nil {
|
||||
t.Fatalf("unmarshal error: %v", err)
|
||||
}
|
||||
|
||||
if int(project.ID) != 1 {
|
||||
t.Errorf("expected ID=1, got %d", project.ID)
|
||||
}
|
||||
if !bool(project.IsActive) {
|
||||
t.Error("expected IsActive=true")
|
||||
}
|
||||
if bool(project.IsPublic) {
|
||||
t.Error("expected IsPublic=false")
|
||||
}
|
||||
if !bool(project.IsPrivate) {
|
||||
t.Error("expected IsPrivate=true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTask_UnmarshalJSON(t *testing.T) {
|
||||
jsonData := `{
|
||||
"id": "42",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue