Repository navigation
fix: field id:0 incorrectly rejected as missing by NestedField.UnmarshalJSON - #761
Merged
Merged
Conversation
…halJSON Problem Commit 95df02c introduced validation in NestedField.UnmarshalJSON to catch payloads that omit the required "id" key (e.g. using "field_id" by mistake). The check used was: ``` if n.ID == 0 { return fmt.Errorf("%w: field is missing required 'id' key in JSON", ErrInvalidSchema) } ``` This conflates two distinct situations: 1. The "id" key is absent from JSON → n.ID stays at its zero value 0 (invalid, should error). 2. The "id" key is present with value 0 → n.ID is also 0 (valid, should not error). Go's encoding/json leaves fields at their zero value when a key is absent, so both cases are indistinguishable using n.ID == 0. As a result, any schema payload with a field using id:0 — which Spark sends for freshly created tables — is incorrectly rejected with ErrInvalidSchema. Example failing payload: ``` { "name": "my_table1", "schema": { "type": "struct", "schema-id": 0, "fields": [ {"id": 0, "name": "id", "required": false, "type": "long"}, {"id": 1, "name": "strings", "required": false, "type": "string"}, {"id": 2, "name": "floats", "required": false, "type": "double"} ] }, "stage-create": true, ... } ``` Fix Use *int for the id field in the unmarshaling aux struct. A nil pointer unambiguously means the key was absent from JSON; a non-nil pointer (including &0) means the key was present. The outer ID *int field shadows the embedded Alias.ID int during JSON decoding, and n.ID is then set explicitly from the dereferenced pointer. ``` aux := struct { ID *int `json:"id"` // nil = key absent, &0 = key present with value 0 Type typeIFace `json:"type"` *Alias }{Alias: (*Alias)(n)} ``` Changes types.go - NestedField.UnmarshalJSON: introduce ID *int in the aux struct; check aux.ID == nil instead of n.ID == 0; set n.ID = *aux.ID after the nil guard. types_test.go - TestNestedFieldUnmarshalZeroIDIsValid: new test asserting that {"id":0,...} unmarshals without error and produces ID == 0. Behaviour after this change | Scenario | Before | After | |----------------------------|-------------------------------|------------------| | "id" key absent from JSON | ErrInvalidSchema | ErrInvalidSchema | | "field_id" instead of "id" | ErrInvalidSchema | ErrInvalidSchema | | "id":0 present in JSON | ErrInvalidSchema (regression) | OK, ID == 0 | | "id":N (N > 0) present | OK | OK | Signed-off-by: Shubhendu Ram Tripathi <[email protected]>
twuebi
approved these changes
Feb 27, 2026
shtripat
added a commit
to shtripat/iceberg-go
that referenced
this pull request
Mar 2, 2026
Problem
When a NestedField has a nil Type — which happens when the JSON payload uses the wrong
key (e.g. "field_type" instead of "type") or omits the "type" key entirely — two failure
modes exist:
1. NestedField.UnmarshalJSON silently accepts nil types.
Go's encoding/json leaves fields at their zero value when a key is absent or unrecognised.
Since typeIFace wraps a Type interface, the zero value is nil. UnmarshalJSON already
validates id but applies no equivalent guard to type, so a payload with a missing or misspelled
type key produces a NestedField with Type == nil and returns no error.
2. typeIFace.MarshalJSON panics on nil type.
// before
func (t *typeIFace) MarshalJSON() ([]byte, error) {
if nested, ok := t.Type.(NestedType); ok {
return json.Marshal(nested)
}
return []byte(`"` + t.Type.Type() + `"`), nil // nil pointer dereference if t.Type == nil
}
When a schema containing a nil-type field is later marshalled (e.g. during CreateTable, CommitTable,
or any serialisation path), t.Type.Type() is a method call on a nil interface — an unrecovered panic.
There is no defer recover() in the json.Marshal call path, so the process crashes.
Example triggering input:
{
"type": "struct",
"fields": [
{"id": 1, "name": "col", "field_type": "long", "required": true}
]
}
Before this fix: json.Unmarshal succeeds silently, subsequent json.Marshal panics.
After this fix: json.Unmarshal returns ErrInvalidSchema immediately.
Changes
types.go
NestedField.UnmarshalJSON — add a nil-type guard after decoding, consistent with the existing id and name guards introduced in PRs apache#758 and apache#761:
if n.Type == nil {
return fmt.Errorf("%w: field %q is missing required 'type' key in JSON", ErrInvalidSchema, n.Name)
}
typeIFace.MarshalJSON — add a defensive nil guard so that a NestedField constructed programmatically with Type == nil returns an error instead of panicking:
if t.Type == nil {
return nil, fmt.Errorf("%w: field type is nil; use the \"type\" JSON key with a valid Iceberg type such as \"long\", \"string\", or \"boolean\"", ErrInvalidSchema)
}
types_test.go — three new tests:
- TestNestedFieldUnmarshalMissingType: absent "type" key → ErrInvalidSchema
- TestNestedFieldUnmarshalWrongTypeKey: "field_type" instead of "type" → ErrInvalidSchema
- TestTypeIFaceMarshalJSONNilType: programmatically constructed NestedField{Type: nil} → json.Marshal returns ErrInvalidSchema, no panic
Behaviour after this change
| Scenario | Before | After |
|-----------------------------------------------|-----------------------------------|-------------------------------|
| "type" key absent from JSON | Silent nil type, panic on marshal | ErrInvalidSchema at unmarshal |
| Wrong key ("field_type") used | Silent nil type, panic on marshal | ErrInvalidSchema at unmarshal |
| NestedField with nil type marshalled directly | Unrecovered panic | ErrInvalidSchema returned |
| Valid field with correct "type" key | OK | OK (unchanged) |
Signed-off-by: Shubhendu Ram Tripathi <[email protected]>
zeroshade
pushed a commit
that referenced
this pull request
Mar 2, 2026
### Problem
When a NestedField has a nil Type — which happens when the JSON payload
uses the wrong key (e.g. "field_type" instead of "type") or omits the
"type" key entirely — two failure modes exist:
1. NestedField.UnmarshalJSON silently accepts nil types.
Go's encoding/json leaves fields at their zero value when a key is
absent or unrecognised. Since typeIFace wraps a Type interface, the zero
value is nil. UnmarshalJSON already validates id but applies no
equivalent guard to type, so a payload with a missing or misspelled type
key produces a NestedField with Type == nil and returns no error.
2. typeIFace.MarshalJSON panics on nil type.
```
// before
func (t *typeIFace) MarshalJSON() ([]byte, error) {
if nested, ok := t.Type.(NestedType); ok {
return json.Marshal(nested)
}
return []byte(`"` + t.Type.Type() + `"`), nil // nil pointer dereference if t.Type == nil
}
```
When a schema containing a nil-type field is later marshalled (e.g.
during CreateTable, CommitTable, or any serialisation path),
t.Type.Type() is a method call on a nil interface — an unrecovered
panic. There is no defer recover() in the json.Marshal call path, so the
process crashes.
```
Example triggering input:
{
"type": "struct",
"fields": [
{"id": 1, "name": "col", "field_type": "long", "required": true}
]
}
```
Before this fix: json.Unmarshal succeeds silently, subsequent
json.Marshal panics. After this fix: json.Unmarshal returns
ErrInvalidSchema immediately.
### Changes
* types.go
NestedField.UnmarshalJSON — add a nil-type guard after decoding,
consistent with the existing id and name guards introduced in PRs #758
and #761: if n.Type == nil {
return fmt.Errorf("%w: field %q is missing required 'type' key in JSON",
ErrInvalidSchema, n.Name)
}
typeIFace.MarshalJSON — add a defensive nil guard so that a NestedField
constructed programmatically with Type == nil returns an error instead
of panicking: if t.Type == nil {
return nil, fmt.Errorf("%w: field type is nil; use the \"type\" JSON key
with a valid Iceberg type such as \"long\", \"string\", or \"boolean\"",
ErrInvalidSchema)
}
* types_test.go — three new tests:
** TestNestedFieldUnmarshalMissingType: absent "type" key →
ErrInvalidSchema
** TestNestedFieldUnmarshalWrongTypeKey: "field_type" instead of "type"
→ ErrInvalidSchema
** TestTypeIFaceMarshalJSONNilType: programmatically constructed
NestedField{Type: nil} → json.Marshal returns ErrInvalidSchema, no panic
### Behaviour after this change
| Scenario | Before | After |
|-----------------------------------------------|-----------------------------------|-------------------------------|
| "type" key absent from JSON | Silent nil type, panic on marshal |
ErrInvalidSchema at unmarshal |
| Wrong key ("field_type") used | Silent nil type, panic on marshal |
ErrInvalidSchema at unmarshal |
| NestedField with nil type marshalled directly | Unrecovered panic |
ErrInvalidSchema returned |
| Valid field with correct "type" key | OK | OK (unchanged) |
Signed-off-by: Shubhendu Ram Tripathi <[email protected]>
rockwotj
pushed a commit
to rockwotj/iceberg-go
that referenced
this pull request
Mar 18, 2026
…halJSON (apache#761) ### Problem Commit 95df02c introduced validation in NestedField.UnmarshalJSON to catch payloads that omit the required "id" key (e.g. using "field_id" by mistake). The check used was: ``` if n.ID == 0 { return fmt.Errorf("%w: field is missing required 'id' key in JSON", ErrInvalidSchema) } ``` This conflates two distinct situations: 1. The "id" key is absent from JSON → n.ID stays at its zero value 0 (invalid, should error). 2. The "id" key is present with value 0 → n.ID is also 0 (valid, should not error). Go's encoding/json leaves fields at their zero value when a key is absent, so both cases are indistinguishable using n.ID == 0. As a result, any schema payload with a field using id:0 — which Spark sends for freshly created tables — is incorrectly rejected with ErrInvalidSchema. ### Example failing payload: ``` { "name": "my_table1", "schema": { "type": "struct", "schema-id": 0, "fields": [ {"id": 0, "name": "id", "required": false, "type": "long"}, {"id": 1, "name": "strings", "required": false, "type": "string"}, {"id": 2, "name": "floats", "required": false, "type": "double"} ] }, "stage-create": true, ... } ``` ### Fix Use *int for the id field in the unmarshaling aux struct. A nil pointer unambiguously means the key was absent from JSON; a non-nil pointer (including &0) means the key was present. The outer ID *int field shadows the embedded Alias.ID int during JSON decoding, and n.ID is then set explicitly from the dereferenced pointer. ``` aux := struct { ID *int `json:"id"` // nil = key absent, &0 = key present with value 0 Type typeIFace `json:"type"` *Alias }{Alias: (*Alias)(n)} ``` ### Changes types.go - NestedField.UnmarshalJSON: introduce ID *int in the aux struct; check aux.ID == nil instead of n.ID == 0; set n.ID = *aux.ID after the nil guard. types_test.go - TestNestedFieldUnmarshalZeroIDIsValid: new test asserting that {"id":0,...} unmarshals without error and produces ID == 0. ### Behavior after this change | Scenario | Before | After | |----------------------------|-------------------------------|------------------| | "id" key absent from JSON | ErrInvalidSchema | ErrInvalidSchema | | "field_id" instead of "id" | ErrInvalidSchema | ErrInvalidSchema | | "id":0 present in JSON | ErrInvalidSchema (regression) | OK, ID == 0 | | "id":N (N > 0) present | OK | OK | Signed-off-by: Shubhendu Ram Tripathi <[email protected]>
rockwotj
pushed a commit
to rockwotj/iceberg-go
that referenced
this pull request
Mar 18, 2026
### Problem
When a NestedField has a nil Type — which happens when the JSON payload
uses the wrong key (e.g. "field_type" instead of "type") or omits the
"type" key entirely — two failure modes exist:
1. NestedField.UnmarshalJSON silently accepts nil types.
Go's encoding/json leaves fields at their zero value when a key is
absent or unrecognised. Since typeIFace wraps a Type interface, the zero
value is nil. UnmarshalJSON already validates id but applies no
equivalent guard to type, so a payload with a missing or misspelled type
key produces a NestedField with Type == nil and returns no error.
2. typeIFace.MarshalJSON panics on nil type.
```
// before
func (t *typeIFace) MarshalJSON() ([]byte, error) {
if nested, ok := t.Type.(NestedType); ok {
return json.Marshal(nested)
}
return []byte(`"` + t.Type.Type() + `"`), nil // nil pointer dereference if t.Type == nil
}
```
When a schema containing a nil-type field is later marshalled (e.g.
during CreateTable, CommitTable, or any serialisation path),
t.Type.Type() is a method call on a nil interface — an unrecovered
panic. There is no defer recover() in the json.Marshal call path, so the
process crashes.
```
Example triggering input:
{
"type": "struct",
"fields": [
{"id": 1, "name": "col", "field_type": "long", "required": true}
]
}
```
Before this fix: json.Unmarshal succeeds silently, subsequent
json.Marshal panics. After this fix: json.Unmarshal returns
ErrInvalidSchema immediately.
### Changes
* types.go
NestedField.UnmarshalJSON — add a nil-type guard after decoding,
consistent with the existing id and name guards introduced in PRs apache#758
and apache#761: if n.Type == nil {
return fmt.Errorf("%w: field %q is missing required 'type' key in JSON",
ErrInvalidSchema, n.Name)
}
typeIFace.MarshalJSON — add a defensive nil guard so that a NestedField
constructed programmatically with Type == nil returns an error instead
of panicking: if t.Type == nil {
return nil, fmt.Errorf("%w: field type is nil; use the \"type\" JSON key
with a valid Iceberg type such as \"long\", \"string\", or \"boolean\"",
ErrInvalidSchema)
}
* types_test.go — three new tests:
** TestNestedFieldUnmarshalMissingType: absent "type" key →
ErrInvalidSchema
** TestNestedFieldUnmarshalWrongTypeKey: "field_type" instead of "type"
→ ErrInvalidSchema
** TestTypeIFaceMarshalJSONNilType: programmatically constructed
NestedField{Type: nil} → json.Marshal returns ErrInvalidSchema, no panic
### Behaviour after this change
| Scenario | Before | After |
|-----------------------------------------------|-----------------------------------|-------------------------------|
| "type" key absent from JSON | Silent nil type, panic on marshal |
ErrInvalidSchema at unmarshal |
| Wrong key ("field_type") used | Silent nil type, panic on marshal |
ErrInvalidSchema at unmarshal |
| NestedField with nil type marshalled directly | Unrecovered panic |
ErrInvalidSchema returned |
| Valid field with correct "type" key | OK | OK (unchanged) |
Signed-off-by: Shubhendu Ram Tripathi <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Commit 95df02c introduced validation in NestedField.UnmarshalJSON to catch payloads that omit the required "id" key (e.g. using "field_id" by mistake). The check used was:
This conflates two distinct situations:
Go's encoding/json leaves fields at their zero value when a key is absent, so both cases are indistinguishable using n.ID == 0. As a result, any schema payload with a field using id:0 — which Spark sends for freshly created tables — is incorrectly rejected with ErrInvalidSchema.
Example failing payload:
Fix
Use *int for the id field in the unmarshaling aux struct. A nil pointer unambiguously means the key was absent from JSON; a non-nil pointer (including &0) means the key was present. The outer ID *int field shadows the embedded Alias.ID int during JSON decoding, and n.ID is then set explicitly from the dereferenced pointer.
Changes
types.go
types_test.go
Behavior after this change