| 1 |
From: Reinhard Tartler <[email protected]> |
| 2 |
Date: Wed, 23 Sep 2026 20:02:07 -0400 |
| 3 |
Subject: Fix JSON marshal error tests for newer Go |
| 4 |
|
| 5 |
Go 1.24+ standard library encoding/json now supports marshaling maps |
| 6 |
with integer keys (such as map[interface{}]interface{}{1: "one"}), |
| 7 |
coercing them to strings instead of failing with an unsupported type error. |
| 8 |
|
| 9 |
Use an unmarshalable channel type (make(chan int)) in test payloads to |
| 10 |
reliably trigger json.Marshal errors across all Go versions. |
| 11 |
|
| 12 |
Bug-Debian: https://bugs.debian.org/1147128 |
| 13 |
Forwarded: https://github.com/pb33f/libopenapi-validator/issues/323 |
| 14 |
Last-Update: 2026-09-23 |
| 15 |
--- |
| 16 |
schema_validation/validate_document_test.go | 19 +++++++------------ |
| 17 |
1 file changed, 7 insertions(+), 12 deletions(-) |
| 18 |
|
| 19 |
diff --git a/schema_validation/validate_document_test.go b/schema_validation/validate_document_test.go |
| 20 |
index 434567a..c810822 100644 |
| 21 |
--- a/schema_validation/validate_document_test.go |
| 22 |
+++ b/schema_validation/validate_document_test.go |
| 23 |
@@ -183,16 +183,15 @@ x-values: |
| 24 |
func TestNormalizeJSON_ReturnsMarshalError(t *testing.T) { |
| 25 |
payload := map[string]interface{}{ |
| 26 |
"openapi": "3.1.0", |
| 27 |
- "invalid": map[interface{}]interface{}{ |
| 28 |
- 1: "one", |
| 29 |
- }, |
| 30 |
+ "invalid": make(chan int), |
| 31 |
} |
| 32 |
|
| 33 |
normalized, err := normalizeJSON(payload) |
| 34 |
|
| 35 |
assert.Nil(t, normalized) |
| 36 |
- assert.Error(t, err) |
| 37 |
- assert.Contains(t, err.Error(), "unsupported type: map[interface {}]interface {}") |
| 38 |
+ if assert.Error(t, err) { |
| 39 |
+ assert.Contains(t, err.Error(), "unsupported type: chan int") |
| 40 |
+ } |
| 41 |
} |
| 42 |
|
| 43 |
func TestValidateDocument_NormalizationErrorDoesNotValidateNil(t *testing.T) { |
| 44 |
@@ -205,9 +204,7 @@ paths: {}` |
| 45 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 46 |
badSpecJSON := map[string]interface{}{ |
| 47 |
"openapi": "3.1.0", |
| 48 |
- "invalid": map[interface{}]interface{}{ |
| 49 |
- 1: "one", |
| 50 |
- }, |
| 51 |
+ "invalid": make(chan int), |
| 52 |
} |
| 53 |
info := doc.GetSpecInfo() |
| 54 |
info.GetSpecJSON() |
| 55 |
@@ -220,7 +217,7 @@ paths: {}` |
| 56 |
assert.Len(t, errors, 1) |
| 57 |
assert.Equal(t, "OpenAPI document validation failed", errors[0].Message) |
| 58 |
assert.Contains(t, errors[0].Reason, "cannot be converted to JSON") |
| 59 |
- assert.Contains(t, errors[0].Reason, "unsupported type: map[interface {}]interface {}") |
| 60 |
+ assert.Contains(t, errors[0].Reason, "unsupported type: chan int") |
| 61 |
assert.NotContains(t, errors[0].Reason, "got null, want object") |
| 62 |
assert.Empty(t, errors[0].SchemaValidationErrors) |
| 63 |
} |
| 64 |
@@ -235,9 +232,7 @@ paths: {}` |
| 65 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 66 |
badSpecJSON := map[string]interface{}{ |
| 67 |
"openapi": "3.1.0", |
| 68 |
- "invalid": map[interface{}]interface{}{ |
| 69 |
- 1: "one", |
| 70 |
- }, |
| 71 |
+ "invalid": make(chan int), |
| 72 |
} |
| 73 |
corrupt := []byte(`{not valid json!!!}`) |
| 74 |
info := doc.GetSpecInfo() |