(t *testing.T)
| 37 | } |
| 38 | |
| 39 | func TestOverridesExtension_MarshalJSON(t *testing.T) { |
| 40 | t.Run("overrides", func(t *testing.T) { |
| 41 | resetRegistryForTesting(t) |
| 42 | get := RegisterExtension(&testExtension{}) |
| 43 | |
| 44 | fieldB := 42 |
| 45 | o := Overrides{} |
| 46 | o.Ingestion.MaxLocalTracesPerUser = 1000 |
| 47 | o.Extensions = map[string]any{ |
| 48 | "test_extension": &testExtension{FieldA: "custom", FieldB: &fieldB}, |
| 49 | } |
| 50 | |
| 51 | b, err := json.Marshal(&o) |
| 52 | require.NoError(t, err) |
| 53 | |
| 54 | var o2 Overrides |
| 55 | require.NoError(t, json.Unmarshal(b, &o2)) |
| 56 | |
| 57 | assert.Equal(t, 1000, o2.Ingestion.MaxLocalTracesPerUser) |
| 58 | ext := get(&o2) |
| 59 | require.NotNil(t, ext) |
| 60 | assert.Equal(t, "custom", ext.FieldA) |
| 61 | assert.Equal(t, 42, *ext.FieldB) |
| 62 | }) |
| 63 | |
| 64 | t.Run("legacy overrides", func(t *testing.T) { |
| 65 | resetRegistryForTesting(t) |
| 66 | RegisterExtension(&testExtension{}) |
| 67 | |
| 68 | fieldB := 7 |
| 69 | o := Overrides{} |
| 70 | o.Ingestion.MaxLocalTracesPerUser = 500 |
| 71 | o.Extensions = map[string]any{ |
| 72 | "test_extension": &testExtension{FieldA: "flat_val", FieldB: &fieldB}, |
| 73 | } |
| 74 | l := o.toLegacy() |
| 75 | |
| 76 | // After toLegacy, Extensions holds the typed instance; flat keys appear only when marshaled. |
| 77 | extInLegacy, ok := l.Extensions["test_extension"].(*testExtension) |
| 78 | require.True(t, ok, "expected typed test_extension in LegacyOverrides.Extensions") |
| 79 | assert.Equal(t, "flat_val", extInLegacy.FieldA) |
| 80 | |
| 81 | b, err := json.Marshal(&l) |
| 82 | require.NoError(t, err) |
| 83 | |
| 84 | // The nested extension key must be replaced by its flat keys in JSON. |
| 85 | var m map[string]any |
| 86 | require.NoError(t, json.Unmarshal(b, &m)) |
| 87 | assert.Equal(t, "flat_val", m["test_extension_field_a"], "flat key must appear in JSON") |
| 88 | assert.Nil(t, m["test_extension"], "nested key must not appear in JSON") |
| 89 | |
| 90 | // Unmarshal the JSON back to LegacyOverrides; processLegacyExtensions converts flat keys |
| 91 | // to a typed instance under the nested key. |
| 92 | var l2 LegacyOverrides |
| 93 | require.NoError(t, json.Unmarshal(b, &l2)) |
| 94 | ext2, ok2 := l2.Extensions["test_extension"].(*testExtension) |
| 95 | require.True(t, ok2, "expected typed test_extension in l2.Extensions after unmarshal") |
| 96 | assert.Equal(t, "flat_val", ext2.FieldA) |
nothing calls this directly
no test coverage detected