Skip to content

Tests expecting json.Marshal to fail on map[interface{}]interface{} fail and panic under Go 1.27 #323

Description

@siretart

Description

When running the test suite under Go 1.27+, TestNormalizeJSON_ReturnsMarshalError in schema_validation/validate_document_test.go fails and panics with a SIGSEGV nil pointer dereference.

Root Cause

In Go 1.27+, encoding/json.Marshal supports marshaling maps with integer keys (such as map[interface{}]interface{}{1: "one"}) by converting the integer keys to strings ({"1":"one"}), rather than returning an unsupported type: map[interface {}]interface {} error as earlier Go versions did.

Because normalizeJSON(payload) now succeeds without error:

  • assert.Error(t, err) fails.
  • Line 195 unconditionally evaluates err.Error(), which dereferences a nil pointer and panics.

Stack Trace

=== RUN   TestNormalizeJSON_ReturnsMarshalError
    validate_document_test.go:193: Expected nil, but got: map[string]interface {}{"invalid":map[string]interface {}{"1":"one"}, "openapi":"3.1.0"}
    validate_document_test.go:194: An error is expected but got nil.
--- FAIL: TestNormalizeJSON_ReturnsMarshalError (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered, repanicked]
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x93fc5d]

goroutine 21 [running]:
...
github.com/pb33f/libopenapi-validator/schema_validation.TestNormalizeJSON_ReturnsMarshalError(...)
	schema_validation/validate_document_test.go:195 +0x19d

Affected Tests

The following tests in schema_validation/validate_document_test.go construct map[interface{}]interface{}{1: "one"} expecting json.Marshal / normalizeJSON to produce an error:

  • TestNormalizeJSON_ReturnsMarshalError (line 184)
  • TestValidateDocument_NormalizationErrorDoesNotValidateNil (line 198)
  • TestValidateDocument_CorruptSpecJSONBytesFallbackNormalizationError (line 228)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions