public void MemberNullability(Nullability nullability, Type type, SchemaValueType valueType)
        {
            var config = new SchemaGeneratorConfiguration
            {
                Nullability = nullability
            };

            // Nullability affects root schema so only PropertiesKeywords are compared
            var expected = new JsonSchemaBuilder()
                           .Properties(
                (nameof(ReferenceMember.Property), new JsonSchemaBuilder().Type(valueType)))
                           .Build()
                           .Keywords
                           .OfType <PropertiesKeyword>()
                           .First();

            var actual = new JsonSchemaBuilder()
                         .FromType(type, config)
                         .Build()
                         .Keywords
                         .OfType <PropertiesKeyword>()
                         .First();

            Assert.AreEqual(expected, actual);
        }
Esempio n. 2
0
        public void TypesWithAnOddNumberOfPropertiesShouldBeImmutable()
        {
            var configuration = new SchemaGeneratorConfiguration
            {
                Refiners = { new Refiner() }
            };

            JsonSchema expected = new JsonSchemaBuilder()
                                  .Type(SchemaValueType.Object)
                                  .Properties(
                ("Value1", new JsonSchemaBuilder().Type(SchemaValueType.Integer)),
                ("Value2", new JsonSchemaBuilder().Type(SchemaValueType.String)),
                ("Value3", new JsonSchemaBuilder().Type(SchemaValueType.Boolean))
                )
                                  .ReadOnly(true);

            JsonSchema actual = new JsonSchemaBuilder().FromType <ThreeProps>(configuration);

            Console.WriteLine(JsonSerializer.Serialize(expected, new JsonSerializerOptions {
                WriteIndented = true
            }));
            Console.WriteLine(JsonSerializer.Serialize(actual, new JsonSerializerOptions {
                WriteIndented = true
            }));
            Assert.AreEqual(expected, actual);
        }
        public void PropertiesByName()
        {
            var config = new SchemaGeneratorConfiguration
            {
                PropertyOrder = PropertyOrder.ByName
            };

            JsonSchema schema = new JsonSchemaBuilder()
                                .FromType <SpecifiedOrder>(config);

            var properties = schema.Keywords.OfType <PropertiesKeyword>().Single();

            Assert.AreEqual(nameof(SpecifiedOrder.First), properties.Properties.Keys.First());
            Assert.AreEqual(nameof(SpecifiedOrder.Second), properties.Properties.Keys.Last());
        }
        public void PropertiesAsDeclaredByType()
        {
            var config = new SchemaGeneratorConfiguration
            {
                PropertyOrder = PropertyOrder.AsDeclared
            };

            JsonSchema schema = new JsonSchemaBuilder()
                                .FromType <SpecifiedOrderDerived>(config);

            var properties = schema.Keywords.OfType <PropertiesKeyword>().Single();

            Assert.AreEqual(nameof(SpecifiedOrder.Second), properties.Properties.Keys.ElementAt(0));
            Assert.AreEqual(nameof(SpecifiedOrder.First), properties.Properties.Keys.ElementAt(1));
            Assert.AreEqual(nameof(SpecifiedOrderDerived.Third), properties.Properties.Keys.ElementAt(2));
        }
        public void TypeNullability(Nullability nullability, Type type, SchemaValueType valueType)
        {
            var config = new SchemaGeneratorConfiguration
            {
                Nullability = nullability
            };

            var expected = new JsonSchemaBuilder()
                           .Type(valueType)
                           .Build();

            var actual = new JsonSchemaBuilder()
                         .FromType(type, config)
                         .Build();

            Assert.AreEqual(expected, actual);
        }
        public void VerifyNameChanges(PropertyNamingMethod namingMethod, string expectedName)
        {
            var config = new SchemaGeneratorConfiguration
            {
                PropertyNamingMethod = namingMethod
            };
            var expected = new JsonSchemaBuilder()
                           .Type(SchemaValueType.Object)
                           .Properties(
                (expectedName, new JsonSchemaBuilder().Type(SchemaValueType.String))
                )
                           .Build();

            var actual = new JsonSchemaBuilder().FromType <Target>(config).Build();

            AssertEqual(expected, actual);
        }