Skip to content

schema.Bundle() fails with NullReferenceException for ItemsKeyword #417

@amis92

Description

@amis92

Environment

  • Nuget Package: JsonSchema.Net
  • Nuget Version: 4.0.0
  • OS: Linux
  • .Net Target: .NET 7.0

Describe the bug

JsonSchemaExtensions.Bundle(EvaluationOptions) throws NullReferenceException given a schema containing ItemsKeyword.

To Reproduce

#r "nuget: JsonSchema.Net, 4.0.0"
using Json.Schema;
// items: true
var singleItemSchema = new JsonSchemaBuilder()
    .Items(JsonSchema.True)
    .Build();
singleItemSchema.Bundle(); // throws
// items: [true, true]
var multiItemSchema = new JsonSchemaBuilder()
    .Items(new[]{JsonSchema.True, JsonSchema.True})
    .Build();
multiItemSchema.Bundle(); // throws
stacktrace

for singleItemSchema:

System.NullReferenceException: Object reference not set to an instance of an object.
at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
at Json.Schema.JsonSchemaExtensions.Bundle(JsonSchema jsonSchema, EvaluationOptions options)

for multiItemSchema:

System.NullReferenceException: Object reference not set to an instance of an object.
at Json.Schema.JsonSchemaExtensions.Bundle(JsonSchema jsonSchema, EvaluationOptions options)

Expected behavior

Method returns bundled schema without exception.

Additional context

It seems (at least for the single-item case) this code is the problem:

https://github.com/gregsdennis/json-everything/blob/44d5fe8f83882d061b336cedc792bcc60563fd3e/JsonSchema/JsonSchemaExtensions.cs#L122-L125

On line 123, ItemsKeyword is ISchemaCollector, but when initialized with a single non-array value, it will return a null for ISchemaCollector.Schemas.

For multi-item case, I am not sure.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions