예제 #1
0
        public void Generate_DefaultValueAttributeTestClass()
        {
            JSchemaGenerator generator = new JSchemaGenerator();
            JSchema          schema    = generator.Generate(typeof(DefaultValueAttributeTestClass));

            string json = schema.ToString();

            Console.WriteLine(json);

            StringAssert.AreEqual(@"{
  ""type"": ""object"",
  ""additionalProperties"": false,
  ""properties"": {
    ""TestField1"": {
      ""type"": ""integer"",
      ""default"": 21
    },
    ""TestProperty1"": {
      ""type"": [
        ""string"",
        ""null""
      ],
      ""default"": ""TestProperty1Value""
    }
  },
  ""required"": [
    ""TestField1"",
    ""TestProperty1""
  ]
}", json);
        }
예제 #2
0
        public void GenerateSchemaWithStringEnum()
        {
            JSchemaGenerator generator = new JSchemaGenerator();

            generator.GenerationProviders.Add(new StringEnumGenerationProvider
            {
                CamelCaseText = true
            });
            JSchema schema = generator.Generate(typeof(Y));

            string json = schema.ToString();

            StringAssert.AreEqual(@"{
  ""type"": ""object"",
  ""properties"": {
    ""y"": {
      ""type"": ""string"",
      ""enum"": [
        ""no"",
        ""asc"",
        ""desc""
      ]
    }
  },
  ""required"": [
    ""y""
  ]
}", json);
        }
예제 #3
0
        public void Generate_UserNullable()
        {
            JSchemaGenerator generator = new JSchemaGenerator();
            JSchema          schema    = generator.Generate(typeof(UserNullable));

            string json = schema.ToString();

            StringAssert.AreEqual(@"{
  ""type"": ""object"",
  ""properties"": {
    ""Id"": {
      ""type"": ""string""
    },
    ""FName"": {
      ""type"": [
        ""string"",
        ""null""
      ]
    },
    ""LName"": {
      ""type"": [
        ""string"",
        ""null""
      ]
    },
    ""RoleId"": {
      ""type"": ""integer""
    },
    ""NullableRoleId"": {
      ""type"": [
        ""integer"",
        ""null""
      ]
    },
    ""NullRoleId"": {
      ""type"": [
        ""integer"",
        ""null""
      ]
    },
    ""Active"": {
      ""type"": [
        ""boolean"",
        ""null""
      ]
    }
  },
  ""required"": [
    ""Id"",
    ""FName"",
    ""LName"",
    ""RoleId"",
    ""NullableRoleId"",
    ""NullRoleId"",
    ""Active""
  ]
}", json);
        }
예제 #4
0
        public void MixedRequired()
        {
            JSchemaGenerator generator = new JSchemaGenerator();

            generator.UndefinedSchemaIdHandling = JSchemaUndefinedIdHandling.None;

            JSchema schema = generator.Generate(typeof(CircularReferenceWithIdClass));

            string json = schema.ToString();

            StringAssert.AreEqual(@"{
  ""id"": ""MyExplicitId"",
  ""type"": ""object"",
  ""properties"": {
    ""Name"": {
      ""type"": [
        ""string"",
        ""null""
      ]
    },
    ""Child"": {
      ""id"": ""MyExplicitId-1"",
      ""type"": [
        ""object"",
        ""null""
      ],
      ""properties"": {
        ""Name"": {
          ""type"": [
            ""string"",
            ""null""
          ]
        },
        ""Child"": {
          ""$ref"": ""#""
        }
      },
      ""required"": [
        ""Name"",
        ""Child""
      ]
    }
  },
  ""required"": [
    ""Name"",
    ""Child""
  ]
}", json);

            CircularReferenceWithIdClass c = new CircularReferenceWithIdClass();
            JToken t = JToken.Parse(JsonConvert.SerializeObject(c, Formatting.Indented));

            Assert.IsTrue(t.IsValid(schema));
        }
예제 #5
0
        public void GenerateSchemaCamelCase()
        {
            JSchemaGenerator generator = new JSchemaGenerator();

            generator.SchemaIdGenerationHandling = SchemaIdGenerationHandling.TypeName;
            generator.ContractResolver           = new CamelCasePropertyNamesContractResolver()
            {
#if !(NETFX_CORE || PORTABLE || ASPNETCORE50 || PORTABLE40)
                IgnoreSerializableAttribute = true
#endif
            };

            JSchema schema = generator.Generate(typeof(Version), true);

            string json = schema.ToString();

            StringAssert.AreEqual(@"{
  ""id"": ""Version"",
  ""type"": [
    ""object"",
    ""null""
  ],
  ""additionalProperties"": false,
  ""properties"": {
    ""major"": {
      ""type"": ""integer""
    },
    ""minor"": {
      ""type"": ""integer""
    },
    ""build"": {
      ""type"": ""integer""
    },
    ""revision"": {
      ""type"": ""integer""
    },
    ""majorRevision"": {
      ""type"": ""integer""
    },
    ""minorRevision"": {
      ""type"": ""integer""
    }
  },
  ""required"": [
    ""major"",
    ""minor"",
    ""build"",
    ""revision"",
    ""majorRevision"",
    ""minorRevision""
  ]
}", json);
        }
예제 #6
0
        public void NestedCircularReference()
        {
            JSchemaGenerator generator = new JSchemaGenerator();

            JSchema schema = generator.Generate(typeof(ParentCircularReferenceClass));
            string  json   = schema.ToString();

            JSchema loadedSchema = JSchema.Parse(json);
            JSchema circularReferenceClassSchema = (JSchema)loadedSchema.ExtensionData["definitions"]["CircularReferenceClass"];

            Assert.IsNotNull(circularReferenceClassSchema);
            Assert.AreEqual(circularReferenceClassSchema, circularReferenceClassSchema.Properties["Child"]);

            StringAssert.AreEqual(@"{
  ""definitions"": {
    ""CircularReferenceClass"": {
      ""type"": [
        ""object"",
        ""null""
      ],
      ""properties"": {
        ""Name"": {
          ""type"": ""string""
        },
        ""Child"": {
          ""$ref"": ""#/definitions/CircularReferenceClass""
        }
      },
      ""required"": [
        ""Name""
      ]
    }
  },
  ""type"": ""object"",
  ""properties"": {
    ""Name"": {
      ""type"": [
        ""string"",
        ""null""
      ]
    },
    ""Nested"": {
      ""$ref"": ""#/definitions/CircularReferenceClass""
    }
  },
  ""required"": [
    ""Name"",
    ""Nested""
  ]
}", json);
        }
예제 #7
0
        public void CircularReferenceWithMixedRequires_WithId()
        {
            JSchemaGenerator generator = new JSchemaGenerator();

            generator.SchemaIdGenerationHandling = SchemaIdGenerationHandling.TypeName;

            JSchema schema = generator.Generate(typeof(CircularReferenceClass), false);
            string  json   = schema.ToString();

            StringAssert.AreEqual(@"{
  ""id"": ""CircularReferenceClass"",
  ""definitions"": {
    ""CircularReferenceClass-1"": {
      ""id"": ""CircularReferenceClass-1"",
      ""type"": [
        ""object"",
        ""null""
      ],
      ""properties"": {
        ""Name"": {
          ""type"": ""string""
        },
        ""Child"": {
          ""$ref"": ""CircularReferenceClass-1""
        }
      },
      ""required"": [
        ""Name""
      ]
    }
  },
  ""type"": ""object"",
  ""properties"": {
    ""Name"": {
      ""type"": ""string""
    },
    ""Child"": {
      ""$ref"": ""CircularReferenceClass-1""
    }
  },
  ""required"": [
    ""Name""
  ]
}", json);
        }
예제 #8
0
        public void CircularReferenceWithMixedRequires()
        {
            JSchemaGenerator generator = new JSchemaGenerator();

            generator.DefaultRequired = Required.AllowNull;

            JSchema schema = generator.Generate(typeof(CircularReferenceClass), false);
            string  json   = schema.ToString();

            StringAssert.AreEqual(@"{
  ""definitions"": {
    ""CircularReferenceClass"": {
      ""type"": [
        ""object"",
        ""null""
      ],
      ""properties"": {
        ""Name"": {
          ""type"": ""string""
        },
        ""Child"": {
          ""$ref"": ""#/definitions/CircularReferenceClass""
        }
      },
      ""required"": [
        ""Name""
      ]
    }
  },
  ""type"": ""object"",
  ""properties"": {
    ""Name"": {
      ""type"": ""string""
    },
    ""Child"": {
      ""$ref"": ""#/definitions/CircularReferenceClass""
    }
  },
  ""required"": [
    ""Name""
  ]
}", json);
        }
예제 #9
0
        public void GenerateForNullableInt32()
        {
            JSchemaGenerator generator = new JSchemaGenerator();

            JSchema schema = generator.Generate(typeof(NullableInt32TestClass));
            string  json   = schema.ToString();

            StringAssert.AreEqual(@"{
  ""type"": ""object"",
  ""properties"": {
    ""Value"": {
      ""type"": [
        ""integer"",
        ""null""
      ]
    }
  },
  ""required"": [
    ""Value""
  ]
}", json);
        }
예제 #10
0
        public void Generate_GenericDictionary()
        {
            JSchemaGenerator generator = new JSchemaGenerator();
            JSchema          schema    = generator.Generate(typeof(Dictionary <string, List <string> >));

            string json = schema.ToString();

            StringAssert.AreEqual(@"{
  ""type"": ""object"",
  ""additionalProperties"": {
    ""type"": [
      ""array"",
      ""null""
    ],
    ""items"": {
      ""type"": [
        ""string"",
        ""null""
      ]
    }
  }
}", json);

            Dictionary <string, List <string> > value = new Dictionary <string, List <string> >
            {
                { "HasValue", new List <string>()
                  {
                      "first", "second", null
                  } },
                { "NoValue", null }
            };

            string  valueJson = JsonConvert.SerializeObject(value, Formatting.Indented);
            JObject o         = JObject.Parse(valueJson);

            Assert.IsTrue(o.IsValid(schema));
        }
예제 #11
0
        public void GenerateSchemaWithNegativeEnum()
        {
            JSchemaGenerator generator = new JSchemaGenerator();
            JSchema          schema    = generator.Generate(typeof(X));

            string json = schema.ToString();

            StringAssert.AreEqual(@"{
  ""type"": ""object"",
  ""properties"": {
    ""x"": {
      ""type"": ""integer"",
      ""enum"": [
        0,
        1,
        -1
      ]
    }
  },
  ""required"": [
    ""x""
  ]
}", json);
        }
예제 #12
0
        public void GenerateSchemaForDirectoryInfo()
        {
            JSchemaGenerator generator = new JSchemaGenerator();

            generator.UndefinedSchemaIdHandling = JSchemaUndefinedIdHandling.UseTypeName;
            generator.ContractResolver          = new CustomDirectoryInfoMapper
            {
#if !(NETFX_CORE || PORTABLE || PORTABLE40 || ASPNETCORE50)
                IgnoreSerializableAttribute = true
#endif
            };

            JSchema schema = generator.Generate(typeof(DirectoryInfo), true);

            string json = schema.ToString();

            Console.WriteLine(json);

            StringAssert.AreEqual(@"{
  ""id"": ""System.IO.DirectoryInfo"",
  ""type"": [
    ""object"",
    ""null""
  ],
  ""additionalProperties"": false,
  ""properties"": {
    ""Name"": {
      ""type"": [
        ""string"",
        ""null""
      ]
    },
    ""Parent"": {
      ""$ref"": ""#""
    },
    ""Exists"": {
      ""type"": ""boolean""
    },
    ""FullName"": {
      ""type"": [
        ""string"",
        ""null""
      ]
    },
    ""Extension"": {
      ""type"": [
        ""string"",
        ""null""
      ]
    },
    ""CreationTime"": {
      ""type"": ""string""
    },
    ""CreationTimeUtc"": {
      ""type"": ""string""
    },
    ""LastAccessTime"": {
      ""type"": ""string""
    },
    ""LastAccessTimeUtc"": {
      ""type"": ""string""
    },
    ""LastWriteTime"": {
      ""type"": ""string""
    },
    ""LastWriteTimeUtc"": {
      ""type"": ""string""
    },
    ""Attributes"": {
      ""type"": ""integer""
    }
  },
  ""required"": [
    ""Name"",
    ""Parent"",
    ""Exists"",
    ""FullName"",
    ""Extension"",
    ""CreationTime"",
    ""CreationTimeUtc"",
    ""LastAccessTime"",
    ""LastAccessTimeUtc"",
    ""LastWriteTime"",
    ""LastWriteTimeUtc"",
    ""Attributes""
  ]
}", json);

            DirectoryInfo temp = new DirectoryInfo(@"c:\temp");

            JTokenWriter   jsonWriter = new JTokenWriter();
            JsonSerializer serializer = new JsonSerializer();
            serializer.Converters.Add(new IsoDateTimeConverter());
            serializer.ContractResolver = new CustomDirectoryInfoMapper
            {
#if !(NETFX_CORE || PORTABLE || PORTABLE40 || ASPNETCORE50)
                IgnoreSerializableInterface = true
#endif
            };
            serializer.Serialize(jsonWriter, temp);

            List <string> errors = new List <string>();
            jsonWriter.Token.Validate(schema, (sender, args) => errors.Add(args.Message));

            Assert.AreEqual(0, errors.Count);
        }
예제 #13
0
        public void GenerateSchemaSerializable()
        {
            JSchemaGenerator generator = new JSchemaGenerator();

            generator.ContractResolver = new DefaultContractResolver
            {
                IgnoreSerializableAttribute = false
            };
            generator.UndefinedSchemaIdHandling = JSchemaUndefinedIdHandling.UseTypeName;

            JSchema schema = generator.Generate(typeof(Version), true);

            string json = schema.ToString();

            StringAssert.AreEqual(@"{
  ""id"": ""System.Version"",
  ""type"": [
    ""object"",
    ""null""
  ],
  ""additionalProperties"": false,
  ""properties"": {
    ""_Major"": {
      ""type"": ""integer""
    },
    ""_Minor"": {
      ""type"": ""integer""
    },
    ""_Build"": {
      ""type"": ""integer""
    },
    ""_Revision"": {
      ""type"": ""integer""
    }
  },
  ""required"": [
    ""_Major"",
    ""_Minor"",
    ""_Build"",
    ""_Revision""
  ]
}", json);

            JTokenWriter   jsonWriter = new JTokenWriter();
            JsonSerializer serializer = new JsonSerializer();

            serializer.ContractResolver = new DefaultContractResolver
            {
                IgnoreSerializableAttribute = false
            };
            serializer.Serialize(jsonWriter, new Version(1, 2, 3, 4));

            List <string> errors = new List <string>();

            jsonWriter.Token.Validate(schema, (sender, args) => errors.Add(args.Message));

            Assert.AreEqual(0, errors.Count);

            StringAssert.AreEqual(@"{
  ""_Major"": 1,
  ""_Minor"": 2,
  ""_Build"": 3,
  ""_Revision"": 4
}", jsonWriter.Token.ToString());

            Version version = jsonWriter.Token.ToObject <Version>(serializer);

            Assert.AreEqual(1, version.Major);
            Assert.AreEqual(2, version.Minor);
            Assert.AreEqual(3, version.Build);
            Assert.AreEqual(4, version.Revision);
        }
예제 #14
0
        public void JsonPropertyWithHandlingValues()
        {
            JSchemaGenerator generator = new JSchemaGenerator();

            generator.SchemaIdGenerationHandling = SchemaIdGenerationHandling.TypeName;
            JSchema schema = generator.Generate(typeof(JsonPropertyWithHandlingValues), true);
            string  json   = schema.ToString();

            StringAssert.AreEqual(@"{
  ""id"": ""JsonPropertyWithHandlingValues"",
  ""type"": [
    ""object"",
    ""null""
  ],
  ""properties"": {
    ""DefaultValueHandlingIgnoreProperty"": {
      ""type"": [
        ""string"",
        ""null""
      ],
      ""default"": ""Default!""
    },
    ""DefaultValueHandlingIncludeProperty"": {
      ""type"": [
        ""string"",
        ""null""
      ],
      ""default"": ""Default!""
    },
    ""DefaultValueHandlingPopulateProperty"": {
      ""type"": [
        ""string"",
        ""null""
      ],
      ""default"": ""Default!""
    },
    ""DefaultValueHandlingIgnoreAndPopulateProperty"": {
      ""type"": [
        ""string"",
        ""null""
      ],
      ""default"": ""Default!""
    },
    ""NullValueHandlingIgnoreProperty"": {
      ""type"": [
        ""string"",
        ""null""
      ]
    },
    ""NullValueHandlingIncludeProperty"": {
      ""type"": [
        ""string"",
        ""null""
      ]
    },
    ""ReferenceLoopHandlingErrorProperty"": {
      ""$ref"": ""JsonPropertyWithHandlingValues""
    },
    ""ReferenceLoopHandlingIgnoreProperty"": {
      ""$ref"": ""JsonPropertyWithHandlingValues""
    },
    ""ReferenceLoopHandlingSerializeProperty"": {
      ""$ref"": ""JsonPropertyWithHandlingValues""
    }
  },
  ""required"": [
    ""DefaultValueHandlingIncludeProperty"",
    ""DefaultValueHandlingPopulateProperty"",
    ""NullValueHandlingIncludeProperty"",
    ""ReferenceLoopHandlingErrorProperty"",
    ""ReferenceLoopHandlingIgnoreProperty"",
    ""ReferenceLoopHandlingSerializeProperty""
  ]
}", json);
        }