Ejemplo n.º 1
0
        public async Task MarkAllDeprecatedOperationVariantsObsolete()
        {
            using (var fileSystem = GenerateCodeForTestFromSpec())
            {
                // Expected Files
                Assert.True(fileSystem.FileExists(@"Models\ResultObject.cs"));
                Assert.True(fileSystem.FileExists(@"IDeprecated.cs"));
                Assert.True(fileSystem.FileExists(@"Deprecated.cs"));
                Assert.True(fileSystem.FileExists(@"DeprecatedExtensions.cs"));
                Assert.True(fileSystem.FileExists(@"IApproved.cs"));
                Assert.True(fileSystem.FileExists(@"Approved.cs"));
                Assert.True(fileSystem.FileExists(@"ApprovedExtensions.cs"));

                var result = await Compile(fileSystem);

                // filter the warnings
                var warnings = result.Messages.Where(
                    each => each.Severity == DiagnosticSeverity.Warning &&
                    !SuppressWarnings.Contains(each.Id)).ToArray();

                // filter the errors
                var errors = result.Messages.Where(each => each.Severity == DiagnosticSeverity.Error).ToArray();

                Write(warnings, fileSystem);
                Write(errors, fileSystem);

                // use this to write out all the messages, even hidden ones.
                // Write(result.Messages, fileSystem);

                // Don't proceed unless we have zero warnings.
                Assert.Empty(warnings);
                // Don't proceed unless we have zero Errors.
                Assert.Empty(errors);

                // Should also succeed.
                Assert.True(result.Succeeded);

                // try to load the assembly
                var asm = LoadAssembly(result.Output);
                Assert.NotNull(asm);

                // verify that deprecated_operations are marked correctly
                var deprecatedInterface = asm.ExportedTypes.FirstOrDefault(each => each.FullName == "Test.IDeprecated");
                Assert.NotNull(deprecatedInterface);
                Assert.NotNull(deprecatedInterface.GetMethod("OperationWithHttpMessagesAsync").GetCustomAttribute(typeof(System.ObsoleteAttribute)));

                var deprecatedClass = asm.ExportedTypes.FirstOrDefault(each => each.FullName == "Test.Deprecated");
                Assert.NotNull(deprecatedClass);
                Assert.NotNull(deprecatedClass.GetMethod("OperationWithHttpMessagesAsync").GetCustomAttribute(typeof(System.ObsoleteAttribute)));

                var deprecatedExtensions = asm.ExportedTypes.FirstOrDefault(each => each.FullName == "Test.DeprecatedExtensions");
                Assert.NotNull(deprecatedExtensions);
                Assert.NotNull(deprecatedExtensions.GetMethod("Operation").GetCustomAttribute(typeof(System.ObsoleteAttribute)));
                Assert.NotNull(deprecatedExtensions.GetMethod("OperationAsync").GetCustomAttribute(typeof(System.ObsoleteAttribute)));

                // verify the other operations are not marked as deprecated
                var approvedInterface = asm.ExportedTypes.FirstOrDefault(each => each.FullName == "Test.IApproved");
                Assert.NotNull(approvedInterface);
                Assert.Null(approvedInterface.GetMethod("OperationWithHttpMessagesAsync").GetCustomAttribute(typeof(System.ObsoleteAttribute)));

                var approvedClass = asm.ExportedTypes.FirstOrDefault(each => each.FullName == "Test.Approved");
                Assert.NotNull(approvedClass);
                Assert.Null(approvedClass.GetMethod("OperationWithHttpMessagesAsync").GetCustomAttribute(typeof(System.ObsoleteAttribute)));

                var approvedExtensions = asm.ExportedTypes.FirstOrDefault(each => each.FullName == "Test.ApprovedExtensions");
                Assert.NotNull(approvedExtensions);
                Assert.Null(approvedExtensions.GetMethod("Operation").GetCustomAttribute(typeof(System.ObsoleteAttribute)));
                Assert.Null(approvedExtensions.GetMethod("OperationAsync").GetCustomAttribute(typeof(System.ObsoleteAttribute)));
            }
        }
Ejemplo n.º 2
0
        public async Task NullableParams()
        {
            using (var fileSystem = GenerateCodeForTestFromSpec())
            {
                // Expected Files
                string generatedCodeFileName = Path.Combine("GeneratedCode", "TestOperations.cs");
                Assert.True(fileSystem.FileExists(generatedCodeFileName));
                var generatedCode = fileSystem.VirtualStore[generatedCodeFileName].ToString();

                var result = await Compile(fileSystem);

                // filter the warnings
                var warnings = result.Messages.Where(
                    each => each.Severity == DiagnosticSeverity.Warning &&
                    !SuppressWarnings.Contains(each.Id)).ToArray();

                // filter the errors
                var errors = result.Messages.Where(each => each.Severity == DiagnosticSeverity.Error).ToArray();

                Write(warnings, fileSystem);
                Write(errors, fileSystem);

                // use this to write out all the messages, even hidden ones.
                // Write(result.Messages, fileSystem);

                // Don't proceed unless we have zero warnings.
                Assert.Empty(warnings);
                // Don't proceed unless we have zero Errors.
                Assert.Empty(errors);

                // Should also succeed.
                Assert.True(result.Succeeded);

                // try to load the assembly
                var asm = LoadAssembly(result.Output);
                Assert.NotNull(asm);
                var testOperations = asm.ExportedTypes.FirstOrDefault(type => type.FullName == "Test.TestOperations");
                Assert.NotNull(testOperations);
                var operation = testOperations.GetMethod("OpWithHttpMessagesAsync");
                Assert.NotNull(operation);
                var parameters = operation.GetParameters();

                for (int propDefault = 0; propDefault < 2; propDefault++)
                {
                    for (int propXNullable = 0; propXNullable < 3; propXNullable++)
                    {
                        for (int propRequired = 0; propRequired < 3; propRequired++)
                        {
                            for (int propReadOnly = 0; propReadOnly < 3; propReadOnly++)
                            {
                                // retrieve param with given configuration
                                string paramName = $"param{propDefault}{propXNullable}{propRequired}{propReadOnly}";
                                var    param     = parameters.FirstOrDefault(p => p.Name == paramName);
                                Assert.NotNull(param);

                                // ensure that null-checks are there exactly if the parameter is nullable
                                bool isNullable       = param.ParameterType.IsNullableValueType();
                                bool isCheckedForNull = generatedCode.Contains($"if ({paramName} != null)");
                                Assert.Equal(isNullable, isCheckedForNull);

                                // ensure that `x-nullable` (if set) determines nullability,
                                // otherwise, consult `required`
                                if (propXNullable < 2)
                                {
                                    Assert.Equal(isNullable, propXNullable == 1);
                                }
                                else
                                {
                                    Assert.Equal(isNullable, propRequired != 1);
                                }

                                // ensure that default value is given except if `required` is set
                                bool hasDefault = param.HasDefaultValue;
                                Assert.Equal(hasDefault, propRequired != 1);

                                // ensure that default value is the one that was specified (if one was specified)
                                int?defaultValue = param.DefaultValue as int?;
                                if (hasDefault && propDefault == 1)
                                {
                                    Assert.Equal(defaultValue, 42);
                                }

                                // print full table
                                // WriteLine($"{propDefault}\t{propXNullable}\t{propRequired}\t{propReadOnly}\t{isNullable}\t{(hasDefault ? defaultValue.ToString() : "-")}");
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public async Task SupportModelsNameOverride()
        {
            using (NewContext)
            {
                string modelsName = "MyModels";

                MemoryFileSystem fileSystem = CreateMockFilesystem();

                var settings = new Settings
                {
                    Modeler         = "Swagger",
                    CodeGenerator   = "CSharp",
                    FileSystem      = fileSystem,
                    OutputDirectory = "GeneratedCode",
                    Namespace       = "Test",
                    ModelsName      = modelsName
                };

                using (fileSystem = $"{GetType().Name}".GenerateCodeInto(fileSystem, settings))
                {
                    // Expected Files
                    Assert.True(fileSystem.FileExists($@"{settings.OutputDirectory}\{modelsName}\ResultObject.cs"));

                    var result = await Compile(fileSystem);

                    // filter the warnings
                    var warnings = result.Messages.Where(
                        each => each.Severity == DiagnosticSeverity.Warning &&
                        !SuppressWarnings.Contains(each.Id)).ToArray();

                    // use this to dump the files to disk for examination
                    // fileSystem.SaveFilesToTemp($"{GetType().Name}");

                    // filter the errors
                    var errors = result.Messages.Where(each => each.Severity == DiagnosticSeverity.Error).ToArray();

                    Write(warnings, fileSystem);
                    Write(errors, fileSystem);

                    // use this to write out all the messages, even hidden ones.
                    // Write(result.Messages, fileSystem);

                    // Don't proceed unless we have zero Warnings.
                    Assert.Empty(warnings);

                    // Don't proceed unless we have zero Errors.
                    Assert.Empty(errors);

                    // Should also succeed.
                    Assert.True(result.Succeeded);

                    // try to load the assembly
                    var asm = Assembly.Load(result.Output.GetBuffer());
                    Assert.NotNull(asm);

                    // verify that we have the class we expected
                    var resultObject =
                        asm.ExportedTypes.FirstOrDefault(each => each.FullName == $"Test.{modelsName}.ResultObject");
                    Assert.NotNull(resultObject);
                }
            }
        }
Ejemplo n.º 4
0
        public async Task CheckModelTypeDisambiguation()
        {
            using (var fileSystem = GenerateCodeForTestFromSpec())
            {
                // Expected Files
                Assert.True(fileSystem.FileExists(@"SimpleAPIExtensions.cs"));
                Assert.True(fileSystem.FileExists(@"Models\CowbellOKResponse.cs"));
                Assert.True(fileSystem.FileExists(@"Models\CowbellOKResponseModel.cs"));
                Assert.True(fileSystem.FileExists(@"Models\CowbellOKResponseModelModel.cs"));

                var result = await Compile(fileSystem);

                // filter the warnings
                var warnings = result.Messages.Where(
                    each => each.Severity == DiagnosticSeverity.Warning &&
                    !SuppressWarnings.Contains(each.Id)).ToArray();

                // filter the errors
                var errors = result.Messages.Where(each => each.Severity == DiagnosticSeverity.Error).ToArray();

                Write(warnings, fileSystem);
                Write(errors, fileSystem);

                // use this to write out all the messages, even hidden ones.
                // Write(result.Messages, fileSystem);

                // Don't proceed unless we have zero warnings.
                Assert.Empty(warnings);
                // Don't proceed unless we have zero Errors.
                Assert.Empty(errors);

                // Should also succeed.
                Assert.True(result.Succeeded);

                // try to load the assembly
                var asm = LoadAssembly(result.Output);
                Assert.NotNull(asm);

                // retrieve generated model types (concrete names of those types may change, please adjust this test case accordingly)
                var cowbellResponse     = asm.ExportedTypes.FirstOrDefault(each => each.FullName == "Test.Models.CowbellOKResponseModel");
                var cowbellResponseInt  = asm.ExportedTypes.FirstOrDefault(each => each.FullName == "Test.Models.CowbellOKResponse");
                var cowbellResponseBool = asm.ExportedTypes.FirstOrDefault(each => each.FullName == "Test.Models.CowbellOKResponseModelModel");

                // validate model types
                var cowbellResponseProp     = cowbellResponse.GetProperty("PropResponse");
                var cowbellResponseIntProp  = cowbellResponseInt.GetProperty("PropParamInt");
                var cowbellResponseBoolProp = cowbellResponseBool.GetProperty("PropParamBool");
                Assert.NotNull(cowbellResponseProp);
                Assert.NotNull(cowbellResponseIntProp);
                Assert.NotNull(cowbellResponseBoolProp);
                Assert.Equal(typeof(string), cowbellResponseProp.PropertyType);
                Assert.Equal(typeof(int?), cowbellResponseIntProp.PropertyType);
                Assert.Equal(typeof(bool?), cowbellResponseBoolProp.PropertyType);

                // verify that signatures are correct
                var simpleApiExtensions = asm.ExportedTypes.FirstOrDefault(each => each.FullName == "Test.SimpleAPIExtensions");
                Assert.NotNull(simpleApiExtensions);
                var cowbellMethod0 = simpleApiExtensions.GetMethod("Cowbell");
                var cowbellMethod1 = simpleApiExtensions.GetMethod("Cowbell1");
                var cowbellMethod2 = simpleApiExtensions.GetMethod("Cowbell2");
                var cowbellMethod3 = simpleApiExtensions.GetMethod("Cowbell3");
                Assert.NotNull(cowbellMethod0);
                Assert.NotNull(cowbellMethod1);
                Assert.NotNull(cowbellMethod2);
                Assert.NotNull(cowbellMethod3);
                var cowbellMethod0Param = cowbellMethod0.GetParameters().FirstOrDefault(p => p.Name == "cowbellOKResponse");
                var cowbellMethod1Param = cowbellMethod1.GetParameters().FirstOrDefault(p => p.Name == "cowbellOKResponse");
                var cowbellMethod2Param = cowbellMethod2.GetParameters().FirstOrDefault(p => p.Name == "cowbellOKResponse");
                var cowbellMethod3Param = cowbellMethod3.GetParameters().FirstOrDefault(p => p.Name == "cowbellOKResponse");
                Assert.NotNull(cowbellMethod0Param);
                Assert.NotNull(cowbellMethod1Param);
                Assert.NotNull(cowbellMethod2Param);
                Assert.NotNull(cowbellMethod3Param);

                Assert.Equal(cowbellResponseInt, cowbellMethod0Param.ParameterType);
                Assert.Equal(cowbellResponseBool, cowbellMethod1Param.ParameterType);
                Assert.Equal(typeof(int?), cowbellMethod2Param.ParameterType);
                Assert.Equal(cowbellResponseBool, cowbellMethod3Param.ParameterType);

                Assert.Equal(cowbellResponse, cowbellMethod0.ReturnType);
                Assert.Equal(typeof(void), cowbellMethod1.ReturnType);
                Assert.Equal(typeof(void), cowbellMethod2.ReturnType);
                Assert.Equal(typeof(void), cowbellMethod3.ReturnType);
            }
        }
        public async Task DeprecatedOnLotsOfStuff()
        {
            // simplified test pattern for unit testing aspects of code generation
            using (var fileSystem = GenerateCodeForTestFromSpec())
            {
                // check for the expected class.
                Assert.True(fileSystem.FileExists(@"PathExtensions.cs"));
                Assert.True(fileSystem.FileExists(@"Models/PetNo.cs"));
                Assert.True(fileSystem.FileExists(@"Models/PetYes.cs"));
                Assert.True(fileSystem.FileExists(@"Models/Pet.cs"));
                Assert.True(fileSystem.FileExists(@"Models/ChildPet.cs"));
                Assert.True(fileSystem.FileExists(@"Models/Enum1No.cs"));
                Assert.True(fileSystem.FileExists(@"Models/Enum1Yes.cs"));
                Assert.True(fileSystem.FileExists(@"Models/Enum2No.cs"));
                Assert.True(fileSystem.FileExists(@"Models/Enum2Yes.cs"));
                Assert.True(fileSystem.FileExists(@"Models/Enum3No.cs"));
                Assert.True(fileSystem.FileExists(@"Models/Enum3Yes.cs"));

                var result = await Compile(fileSystem);

                // filter the warnings
                var warnings = result.Messages.Where(
                    each => each.Severity == DiagnosticSeverity.Warning &&
                    !SuppressWarnings.Contains(each.Id)).ToArray();

                // filter the errors
                var errors = result.Messages.Where(each => each.Severity == DiagnosticSeverity.Error).ToArray();

                Write(warnings, fileSystem);
                Write(errors, fileSystem);

                // Don't proceed unless we have zero Warnings (except "obsolete" stuff).
                Assert.Empty(warnings.Where(x => !x.GetMessage().Contains("obsolete")));

                // Don't proceed unless we have zero Errors.
                Assert.Empty(errors);

                // Should also succeed.
                Assert.True(result.Succeeded);

                // try to load the assembly
                var asm = LoadAssembly(result.Output);
                Assert.NotNull(asm);


                // VALIDATE

                // - method
                var approvedExtensions = asm.ExportedTypes.FirstOrDefault(each => each.FullName == "Test.PathExtensions");
                Assert.Null(approvedExtensions.GetMethod("No").GetCustomAttribute(typeof(System.ObsoleteAttribute)));
                Assert.NotNull(approvedExtensions.GetMethod("Yes").GetCustomAttribute(typeof(System.ObsoleteAttribute)));

                // - class
                Assert.Null(asm.ExportedTypes.FirstOrDefault(each => each.FullName == "Test.Models.PetNo").GetCustomAttribute(typeof(System.ObsoleteAttribute)));
                Assert.NotNull(asm.ExportedTypes.FirstOrDefault(each => each.FullName == "Test.Models.PetYes").GetCustomAttribute(typeof(System.ObsoleteAttribute)));

                // - enums
                Assert.Null(asm.ExportedTypes.FirstOrDefault(each => each.FullName == "Test.Models.Enum1No").GetCustomAttribute(typeof(System.ObsoleteAttribute)));
                Assert.NotNull(asm.ExportedTypes.FirstOrDefault(each => each.FullName == "Test.Models.Enum1Yes").GetCustomAttribute(typeof(System.ObsoleteAttribute)));
                Assert.Null(asm.ExportedTypes.FirstOrDefault(each => each.FullName == "Test.Models.Enum2No").GetCustomAttribute(typeof(System.ObsoleteAttribute)));
                Assert.NotNull(asm.ExportedTypes.FirstOrDefault(each => each.FullName == "Test.Models.Enum2Yes").GetCustomAttribute(typeof(System.ObsoleteAttribute)));
                Assert.Null(asm.ExportedTypes.FirstOrDefault(each => each.FullName == "Test.Models.Enum3No").GetCustomAttribute(typeof(System.ObsoleteAttribute)));
                Assert.NotNull(asm.ExportedTypes.FirstOrDefault(each => each.FullName == "Test.Models.Enum3Yes").GetCustomAttribute(typeof(System.ObsoleteAttribute)));

                // - property
                var pet = asm.ExportedTypes.FirstOrDefault(each => each.FullName == "Test.Models.Pet");
                Assert.Null(pet.GetProperty("NameNo").GetCustomAttribute(typeof(System.ObsoleteAttribute)));
                Assert.NotNull(pet.GetProperty("NameYes").GetCustomAttribute(typeof(System.ObsoleteAttribute)));

                // - property via type
                var pet2 = asm.ExportedTypes.FirstOrDefault(each => each.FullName == "Test.Models.ChildPet");
                Assert.Null(pet2.GetProperty("ParentNo").GetCustomAttribute(typeof(System.ObsoleteAttribute)));
                Assert.Null(pet2.GetProperty("ParentYes").GetCustomAttribute(typeof(System.ObsoleteAttribute)));
            }
        }