Search code examples
c#jsonschemanjsonschema

NJsonSchema: JSON schemas with nested arrays generate incorrect C# types


I would like to use NJsonSchema to generate C# classes. The problem is that for nested arrays, the type ends up looking like this:

System.Collections.ObjectModel.ObservableCollection<System.Tuple<System.Linq.Enumerable+WhereSelectEnumerableIterator2[NJsonSchema.JsonSchema4,System.String]>>

My test code for generating the classes looks like this:

var schemaAsync = NJsonSchema.JsonSchema4.FromFileAsync(
    @"<myPath>/MyFile.json");

var schema = schemaAsync.Result;

var generator = new NJsonSchema.CodeGeneration.CSharp.CSharpGenerator(schema);
var file = generator.GenerateFile();

System.IO.File.WriteAllText(
    @"<myPath>/SomeClass.cs",
    file);

I have two JSON schema files, there's the definition file (Def1.json):

{
    "$schema": "http://json-schema.org/draft-04/schema#",

    "definitions": {
        "InnerList": {
            "description": "Only ever has 2 items",
            "type": "array",
            "items": [ 
                {
                    "type": "integer",
                    "minimum": 0,
                    "maximum": 100,
                    "default": 0
                },
                {
                    "type": "integer",
                    "minimum": 0,
                    "maximum": 100,
                    "default": 0
                }
           ],
          "additionalItems": false
        },
        "OuterList": {
            "type": "array",
            "items": {
                "$ref": "#/definitions/InnerList"
            }
        },
    }
}

And a separate schema that makes use of the definition file:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "MySchema",
    "type": "object",
    "required": [ "OuterList" ],
    "properties": {
        "OuterList": { "$ref": "Def1.json#/definitions/OuterList" }
    },
    "additionalProperties": false
}

Am I using the library incorrectly?


Solution

  • This problem has been fixed in the latest version.