-Fixed various schema generation bugs
-Renamed MemberHandler delegate to ObjectConstructor
This commit is contained in:
@@ -30,6 +30,11 @@ using System.Text;
|
||||
using NUnit.Framework;
|
||||
using Newtonsoft.Json.Schema;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json.Tests.TestObjects;
|
||||
#if !SILVERLIGHT
|
||||
using System.Data;
|
||||
#endif
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Schema
|
||||
{
|
||||
@@ -144,5 +149,53 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
// true
|
||||
}
|
||||
|
||||
private void GenerateSchemaAndSerializeFromType<T>(T value)
|
||||
{
|
||||
JsonSchemaGenerator generator = new JsonSchemaGenerator();
|
||||
generator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseAssemblyQualifiedName;
|
||||
JsonSchema typeSchema = generator.Generate(typeof (T));
|
||||
string schema = typeSchema.ToString();
|
||||
|
||||
string json = JsonConvert.SerializeObject(value, Formatting.Indented);
|
||||
JToken token = JToken.ReadFrom(new JsonTextReader(new StringReader(json)));
|
||||
|
||||
List<string> errors = new List<string>();
|
||||
|
||||
token.Validate(typeSchema, (sender, args) =>
|
||||
{
|
||||
errors.Add(args.Message);
|
||||
});
|
||||
|
||||
if (errors.Count > 0)
|
||||
Assert.Fail("Schema generated for type '{0}' is not valid." + Environment.NewLine + string.Join(Environment.NewLine, errors.ToArray()), typeof(T));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GenerateSchemaAndSerializeFromTypeTests()
|
||||
{
|
||||
GenerateSchemaAndSerializeFromType(new List<string> { "1", "Two", "III" });
|
||||
GenerateSchemaAndSerializeFromType(new List<int> { 1 });
|
||||
GenerateSchemaAndSerializeFromType(new Version("1.2.3.4"));
|
||||
GenerateSchemaAndSerializeFromType(new Store());
|
||||
GenerateSchemaAndSerializeFromType(new Person());
|
||||
GenerateSchemaAndSerializeFromType(new PersonRaw());
|
||||
GenerateSchemaAndSerializeFromType(new CircularReferenceClass() { Name = "I'm required" });
|
||||
GenerateSchemaAndSerializeFromType(new CircularReferenceWithIdClass());
|
||||
GenerateSchemaAndSerializeFromType(new ClassWithArray());
|
||||
GenerateSchemaAndSerializeFromType(new ClassWithGuid());
|
||||
#if !NET20 && !PocketPC
|
||||
GenerateSchemaAndSerializeFromType(new NullableDateTimeTestClass());
|
||||
#endif
|
||||
#if !SILVERLIGHT
|
||||
GenerateSchemaAndSerializeFromType(new DataSet());
|
||||
#endif
|
||||
GenerateSchemaAndSerializeFromType(new object());
|
||||
GenerateSchemaAndSerializeFromType(1);
|
||||
GenerateSchemaAndSerializeFromType("Hi");
|
||||
GenerateSchemaAndSerializeFromType(new DateTime(2000, 12, 29, 23, 59, 0, DateTimeKind.Utc));
|
||||
GenerateSchemaAndSerializeFromType(TimeSpan.FromTicks(1000000));
|
||||
GenerateSchemaAndSerializeFromType(DBNull.Value);
|
||||
GenerateSchemaAndSerializeFromType(new JsonPropertyWithHandlingValues());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -59,14 +59,17 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
""null""
|
||||
],
|
||||
""items"": {
|
||||
""type"": ""string""
|
||||
""type"": [
|
||||
""string"",
|
||||
""null""
|
||||
]
|
||||
}
|
||||
}
|
||||
}", json);
|
||||
|
||||
Dictionary<string, List<string>> value = new Dictionary<string, List<string>>
|
||||
{
|
||||
{"HasValue", new List<string>() { "first", "second"}},
|
||||
{"HasValue", new List<string>() { "first", "second", null }},
|
||||
{"NoValue", null}
|
||||
};
|
||||
|
||||
@@ -282,7 +285,7 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
|
||||
Assert.AreEqual(JsonSchemaType.String | JsonSchemaType.Null, schema.Properties["Name"].Type);
|
||||
Assert.AreEqual("MyExplicitId", schema.Id);
|
||||
Assert.AreEqual(JsonSchemaType.Object, schema.Properties["Child"].Type);
|
||||
Assert.AreEqual(JsonSchemaType.Object | JsonSchemaType.Null, schema.Properties["Child"].Type);
|
||||
Assert.AreEqual(schema, schema.Properties["Child"]);
|
||||
}
|
||||
|
||||
@@ -532,6 +535,90 @@ namespace Newtonsoft.Json.Tests.Schema
|
||||
// should succeed
|
||||
Assert.IsNotNull(jsonSchema);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CircularReferenceWithMixedRequires()
|
||||
{
|
||||
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
|
||||
|
||||
jsonSchemaGenerator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
||||
JsonSchema jsonSchema = jsonSchemaGenerator.Generate(typeof(CircularReferenceClass));
|
||||
string json = jsonSchema.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""id"": ""Newtonsoft.Json.Tests.TestObjects.CircularReferenceClass"",
|
||||
""optional"": true,
|
||||
""type"": [
|
||||
""object"",
|
||||
""null""
|
||||
],
|
||||
""properties"": {
|
||||
""Name"": {
|
||||
""type"": ""string""
|
||||
},
|
||||
""Child"": {
|
||||
""$ref"": ""Newtonsoft.Json.Tests.TestObjects.CircularReferenceClass""
|
||||
}
|
||||
}
|
||||
}", json);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void JsonPropertyWithHandlingValues()
|
||||
{
|
||||
JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator();
|
||||
|
||||
jsonSchemaGenerator.UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName;
|
||||
JsonSchema jsonSchema = jsonSchemaGenerator.Generate(typeof(JsonPropertyWithHandlingValues));
|
||||
string json = jsonSchema.ToString();
|
||||
|
||||
Assert.AreEqual(@"{
|
||||
""id"": ""Newtonsoft.Json.Tests.TestObjects.JsonPropertyWithHandlingValues"",
|
||||
""type"": [
|
||||
""object"",
|
||||
""null""
|
||||
],
|
||||
""properties"": {
|
||||
""DefaultValueHandlingIgnoreProperty"": {
|
||||
""optional"": true,
|
||||
""type"": [
|
||||
""string"",
|
||||
""null""
|
||||
],
|
||||
""default"": ""Default!""
|
||||
},
|
||||
""DefaultValueHandlingIncludeProperty"": {
|
||||
""type"": [
|
||||
""string"",
|
||||
""null""
|
||||
],
|
||||
""default"": ""Default!""
|
||||
},
|
||||
""NullValueHandlingIgnoreProperty"": {
|
||||
""optional"": true,
|
||||
""type"": [
|
||||
""string"",
|
||||
""null""
|
||||
]
|
||||
},
|
||||
""NullValueHandlingIncludeProperty"": {
|
||||
""type"": [
|
||||
""string"",
|
||||
""null""
|
||||
]
|
||||
},
|
||||
""ReferenceLoopHandlingErrorProperty"": {
|
||||
""$ref"": ""Newtonsoft.Json.Tests.TestObjects.JsonPropertyWithHandlingValues""
|
||||
},
|
||||
""ReferenceLoopHandlingIgnoreProperty"": {
|
||||
""$ref"": ""Newtonsoft.Json.Tests.TestObjects.JsonPropertyWithHandlingValues""
|
||||
},
|
||||
""ReferenceLoopHandlingSerializeProperty"": {
|
||||
""$ref"": ""Newtonsoft.Json.Tests.TestObjects.JsonPropertyWithHandlingValues""
|
||||
}
|
||||
}
|
||||
}", json);
|
||||
}
|
||||
}
|
||||
|
||||
public class DMDSLBase
|
||||
|
||||
@@ -2546,6 +2546,19 @@ keyword such as type of business.""
|
||||
internal DateTimeOffset _dateTimeOffsetValue;
|
||||
internal Person _personValue;
|
||||
internal Person _nullPersonValue;
|
||||
internal int? _nullableInt;
|
||||
internal bool _booleanValue;
|
||||
internal byte _byteValue;
|
||||
internal char _charValue;
|
||||
internal DateTime _dateTimeValue;
|
||||
internal decimal _decimalValue;
|
||||
internal short _shortValue;
|
||||
internal long _longValue;
|
||||
internal sbyte _sbyteValue;
|
||||
internal float _floatValue;
|
||||
internal ushort _ushortValue;
|
||||
internal uint _uintValue;
|
||||
internal ulong _ulongValue;
|
||||
|
||||
public ISerializableTestObject(string stringValue, int intValue, DateTimeOffset dateTimeOffset, Person personValue)
|
||||
{
|
||||
@@ -2553,6 +2566,7 @@ keyword such as type of business.""
|
||||
_intValue = intValue;
|
||||
_dateTimeOffsetValue = dateTimeOffset;
|
||||
_personValue = personValue;
|
||||
_dateTimeValue = new DateTime(0, DateTimeKind.Utc);
|
||||
}
|
||||
|
||||
protected ISerializableTestObject(SerializationInfo info, StreamingContext context)
|
||||
@@ -2562,6 +2576,20 @@ keyword such as type of business.""
|
||||
_dateTimeOffsetValue = (DateTimeOffset)info.GetValue("dateTimeOffsetValue", typeof(DateTimeOffset));
|
||||
_personValue = (Person)info.GetValue("personValue", typeof(Person));
|
||||
_nullPersonValue = (Person)info.GetValue("nullPersonValue", typeof(Person));
|
||||
_nullableInt = (int?)info.GetValue("nullableInt", typeof(int?));
|
||||
|
||||
_booleanValue = info.GetBoolean("booleanValue");
|
||||
_byteValue = info.GetByte("byteValue");
|
||||
_charValue = info.GetChar("charValue");
|
||||
_dateTimeValue = info.GetDateTime("dateTimeValue");
|
||||
_decimalValue = info.GetDecimal("decimalValue");
|
||||
_shortValue = info.GetInt16("shortValue");
|
||||
_longValue = info.GetInt64("longValue");
|
||||
_sbyteValue = info.GetSByte("sbyteValue");
|
||||
_floatValue = info.GetSingle("floatValue");
|
||||
_ushortValue = info.GetUInt16("ushortValue");
|
||||
_uintValue = info.GetUInt32("uintValue");
|
||||
_ulongValue = info.GetUInt64("ulongValue");
|
||||
}
|
||||
|
||||
public void GetObjectData(SerializationInfo info, StreamingContext context)
|
||||
@@ -2571,6 +2599,20 @@ keyword such as type of business.""
|
||||
info.AddValue("dateTimeOffsetValue", _dateTimeOffsetValue);
|
||||
info.AddValue("personValue", _personValue);
|
||||
info.AddValue("nullPersonValue", _nullPersonValue);
|
||||
info.AddValue("nullableInt", null);
|
||||
|
||||
info.AddValue("booleanValue", _booleanValue);
|
||||
info.AddValue("byteValue", _byteValue);
|
||||
info.AddValue("charValue", _charValue);
|
||||
info.AddValue("dateTimeValue", _dateTimeValue);
|
||||
info.AddValue("decimalValue", _decimalValue);
|
||||
info.AddValue("shortValue", _shortValue);
|
||||
info.AddValue("longValue", _longValue);
|
||||
info.AddValue("sbyteValue", _sbyteValue);
|
||||
info.AddValue("floatValue", _floatValue);
|
||||
info.AddValue("ushortValue", _ushortValue);
|
||||
info.AddValue("uintValue", _uintValue);
|
||||
info.AddValue("ulongValue", _ulongValue);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2603,7 +2645,20 @@ keyword such as type of business.""
|
||||
""BirthDate"": ""\/Date(946688461000)\/"",
|
||||
""LastModified"": ""\/Date(946688461000)\/""
|
||||
},
|
||||
""nullPersonValue"": null
|
||||
""nullPersonValue"": null,
|
||||
""nullableInt"": null,
|
||||
""booleanValue"": false,
|
||||
""byteValue"": 0,
|
||||
""charValue"": ""\u0000"",
|
||||
""dateTimeValue"": ""\/Date(-62135596800000)\/"",
|
||||
""decimalValue"": 0.0,
|
||||
""shortValue"": 0,
|
||||
""longValue"": 0,
|
||||
""sbyteValue"": 0,
|
||||
""floatValue"": 0.0,
|
||||
""ushortValue"": 0,
|
||||
""uintValue"": 0,
|
||||
""ulongValue"": 0
|
||||
}", json);
|
||||
|
||||
ISerializableTestObject o2 = JsonConvert.DeserializeObject<ISerializableTestObject>(json);
|
||||
@@ -2612,6 +2667,7 @@ keyword such as type of business.""
|
||||
Assert.AreEqual(dateTimeOffset, o2._dateTimeOffsetValue);
|
||||
Assert.AreEqual("Name!", o2._personValue.Name);
|
||||
Assert.AreEqual(null, o2._nullPersonValue);
|
||||
Assert.AreEqual(null, o2._nullableInt);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ namespace Newtonsoft.Json.Tests.TestObjects
|
||||
{
|
||||
[JsonProperty(Required = Required.Always)]
|
||||
public string Name { get; set; }
|
||||
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
|
||||
public CircularReferenceClass Child { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user