99
1010 "github.com/santhosh-tekuri/jsonschema/v6"
1111 "github.com/stretchr/testify/require"
12+ "go.yaml.in/yaml/v3"
1213)
1314
1415func TestCombineCollectorSchema_LayoutAndValidation (t * testing.T ) {
@@ -173,6 +174,123 @@ func TestCombineCollectorSchema_DeprecatedTypeDuplicatesMainType(t *testing.T) {
173174 require .ErrorContains (t , err , `duplicate component identifier "health_check"` )
174175}
175176
177+ func TestCombineCollectorSchema_NullComponentBodyIsAccepted (t * testing.T ) {
178+ t .Parallel ()
179+
180+ schema , err := CombineCollectorSchema (CollectorSchemaParts {
181+ Receivers : []CollectorComponentSchema {
182+ {
183+ Type : "otlp" ,
184+ Schema : & JSONSchema {
185+ Type : "object" ,
186+ Properties : map [string ]* JSONSchema {
187+ "endpoint" : {Type : "string" },
188+ },
189+ },
190+ },
191+ },
192+ })
193+ require .NoError (t , err )
194+
195+ compiled := compileSchema (t , schema )
196+
197+ require .NoError (t , compiled .Validate (unmarshalJSON (t , `{
198+ "receivers": {"otlp": null}
199+ }` )))
200+
201+ require .NoError (t , compiled .Validate (unmarshalYAML (t , "receivers:\n otlp:\n " )))
202+
203+ require .NoError (t , compiled .Validate (unmarshalJSON (t , `{
204+ "receivers": {"otlp/secondary": null}
205+ }` )))
206+
207+ require .NoError (t , compiled .Validate (unmarshalJSON (t , `{
208+ "receivers": {"otlp": {"endpoint": "localhost:4317"}}
209+ }` )))
210+ }
211+
212+ func TestCombineCollectorSchema_NullBranchDoesNotSwallowTypeErrors (t * testing.T ) {
213+ t .Parallel ()
214+
215+ schema , err := CombineCollectorSchema (CollectorSchemaParts {
216+ Receivers : []CollectorComponentSchema {
217+ {
218+ Type : "otlp" ,
219+ Schema : & JSONSchema {
220+ Type : "object" ,
221+ Properties : map [string ]* JSONSchema {
222+ "endpoint" : {Type : "string" },
223+ },
224+ Required : []string {"endpoint" },
225+ },
226+ },
227+ },
228+ })
229+ require .NoError (t , err )
230+
231+ compiled := compileSchema (t , schema )
232+
233+ for name , config := range map [string ]string {
234+ "scalar body" : `{"receivers": {"otlp": 5}}` ,
235+ "string body" : `{"receivers": {"otlp": "enabled"}}` ,
236+ "array body" : `{"receivers": {"otlp": []}}` ,
237+ "wrong property type" : `{"receivers": {"otlp": {"endpoint": 4317}}}` ,
238+ "missing required" : `{"receivers": {"otlp": {}}}` ,
239+ } {
240+ t .Run (name , func (t * testing.T ) {
241+ require .Error (t , compiled .Validate (unmarshalJSON (t , config )))
242+ })
243+ }
244+ }
245+
246+ func TestCombineCollectorSchema_NilSchemaIsNotWrapped (t * testing.T ) {
247+ t .Parallel ()
248+
249+ schema , err := CombineCollectorSchema (CollectorSchemaParts {
250+ Exporters : []CollectorComponentSchema {
251+ {Type : "debug" },
252+ },
253+ })
254+ require .NoError (t , err )
255+
256+ debug := schema .Properties [string (CollectorSectionExporters )].PatternProperties [collectorComponentPattern ("debug" )]
257+ require .NotNil (t , debug )
258+ require .Empty (t , debug .AnyOf )
259+
260+ data , err := debug .MarshalJSON ()
261+ require .NoError (t , err )
262+ require .JSONEq (t , `true` , string (data ))
263+
264+ compiled := compileSchema (t , schema )
265+ require .NoError (t , compiled .Validate (unmarshalJSON (t , `{"exporters": {"debug": null}}` )))
266+ require .NoError (t , compiled .Validate (unmarshalJSON (t , `{"exporters": {"debug": {"verbosity": "detailed"}}}` )))
267+ }
268+
269+ func TestCombineCollectorSchema_NilSchemaDeprecatedKeepsMarker (t * testing.T ) {
270+ t .Parallel ()
271+
272+ schema , err := CombineCollectorSchema (CollectorSchemaParts {
273+ Extensions : []CollectorComponentSchema {
274+ {Type : "health_check" , DeprecatedType : "healthcheck" },
275+ },
276+ })
277+ require .NoError (t , err )
278+
279+ extensions := schema .Properties [string (CollectorSectionExtensions )]
280+ deprecatedSchema := extensions .PatternProperties [collectorComponentPattern ("healthcheck" )]
281+ require .NotNil (t , deprecatedSchema )
282+ require .True (t , deprecatedSchema .Deprecated )
283+ require .Empty (t , deprecatedSchema .AnyOf )
284+
285+ data , err := deprecatedSchema .MarshalJSON ()
286+ require .NoError (t , err )
287+ require .JSONEq (t , `{"deprecated": true}` , string (data ))
288+
289+ compiled := compileSchema (t , schema )
290+ require .NoError (t , compiled .Validate (unmarshalJSON (t , `{"extensions": {"healthcheck": null}}` )))
291+ require .NoError (t , compiled .Validate (unmarshalJSON (t , `{"extensions": {"healthcheck/1": {"endpoint": "x"}}}` )))
292+ }
293+
176294func compileSchema (t * testing.T , schema * JSONSchema ) * jsonschema.Schema {
177295 t .Helper ()
178296
@@ -198,3 +316,12 @@ func unmarshalJSON(t *testing.T, data string) any {
198316
199317 return value
200318}
319+
320+ func unmarshalYAML (t * testing.T , data string ) any {
321+ t .Helper ()
322+
323+ var value any
324+ require .NoError (t , yaml .Unmarshal ([]byte (data ), & value ))
325+
326+ return value
327+ }
0 commit comments