public void TypeNormalizationWithComplexTypesTest()
        {
            using (NewContext)
            {
                var codeModel = New <CodeModel>();
                codeModel.Name = "azure always rocks!";

                var childObject = New <CompositeType>("child");
                childObject.Add(New <Property>(new
                {
                    Name      = "childProperty",
                    ModelType = New <PrimaryType>(KnownPrimaryType.String)
                }));

                var customObjectType = New <CompositeType>("sample");
                customObjectType.Add(New <Property>(new
                {
                    Name      = "child",
                    ModelType = childObject
                }));
                customObjectType.Add(New <Property>(new
                {
                    Name      = "childList",
                    ModelType = New <SequenceType>(new
                    {
                        ElementType = childObject
                    })
                }));
                customObjectType.Add(New <Property>(new
                {
                    Name      = "childDict",
                    ModelType = New <DictionaryType>(new
                    {
                        ValueType = childObject
                    })
                }));

                codeModel.Add(customObjectType);
                codeModel.Add(childObject);

                new Settings();
                var plugin = new PluginCs();
                using (plugin.Activate()) {
                    codeModel = plugin.Serializer.Load(codeModel);
                    codeModel = plugin.Transformer.TransformCodeModel(codeModel);

                    Assert.Equal("Sample", codeModel.ModelTypes.First(m => m.Name == "Sample").Name);
                    Assert.Equal("Child", codeModel.ModelTypes.First(m => m.Name == "Sample").Properties[0].Name);
                    Assert.Equal("Child", codeModel.ModelTypes.First(m => m.Name == "Sample").Properties[0].ModelType.Name);
                    Assert.Equal("ChildList", codeModel.ModelTypes.First(m => m.Name == "Sample").Properties[1].Name);
                    Assert.Equal("System.Collections.Generic.IList<Child>",
                                 codeModel.ModelTypes.First(m => m.Name == "Sample").Properties[1].ModelType.Name);
                    Assert.Equal("ChildDict", codeModel.ModelTypes.First(m => m.Name == "Sample").Properties[2].Name);
                    Assert.Equal("System.Collections.Generic.IDictionary<string, Child>",
                                 codeModel.ModelTypes.First(m => m.Name == "Sample").Properties[2].ModelType.Name);
                    Assert.Equal("Child", codeModel.ModelTypes.First(m => m.Name == "Child").Name);
                    Assert.Equal("string", codeModel.ModelTypes.First(m => m.Name == "Child").Properties[0].ModelType.Name);
                }
            }
        }
Пример #2
0
        public void TestInputMapping()
        {
            var input   = Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory(typeof(MappingExtensionsTests)), "Resource", "swagger-payload-flatten.json");
            var modeler = new SwaggerModeler(new Settings {
                PayloadFlatteningThreshold = 3
            });
            var clientModel = modeler.Build(SwaggerParser.Parse(File.ReadAllText(input)));
            var plugin      = new PluginCs();

            using (plugin.Activate())
            {
                clientModel = plugin.Serializer.Load(clientModel);
                clientModel = plugin.Transformer.TransformCodeModel(clientModel);
                CodeGeneratorCs generator = new CodeGeneratorCs();

                generator.Generate(clientModel).GetAwaiter().GetResult();
                string body = Settings.Instance.FileSystemOutput.ReadAllText(Path.Combine("Payload.cs"));
                Assert.True(body.ContainsMultiline(@"
                    MinProduct minProduct = new MinProduct();
                    if (baseProductId != null || baseProductDescription != null || maxProductReference != null)
                    {
                        minProduct.BaseProductId = baseProductId;
                        minProduct.BaseProductDescription = baseProductDescription;
                        minProduct.MaxProductReference = maxProductReference;
                    }"));
            }
        }
        public void SequenceWithRenamedComplexType()
        {
            using (NewContext)
            {
                var codeModel = New <CodeModel>();
                codeModel.Namespace = "Polar.Greetings";
                codeModel.Name      = "azure always rocks!";

                var complexType = New <CompositeType>("Greetings");

                codeModel.Add(New <Method>(new
                {
                    Name       = "List",
                    ReturnType = new Response(New <SequenceType>(new { ElementType = complexType }), null)
                }));

                codeModel.Add(New <Method>(new
                {
                    Name       = "List2",
                    ReturnType = new Response(New <DictionaryType>(new { ValueType = complexType }), null)
                }));

                codeModel.Add(complexType);

                using (NewContext)
                {
                    new Settings();
                    var plugin = new PluginCs();
                    using (plugin.Activate()) {
                        codeModel = plugin.Serializer.Load(codeModel);
                        codeModel = plugin.Transformer.TransformCodeModel(codeModel);

                        Assert.Equal("GreetingsModel", codeModel.ModelTypes[0].Name);
                        Assert.Equal("System.Collections.Generic.IList<GreetingsModel>",
                                     codeModel.Methods[0].ReturnType.Body.Name);
                        Assert.Equal("System.Collections.Generic.IDictionary<string, GreetingsModel>",
                                     codeModel.Methods[1].ReturnType.Body.Name);
                    }
                }
            }
        }
Пример #4
0
        public void TestInputMapping()
        {
            using (NewContext)
            {
                var settings = new Settings
                {
                    Namespace = "Test",
                    Input     = Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "swagger-payload-flatten.json"),
                    PayloadFlatteningThreshold = 3,
                    OutputDirectory            = Path.GetTempPath()
                };
                settings.FileSystem = new MemoryFileSystem();
                settings.FileSystem.WriteFile("AutoRest.json", File.ReadAllText(Path.Combine(Core.Utilities.Extensions.CodeBaseDirectory, "Resource", "AutoRest.json")));
                settings.FileSystem.CreateDirectory(Path.GetDirectoryName(settings.Input));
                settings.FileSystem.WriteFile(settings.Input, File.ReadAllText(settings.Input));

                var modeler     = new SwaggerModeler();
                var clientModel = modeler.Build();
                var plugin      = new PluginCs();
                using (plugin.Activate())
                {
                    clientModel = plugin.Serializer.Load(clientModel);
                    clientModel = plugin.Transformer.TransformCodeModel(clientModel);
                    CodeGeneratorCs generator = new CodeGeneratorCs();

                    generator.Generate(clientModel).GetAwaiter().GetResult();
                    string body = settings.FileSystem.ReadFileAsText(Path.Combine(settings.OutputDirectory, "Payload.cs"));
                    Assert.True(body.ContainsMultiline(@"
                    MinProduct minProduct = new MinProduct();
                    if (baseProductId != null || baseProductDescription != null || maxProductReference != null)
                    {
                        minProduct.BaseProductId = baseProductId;
                        minProduct.BaseProductDescription = baseProductDescription;
                        minProduct.MaxProductReference = maxProductReference;
                    }"));
                }
            }
        }
        public void TypeNormalizationWithComplexTypesTest()
        {
            using (NewContext)
            {

                var codeModel = New<CodeModel>();
                codeModel.Name = "azure always rocks!";

                var childObject = New<CompositeType>("child");
                childObject.Add(New<Property>(new
                {
                    Name = "childProperty",
                    ModelType = New<PrimaryType>(KnownPrimaryType.String)
                }));

                var customObjectType = New<CompositeType>("sample");
                customObjectType.Add(New<Property>(new
                {
                    Name = "child",
                    ModelType = childObject
                }));
                customObjectType.Add(New<Property>(new
                {
                    Name = "childList",
                    ModelType = New<SequenceType>(new
                    {
                        ElementType = childObject
                    })
                }));
                customObjectType.Add(New<Property>(new
                {
                    Name = "childDict",
                    ModelType = New<DictionaryType>(new
                    {
                        ValueType = childObject
                    })
                }));

                codeModel.Add(customObjectType);
                codeModel.Add(childObject);

                new Settings();
                var plugin = new PluginCs();
                using (plugin.Activate()) {
                    codeModel = plugin.Serializer.Load(codeModel);
                    codeModel = plugin.Transformer.TransformCodeModel(codeModel);

                    Assert.Equal("Sample", codeModel.ModelTypes.First(m => m.Name == "Sample").Name);
                    Assert.Equal("Child", codeModel.ModelTypes.First(m => m.Name == "Sample").Properties[0].Name);
                    Assert.Equal("Child", codeModel.ModelTypes.First(m => m.Name == "Sample").Properties[0].ModelType.Name);
                    Assert.Equal("ChildList", codeModel.ModelTypes.First(m => m.Name == "Sample").Properties[1].Name);
                    Assert.Equal("System.Collections.Generic.IList<Child>",
                        codeModel.ModelTypes.First(m => m.Name == "Sample").Properties[1].ModelType.Name);
                    Assert.Equal("ChildDict", codeModel.ModelTypes.First(m => m.Name == "Sample").Properties[2].Name);
                    Assert.Equal("System.Collections.Generic.IDictionary<string, Child>",
                        codeModel.ModelTypes.First(m => m.Name == "Sample").Properties[2].ModelType.Name);
                    Assert.Equal("Child", codeModel.ModelTypes.First(m => m.Name == "Child").Name);
                    Assert.Equal("string", codeModel.ModelTypes.First(m => m.Name == "Child").Properties[0].ModelType.Name);
                }
            }
        }
        public void SequenceWithRenamedComplexType()
        {
            using (NewContext)
            {

                var codeModel = New<CodeModel>();
                codeModel.Namespace = "Polar.Greetings";
                codeModel.Name = "azure always rocks!";

                var complexType = New<CompositeType>("Greetings");

                codeModel.Add(New<Method>(new
                {
                    Name = "List",
                    ReturnType = new Response(New<SequenceType>(new {ElementType = complexType}), null)
                }));

                codeModel.Add(New<Method>(new
                {
                    Name = "List2",
                    ReturnType = new Response(New<DictionaryType>(new {ValueType = complexType}), null)
                }));

                codeModel.Add(complexType);

                using (NewContext)
                {
                    new Settings();
                    var plugin = new PluginCs();
                    using (plugin.Activate()) {
                        codeModel = plugin.Serializer.Load(codeModel);
                        codeModel = plugin.Transformer.TransformCodeModel(codeModel);

                        Assert.Equal("GreetingsModel", codeModel.ModelTypes[0].Name);
                        Assert.Equal("System.Collections.Generic.IList<GreetingsModel>",
                            codeModel.Methods[0].ReturnType.Body.Name);
                        Assert.Equal("System.Collections.Generic.IDictionary<string, GreetingsModel>",
                            codeModel.Methods[1].ReturnType.Body.Name);
                    }
                }
            }
        }