public void It_should_generate_models_for_explicitly_configured_sub_types()
        {
            var productType = new BasePolymorphicType<Product>()
                .DiscriminateBy(p => p.Type)
                .SubType<Book>()
                .SubType<Album>()
                .SubType<Service>(s => s
                    .SubType<Shipping>()
                    .SubType<Packaging>());

            var swaggerProvider = GetSwaggerProvider(polymorphicTypes: new PolymorphicType[] { productType });

            ApiDeclaration(swaggerProvider, "Products", dec =>
                {
                    Assert.AreEqual(6, dec.Models.Count);

                    Model(dec, "Product", model =>
                        {
                            ModelProperty(model, "Id", property =>
                                {
                                    Assert.AreEqual("integer", property.Type);
                                    Assert.AreEqual("int32", property.Format);
                                    Assert.IsNull(property.Items);
                                    Assert.IsNull(property.Enum);
                                });

                            ModelProperty(model, "Name", property =>
                            {
                                Assert.AreEqual("string", property.Type);
                                Assert.IsNull(property.Format);
                                Assert.IsNull(property.Items);
                                Assert.IsNull(property.Enum);
                            });

                            ModelProperty(model, "Price", property =>
                                {
                                    Assert.AreEqual("number", property.Type);
                                    Assert.AreEqual("double", property.Format);
                                    Assert.IsNull(property.Items);
                                    Assert.IsNull(property.Enum);
                                });

                            ModelProperty(model, "Type", property =>
                                {
                                    Assert.AreEqual("string", property.Type);
                                    Assert.IsNull(property.Format);
                                    Assert.IsNull(property.Items);
                                    CollectionAssert.AreEqual(new[] { "Book", "Album", "Shipping", "Packaging" }, property.Enum);
                                });

                            CollectionAssert.AreEqual(new[] {"Book", "Album", "Service"}, model.SubTypes);
                            Assert.AreEqual("Type", model.Discriminator);
                        });

                    Model(dec, "Book", model =>
                        {
                            Assert.AreEqual(1, model.Properties.Count);

                            ModelProperty(model, "Author", property =>
                                {
                                    Assert.AreEqual("string", property.Type);
                                    Assert.IsNull(property.Format);
                                    Assert.IsNull(property.Items);
                                    Assert.IsNull(property.Enum);
                                });

                            CollectionAssert.IsEmpty(model.SubTypes);
                            Assert.IsNull(model.Discriminator);
                        });

                    Model(dec, "Album", model =>
                        {
                            Assert.AreEqual(1, model.Properties.Count);

                            ModelProperty(model, "Artist", property =>
                                {
                                    Assert.AreEqual("string", property.Type);
                                    Assert.IsNull(property.Format);
                                    Assert.IsNull(property.Items);
                                    Assert.IsNull(property.Enum);
                                });

                            CollectionAssert.IsEmpty(model.SubTypes);
                            Assert.IsNull(model.Discriminator);
                        });

                    Model(dec, "Service", model =>
                        {
                            CollectionAssert.IsEmpty(model.Properties);
                            CollectionAssert.AreEqual(new[] {"Shipping", "Packaging"}, model.SubTypes);
                            Assert.IsNull(model.Discriminator);
                        });

                    Model(dec, "Shipping", model =>
                        {
                            CollectionAssert.IsEmpty(model.Properties);
                            CollectionAssert.IsEmpty(model.SubTypes);
                            Assert.IsNull(model.Discriminator);
                        });

                    Model(dec, "Packaging", model =>
                        {
                            CollectionAssert.IsEmpty(model.Properties);
                            CollectionAssert.IsEmpty(model.SubTypes);
                            Assert.IsNull(model.Discriminator);
                        });
                });
        }
Esempio n. 2
0
        public void It_should_generate_models_for_explicitly_configured_sub_types()
        {
            var productType = new BasePolymorphicType <Product>()
                              .DiscriminateBy(p => p.Type)
                              .SubType <Book>()
                              .SubType <Album>()
                              .SubType <Service>(s => s
                                                 .SubType <Shipping>()
                                                 .SubType <Packaging>());

            var swaggerProvider = GetSwaggerProvider(polymorphicTypes: new PolymorphicType[] { productType });

            ApiDeclaration(swaggerProvider, "Products", dec =>
            {
                Assert.AreEqual(6, dec.Models.Count);

                Model(dec, "Product", model =>
                {
                    ModelProperty(model, "Id", property =>
                    {
                        Assert.AreEqual("integer", property.Type);
                        Assert.AreEqual("int32", property.Format);
                        Assert.IsNull(property.Items);
                        Assert.IsNull(property.Enum);
                    });

                    ModelProperty(model, "Name", property =>
                    {
                        Assert.AreEqual("string", property.Type);
                        Assert.IsNull(property.Format);
                        Assert.IsNull(property.Items);
                        Assert.IsNull(property.Enum);
                    });

                    ModelProperty(model, "Price", property =>
                    {
                        Assert.AreEqual("number", property.Type);
                        Assert.AreEqual("double", property.Format);
                        Assert.IsNull(property.Items);
                        Assert.IsNull(property.Enum);
                    });

                    ModelProperty(model, "Type", property =>
                    {
                        Assert.AreEqual("string", property.Type);
                        Assert.IsNull(property.Format);
                        Assert.IsNull(property.Items);
                        CollectionAssert.AreEqual(new[] { "Book", "Album", "Shipping", "Packaging" }, property.Enum);
                    });

                    CollectionAssert.AreEqual(new[] { "Book", "Album", "Service" }, model.SubTypes);
                    Assert.AreEqual("Type", model.Discriminator);
                });

                Model(dec, "Book", model =>
                {
                    Assert.AreEqual(1, model.Properties.Count);

                    ModelProperty(model, "Author", property =>
                    {
                        Assert.AreEqual("string", property.Type);
                        Assert.IsNull(property.Format);
                        Assert.IsNull(property.Items);
                        Assert.IsNull(property.Enum);
                    });

                    CollectionAssert.IsEmpty(model.SubTypes);
                    Assert.IsNull(model.Discriminator);
                });

                Model(dec, "Album", model =>
                {
                    Assert.AreEqual(1, model.Properties.Count);

                    ModelProperty(model, "Artist", property =>
                    {
                        Assert.AreEqual("string", property.Type);
                        Assert.IsNull(property.Format);
                        Assert.IsNull(property.Items);
                        Assert.IsNull(property.Enum);
                    });

                    CollectionAssert.IsEmpty(model.SubTypes);
                    Assert.IsNull(model.Discriminator);
                });

                Model(dec, "Service", model =>
                {
                    CollectionAssert.IsEmpty(model.Properties);
                    CollectionAssert.AreEqual(new[] { "Shipping", "Packaging" }, model.SubTypes);
                    Assert.IsNull(model.Discriminator);
                });

                Model(dec, "Shipping", model =>
                {
                    CollectionAssert.IsEmpty(model.Properties);
                    CollectionAssert.IsEmpty(model.SubTypes);
                    Assert.IsNull(model.Discriminator);
                });

                Model(dec, "Packaging", model =>
                {
                    CollectionAssert.IsEmpty(model.Properties);
                    CollectionAssert.IsEmpty(model.SubTypes);
                    Assert.IsNull(model.Discriminator);
                });
            });
        }