Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in serialization of dependencies in JSchema #187

Closed
eivindjk opened this issue May 10, 2019 · 5 comments · Fixed by #188
Closed

Bug in serialization of dependencies in JSchema #187

eivindjk opened this issue May 10, 2019 · 5 comments · Fixed by #188

Comments

@eivindjk
Copy link

There might be a bug with serialization of the Dependency property of the JSchema object in Newtonsoft.Json.Schema.

When serialising a JSchema object with dependencis included, the serializer does not include the JSchemas addet to the dependency property. It is possible that i have done something wrong and has to use som serializer settings, but i suspect this might be a bug.

Below are two code examples with corresponding results. In both cases the objects can be indentified with the corerect values in the debugger in visual studio 2019.

Code example 1

var schema = new JSchema()
{
    Type = JSchemaType.Object,
    SchemaVersion = new Uri("http://json-schema.org/draft-07/schema#")
};

schema.Properties.Add("ShowField", new JSchema() { Type = JSchemaType.Boolean });

var oneOf1 = new JSchema();
oneOf1.Properties.Add("ShowField", new JSchema() {Enum = { false }});

var oneOf2 = new JSchema();
oneOf2.Properties.Add("ShowField", new JSchema() { Enum = { true } });
oneOf2.Properties.Add("ExtraField", new JSchema() { Type = JSchemaType.String });

var oneOf = new JSchema();
oneOf.OneOf.Add(oneOf1);
oneOf.OneOf.Add(oneOf2);

schema.Dependencies.Add("ShowField", oneOf);

Console.WriteLine(schema.ToString());

Actual destination json

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "ShowField": {
      "type": "boolean"
    }
  }
}

Expected destination json

I expected the result to be like this:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "ShowField": {
      "type": "boolean"
    }
  },
  "dependencies": {
    "ShowField": {
      "oneOf": [
        {
          "properties": {
            "ShowField": {
              "enum": [false]
            }
          }
	    },
        {
          "properties": {
            "ShowField": {
              "enum": [true]
            },
            "ExtraField": {
              "type": "string"
            }
          }
        }
      ]
    }
  }
}

Code example 2

In this code I deserialized a JSchema and verifide that the values of the dependecy object was present by using the debugger. I expected the same output as input when deserializing and the serializing. This was not the case

var schemaString = "{\"$schema\": \"http://json-schema.org/draft-07/schema#\",\"type\": \"object\",\"properties\": {\"ShowField\": {\"type\": \"boolean\"}},\"dependencies\": {\"ShowField\": {\"oneOf\": [{\"properties\": {\"ShowField\": {\"enum\": [false]}}},{\"properties\": {\"ShowField\": {\"enum\": [true]},\"ExtraField\": {\"type\": \"string\"}}}]}}}";

var schema = JsonConvert.DeserializeObject<JSchema>(schemaString);
Console.WriteLine(schema.ToString());

Actual destination json

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "ShowField": {
      "type": "boolean"
    }
  }
}

Expected destination json

I expected the result to be like this:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "ShowField": {
      "type": "boolean"
    }
  },
  "dependencies": {
    "ShowField": {
      "oneOf": [
        {
          "properties": {
            "ShowField": {
              "enum": [false]
            }
          }
	    },
        {
          "properties": {
            "ShowField": {
              "enum": [true]
            },
            "ExtraField": {
              "type": "string"
            }
          }
        }
      ]
    }
  }
}

Steps to reproduce

I coded both examples as a .net core console application

@JamesNK
Copy link
Owner

JamesNK commented May 11, 2019

Thanks for the good repos. I've fix your issue.

@eivindjk
Copy link
Author

eivindjk commented May 13, 2019

Thank you :) @JamesNK When will this be released? I would like implement this in my project soon :)

@SimBri
Copy link

SimBri commented May 23, 2019

@eivindjk : Thanks for reporting the issue. I ran into this too and I just downloaded the source, made a buld i VS and referenced the dll untill I can get the new version in a Nuget release.

@HoffernJr
Copy link

I have the same issue as @eivindjk . I have dowloaded the source and build it in VS. The solution works. Do you have a release date @JamesNK ? I want to reference the fix from nuget instead of using the dll when we deploy our application.

@JamesNK
Copy link
Owner

JamesNK commented Sep 8, 2019

A preview package is now on NuGet: https://www.nuget.org/packages/Newtonsoft.Json.Schema/3.0.12-beta1

This contains the dependency fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants