diff --git a/packages/http-client-csharp/eng/scripts/Test-CadlRanch.ps1 b/packages/http-client-csharp/eng/scripts/Test-CadlRanch.ps1 index 135a5f6b10..eb9be604f2 100644 --- a/packages/http-client-csharp/eng/scripts/Test-CadlRanch.ps1 +++ b/packages/http-client-csharp/eng/scripts/Test-CadlRanch.ps1 @@ -14,7 +14,6 @@ $azureSpecsDirectory = Join-Path $packageRoot 'node_modules' '@azure-tools' 'azu $cadlRanchRoot = Join-Path $packageRoot 'generator' 'TestProjects' 'CadlRanch' $cadlRanchRootHttp = Join-Path $cadlRanchRoot 'http' $directories = Get-ChildItem -Path "$cadlRanchRootHttp" -Directory -Recurse -$cadlRanchCsproj = Join-Path $packageRoot 'generator' 'TestProjects' 'CadlRanch.Tests' 'TestProjects.CadlRanch.Tests.csproj' $coverageDir = Join-Path $packageRoot 'generator' 'artifacts' 'coverage' @@ -46,7 +45,7 @@ foreach ($directory in $directories) { $testFilter += "._$segment" $testPath = Join-Path $testPath "_$segment" } - else{ + else { $testFilter += ".$segment" $testPath = Join-Path $testPath $segment } diff --git a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Client/Structure/ClientOperationGroup/ClientOperationGroupTests.cs b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Client/Structure/ClientOperationGroup/ClientOperationGroupTests.cs index 3c73c0e356..04974e4ac2 100644 --- a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Client/Structure/ClientOperationGroup/ClientOperationGroupTests.cs +++ b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Client/Structure/ClientOperationGroup/ClientOperationGroupTests.cs @@ -1,11 +1,11 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using System.Linq; +using System.Threading.Tasks; using Client.Structure.Service; using Client.Structure.Service.Models; using NUnit.Framework; -using System.Linq; -using System.Threading.Tasks; namespace TestProjects.CadlRanch.Tests.Http.Client.Structure.ClientOperationGroup { diff --git a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Client/Structure/Default/DefaultTests.cs b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Client/Structure/Default/DefaultTests.cs index fae20c8f35..3d2b7645da 100644 --- a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Client/Structure/Default/DefaultTests.cs +++ b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Client/Structure/Default/DefaultTests.cs @@ -6,7 +6,6 @@ using System.Threading.Tasks; using Client.Structure.Service.Default; using Client.Structure.Service.Default.Models; -using Client.Structure.Service.Renamed.Operation; using NUnit.Framework; namespace TestProjects.CadlRanch.Tests.Http.Client.Structure.Default diff --git a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Payload/Multipart/MultipartTests.cs b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Payload/Multipart/MultipartTests.cs index 1f3541ad25..6519fe2b0d 100644 --- a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Payload/Multipart/MultipartTests.cs +++ b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Payload/Multipart/MultipartTests.cs @@ -1,7 +1,13 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using System.ClientModel; +using System.Globalization; +using System; using System.IO; +using System.Net.Http.Headers; +using System.Net.Http; +using System.Threading; using System.Threading.Tasks; using NUnit.Framework; using Payload.MultiPart; @@ -219,5 +225,185 @@ private Task MultiBinaryParts(bool hasPicture) => Test(async (host) => var response = await new MultiPartClient(host, null).GetFormDataClient().MultiBinaryPartsAsync(content, content.ContentType, null); Assert.AreEqual(204, response.GetRawResponse().Status); }); + + internal partial class MultiPartFormDataBinaryContent : BinaryContent + { + private readonly MultipartFormDataContent _multipartContent; + private static readonly Random _random = new Random(); + private static readonly char[] _boundaryValues = "0123456789=ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz".ToCharArray(); + + public MultiPartFormDataBinaryContent() + { + _multipartContent = new MultipartFormDataContent(CreateBoundary()); + } + + public string? ContentType + { + get + { + return _multipartContent.Headers.ContentType?.ToString(); + } + } + + internal HttpContent HttpContent => _multipartContent; + + private static string CreateBoundary() + { + Span chars = new char[70]; + byte[] random = new byte[70]; + _random.NextBytes(random); + int mask = 255 >> 2; + int i = 0; + for (; i < 70; i++) + { + chars[i] = _boundaryValues[random[i] & mask]; + } + return chars.ToString(); + } + + public void Add(string content, string name, string? filename = default, string? contentType = default) + { + ArgumentNullException.ThrowIfNull(content, nameof(content)); + ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name)); + + Add(new StringContent(content), name, filename, contentType); + } + + public void Add(int content, string name, string? filename = default, string? contentType = default) + { + ArgumentNullException.ThrowIfNull(content, nameof(content)); + ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name)); + + string value = content.ToString("G", CultureInfo.InvariantCulture); + Add(new StringContent(value), name, filename, contentType); + } + + public void Add(long content, string name, string? filename = default, string? contentType = default) + { + ArgumentNullException.ThrowIfNull(content, nameof(content)); + ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name)); + + string value = content.ToString("G", CultureInfo.InvariantCulture); + Add(new StringContent(value), name, filename, contentType); + } + + public void Add(float content, string name, string? filename = default, string? contentType = default) + { + ArgumentNullException.ThrowIfNull(content, nameof(content)); + ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name)); + + string value = content.ToString("G", CultureInfo.InvariantCulture); + Add(new StringContent(value), name, filename, contentType); + } + + public void Add(double content, string name, string? filename = default, string? contentType = default) + { + ArgumentNullException.ThrowIfNull(content, nameof(content)); + ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name)); + + string value = content.ToString("G", CultureInfo.InvariantCulture); + Add(new StringContent(value), name, filename, contentType); + } + + public void Add(decimal content, string name, string? filename = default, string? contentType = default) + { + ArgumentNullException.ThrowIfNull(content, nameof(content)); + ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name)); + + string value = content.ToString("G", CultureInfo.InvariantCulture); + Add(new StringContent(value), name, filename, contentType); + } + + public void Add(bool content, string name, string? filename = default, string? contentType = default) + { + ArgumentNullException.ThrowIfNull(content, nameof(content)); + ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name)); + + string value = content ? "true" : "false"; + Add(new StringContent(value), name, filename, contentType); + } + + public void Add(Stream content, string name, string? filename = default, string? contentType = default) + { + ArgumentNullException.ThrowIfNull(content, nameof(content)); + ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name)); + + Add(new StreamContent(content), name, filename, contentType); + } + + public void Add(byte[] content, string name, string? filename = default, string? contentType = default) + { + ArgumentNullException.ThrowIfNull(content, nameof(content)); + ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name)); + + Add(new ByteArrayContent(content), name, filename, contentType); + } + + public void Add(BinaryData content, string name, string? filename = default, string? contentType = default) + { + ArgumentNullException.ThrowIfNull(content, nameof(content)); + ArgumentNullException.ThrowIfNullOrEmpty(name, nameof(name)); + + Add(new ByteArrayContent(content.ToArray()), name, filename, contentType); + } + + private void Add(HttpContent content, string name, string? filename, string? contentType) + { + if (contentType != null) + { + ArgumentNullException.ThrowIfNullOrEmpty(contentType, nameof(contentType)); + AddContentTypeHeader(content, contentType); + } + if (filename != null) + { + ArgumentNullException.ThrowIfNullOrEmpty(filename, nameof(filename)); + _multipartContent.Add(content, name, filename); + } + else + { + _multipartContent.Add(content, name); + } + } + + public static void AddContentTypeHeader(HttpContent content, string contentType) + { + MediaTypeHeaderValue header = new MediaTypeHeaderValue(contentType); + content.Headers.ContentType = header; + } + + public override bool TryComputeLength(out long length) + { + if (_multipartContent.Headers.ContentLength is long contentLength) + { + length = contentLength; + return true; + } + length = 0; + return false; + } + + public override void WriteTo(Stream stream, CancellationToken cancellationToken = default) + { +#if NET6_0_OR_GREATER + _multipartContent.CopyTo(stream, default, cancellationToken); +#else + _multipartContent.CopyToAsync(stream).GetAwaiter().GetResult(); +#endif + } + + public override async Task WriteToAsync(Stream stream, CancellationToken cancellationToken = default) + { +#if NET6_0_OR_GREATER + await _multipartContent.CopyToAsync(stream).ConfigureAwait(false); +#else + await _multipartContent.CopyToAsync(stream).ConfigureAwait(false); +#endif + } + + public override void Dispose() + { + _multipartContent.Dispose(); + } + } } } diff --git a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/Added/V1/VersioningAddedV1Tests.cs b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/Added/V1/VersioningAddedV1Tests.cs index 785e980779..90270620e3 100644 --- a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/Added/V1/VersioningAddedV1Tests.cs +++ b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/Added/V1/VersioningAddedV1Tests.cs @@ -1,11 +1,13 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -using NUnit.Framework; +extern alias VersioningAddedV1; + using System; using System.Linq; -using Versioning.Added.V1; -using Versioning.Added.V1.Models; +using NUnit.Framework; +using VersioningAddedV1::Versioning.Added.V1; +using VersioningAddedV1::Versioning.Added.V1.Models; namespace TestProjects.CadlRanch.Tests.Http.Versioning.Added.V1 { diff --git a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/Added/V2/VersioningAddedV2Tests.cs b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/Added/V2/VersioningAddedV2Tests.cs index ace5b75b5d..c7408f86c1 100644 --- a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/Added/V2/VersioningAddedV2Tests.cs +++ b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/Added/V2/VersioningAddedV2Tests.cs @@ -1,12 +1,14 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -using System.Threading.Tasks; -using NUnit.Framework; +extern alias VersioningAddedV2; + using System; using System.Linq; -using Versioning.Added.V2; -using Versioning.Added.V2.Models; +using System.Threading.Tasks; +using NUnit.Framework; +using VersioningAddedV2::Versioning.Added.V2; +using VersioningAddedV2::Versioning.Added.V2.Models; namespace TestProjects.CadlRanch.Tests.Http.Versioning.Added.V2 { @@ -31,10 +33,12 @@ public void TestAddedMembersV2Client() Assert.IsTrue(enumValues.Contains("EnumMemberV2")); /* check existence of the added model ModelV2. */ - Assert.IsNotNull(Type.GetType("Versioning.Added.V2.Models.ModelV2")); + var modelV2Type = typeof(ModelV1).Assembly.GetType("Versioning.Added.V2.Models.ModelV2"); + Assert.IsNotNull(modelV2Type); /* check existence of the added enum EnumV2. */ - Assert.IsNotNull(Type.GetType("Versioning.Added.V2.Models.EnumV2")); + var enumV2Type = typeof(ModelV1).Assembly.GetType("Versioning.Added.V2.Models.EnumV2"); + Assert.IsNotNull(enumV2Type); /* check the added parameter. */ var methods = typeof(AddedClient).GetMethods().Where(m => m.Name == "V1" || m.Name == "V1Async"); @@ -52,7 +56,8 @@ public void TestAddedMembersV2Client() Assert.AreEqual(4, addedMethods.Count()); /* check the existence of added interface in V2. */ - Assert.IsNotNull(Type.GetType("Versioning.Added.V2.InterfaceV2")); + var interfaceV2Type = typeof(ModelV1).Assembly.GetType("Versioning.Added.V2.InterfaceV2"); + Assert.IsNotNull(interfaceV2Type); } [CadlRanchTest] diff --git a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/Removed/V1/VersioningRemovedV1Tests.cs b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/Removed/V1/VersioningRemovedV1Tests.cs index d96e2a4292..fb0b4939aa 100644 --- a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/Removed/V1/VersioningRemovedV1Tests.cs +++ b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/Removed/V1/VersioningRemovedV1Tests.cs @@ -15,11 +15,14 @@ public class VersioningRemovedV1Tests : CadlRanchTestBase [CadlRanchTest] public void TestRemovedMembers() { + var assembly = typeof(RemovedClient).Assembly; /* check existence of the removed model ModelV1. */ - Assert.IsNotNull(Type.GetType("Versioning.Removed.V1.Models.ModelV1")); + var modelV1Type = assembly.GetType("Versioning.Removed.V1.Models.ModelV1"); + Assert.IsNotNull(modelV1Type); /* check existence of the removed enum EnumV1. */ - Assert.IsNotNull(Type.GetType("Versioning.Removed.V1.Models.EnumV1")); + var enumV1Type = assembly.GetType("Versioning.Removed.V1.Models.EnumV1"); + Assert.IsNotNull(enumV1Type); /* check existence of removed method V1 */ var removedMethods = typeof(RemovedClient).GetMethods().Where(m => m.Name == "V1" || m.Name == "V1Async"); @@ -35,7 +38,8 @@ public void TestRemovedMembers() } /* check existence of removed interface. */ - Assert.IsNotNull(Type.GetType("Versioning.Removed.V1.InterfaceV1")); + var interfaceV1Type = assembly.GetType("Versioning.Removed.V1.InterfaceV1"); + Assert.IsNotNull(interfaceV1Type); // Only initial versions is defined var enumType = typeof(RemovedClientOptions.ServiceVersion); diff --git a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/RenamedFrom/V1/VersioningRenamedFromV1Tests.cs b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/RenamedFrom/V1/VersioningRenamedFromV1Tests.cs deleted file mode 100644 index 2b17334cf4..0000000000 --- a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/RenamedFrom/V1/VersioningRenamedFromV1Tests.cs +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using NUnit.Framework; -using System; -using System.Linq; -using Versioning.RenamedFrom.V2; -using Versioning.RenamedFrom.V2.Models; - -namespace TestProjects.CadlRanch.Tests.Http.Versioning.RenamedFrom.V1 -{ - public class VersioningRenamedFromTests : CadlRanchTestBase - { - [CadlRanchTest] - public void TestRenamedMembers() - { - Assert.IsNotNull(Type.GetType("Versioning.RenamedFrom.V1.Models.OldModel")); - Assert.IsNull(Type.GetType("Versioning.RenamedFrom.V1.Models.NewModel")); - - /* check the renamed property of model */ - var properties = typeof(NewModel).GetProperties(); - Assert.IsNotNull(properties); - Assert.AreEqual(3, properties.Length); - Assert.IsNull(typeof(NewModel).GetProperty("OldProp")); - Assert.IsNotNull(typeof(NewModel).GetProperty("NewProp")); - - /* check the renamed enum from `OldEnum` to `NewEnum` */ - Assert.IsNotNull(Type.GetType("Versioning.RenamedFrom.V1.Models.OldEnum")); - Assert.IsNull(Type.GetType("Versioning.RenamedFrom.V1.Models.NewEnum")); - - /* check the renamed enum value */ - var enumValues = typeof(NewEnum).GetEnumNames(); - Assert.IsNotNull(enumValues); - Assert.AreEqual(1, enumValues.Length); - Assert.IsFalse(enumValues.Contains("OldEnumMember")); - Assert.IsTrue(enumValues.Contains("NewEnumMember")); - - /* check the renamed operation */ - var oldMethods = typeof(RenamedFromClient).GetMethods().Where(m => m.Name == "OldOp" || m.Name == "OldOpAsync"); - Assert.AreEqual(0, oldMethods.Count()); - var newMethods = typeof(RenamedFromClient).GetMethods().Where(m => m.Name == "NewOp" || m.Name == "NewOpAsync"); - Assert.AreEqual(4, newMethods.Count()); - - /* check the renamed interface */ - Assert.IsNotNull(Type.GetType("Versioning.RenamedFrom.V1.OldInterface")); - Assert.IsNull(Type.GetType("Versioning.RenamedFrom.V1.NewInterface")); - } - } -} diff --git a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/RenamedFrom/V2/VersioningRenamedFromV2Tests.cs b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/RenamedFrom/V2/VersioningRenamedFromV2Tests.cs index f5fe18224f..781364e03e 100644 --- a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/RenamedFrom/V2/VersioningRenamedFromV2Tests.cs +++ b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/Versioning/RenamedFrom/V2/VersioningRenamedFromV2Tests.cs @@ -15,9 +15,12 @@ public class VersioningRenamedFromTests : CadlRanchTestBase [CadlRanchTest] public void TestRenamedMembers() { + var assembly = typeof(RenamedFromClient).Assembly; /* check the renamed model from `OldModel` to `NewModel` */ - Assert.IsNull(Type.GetType("Versioning.RenamedFrom.V2.Models.OldModel")); - Assert.IsNotNull(Type.GetType("Versioning.RenamedFrom.V2.Models.NewModel")); + var oldModel = assembly.GetType("Versioning.RenamedFrom.V2.Models.OldModel"); + Assert.IsNull(oldModel); + var newModel = assembly.GetType("Versioning.RenamedFrom.V2.Models.NewModel"); + Assert.IsNotNull(newModel); /* check the renamed property of model */ var properties = typeof(NewModel).GetProperties(); @@ -27,8 +30,10 @@ public void TestRenamedMembers() Assert.IsNotNull(typeof(NewModel).GetProperty("NewProp")); /* check the renamed enum from `OldEnum` to `NewEnum` */ - Assert.IsNull(Type.GetType("Versioning.RenamedFrom.V2.Models.OldEnum")); - Assert.IsNotNull(Type.GetType("Versioning.RenamedFrom.V2.Models.NewEnum")); + var oldEnum = assembly.GetType("Versioning.RenamedFrom.V2.Models.OldEnum"); + Assert.IsNull(oldEnum); + var newEnum = assembly.GetType("Versioning.RenamedFrom.V2.Models.NewEnum"); + Assert.IsNotNull(newEnum); /* check the renamed enum value */ var enumValues = typeof(NewEnum).GetEnumNames(); @@ -44,8 +49,10 @@ public void TestRenamedMembers() Assert.AreEqual(4, newMethods.Count()); /* check the renamed interface */ - Assert.IsNull(Type.GetType("Versioning.RenamedFrom.V2.OldInterface")); - Assert.IsNotNull(Type.GetType("Versioning.RenamedFrom.V2.NewInterface")); + var oldInterface = assembly.GetType("Versioning.RenamedFrom.V2.OldInterface"); + Assert.IsNull(oldInterface); + var newInterface = assembly.GetType("Versioning.RenamedFrom.V2.NewInterface"); + Assert.IsNotNull(newInterface); } [CadlRanchTest] diff --git a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/_Type/Model/Inheritance/EnumDiscriminator/EnumDiscriminatorTest.cs b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/_Type/Model/Inheritance/EnumDiscriminator/EnumDiscriminatorTest.cs index c4506b66ce..4c69f8b14f 100644 --- a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/_Type/Model/Inheritance/EnumDiscriminator/EnumDiscriminatorTest.cs +++ b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/_Type/Model/Inheritance/EnumDiscriminator/EnumDiscriminatorTest.cs @@ -2,6 +2,8 @@ // Licensed under the MIT License. using System; +using System.Linq; +using System.Reflection; using System.Threading.Tasks; using _Type.Model.Inheritance.EnumDiscriminator; using _Type.Model.Inheritance.EnumDiscriminator.Models; @@ -41,7 +43,10 @@ public Task GetExtensibleModelWrongDiscriminator() => Test(async (host) => { var result = await new EnumDiscriminatorClient(host, null).GetExtensibleModelWrongDiscriminatorAsync(); Assert.AreEqual(200, result.GetRawResponse().Status); - Assert.IsInstanceOf(result.Value); + + var unknownDogType = typeof(Dog).Assembly.DefinedTypes.FirstOrDefault(t => t.Name == "UnknownDog"); + Assert.IsNotNull(unknownDogType); + Assert.AreEqual(unknownDogType, result.Value.GetType()); Assert.AreEqual(8, result.Value.Weight); }); @@ -66,7 +71,10 @@ public Task GetFixedModelMissingDiscriminator() => Test(async (host) => { var response = await new EnumDiscriminatorClient(host, null).GetFixedModelMissingDiscriminatorAsync(); Assert.AreEqual(200, response.GetRawResponse().Status); - Assert.IsInstanceOf(response.Value); + + var unknownSnakeType = typeof(Snake).Assembly.GetTypes().FirstOrDefault(t => t.Name == "UnknownSnake"); + Assert.IsNotNull(unknownSnakeType); + Assert.AreEqual(unknownSnakeType, response.Value.GetType()); Assert.AreEqual(10, response.Value.Length); }); diff --git a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/_Type/Model/Inheritance/NestedDiscriminator/NestedDiscriminatorTests.cs b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/_Type/Model/Inheritance/NestedDiscriminator/NestedDiscriminatorTests.cs index 344a4891b2..c0e04425b8 100644 --- a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/_Type/Model/Inheritance/NestedDiscriminator/NestedDiscriminatorTests.cs +++ b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/_Type/Model/Inheritance/NestedDiscriminator/NestedDiscriminatorTests.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using System.Linq; +using System.Reflection; using System.Threading.Tasks; using _Type.Model.Inheritance.NestedDiscriminator; using _Type.Model.Inheritance.NestedDiscriminator.Models; @@ -37,7 +39,8 @@ public Task GetRecursiveModel() => Test(async (host) => Assert.IsTrue(salmon.Partner is Shark); var shark = (Shark)salmon.Partner; - Assert.AreEqual("saw", shark.Sharktype); + var sharkTypeProperty = shark.GetType().GetProperty("Sharktype", BindingFlags.Instance | BindingFlags.NonPublic); + Assert.AreEqual("saw", sharkTypeProperty?.GetValue(shark)); Assert.IsInstanceOf(shark); Assert.AreEqual(2, salmon.Friends.Count); Assert.IsInstanceOf(salmon.Friends[0]); @@ -55,9 +58,13 @@ public Task GetRecursiveModel() => Test(async (host) => public Task GetWrongDiscriminator() => Test(async (host) => { var result = await new NestedDiscriminatorClient(host, null).GetWrongDiscriminatorAsync(); - Assert.IsInstanceOf(result.Value); + + var unknownFishType = typeof(Fish).Assembly.GetTypes().FirstOrDefault(t => t.Name == "UnknownFish"); + Assert.IsNotNull(unknownFishType); + Assert.AreEqual(unknownFishType, result.Value.GetType()); Assert.AreEqual(1, result.Value.Age); - Assert.AreEqual("wrongKind", result.Value.Kind); + var kindProperty = result.Value.GetType().GetProperty("Kind", BindingFlags.Instance | BindingFlags.NonPublic); + Assert.AreEqual("wrongKind", kindProperty?.GetValue(result.Value)); }); [CadlRanchTest] diff --git a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/_Type/Model/Inheritance/SingleDiscriminator/SingleDiscriminatorTests.cs b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/_Type/Model/Inheritance/SingleDiscriminator/SingleDiscriminatorTests.cs index 13b993b2a8..2f075a9de0 100644 --- a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/_Type/Model/Inheritance/SingleDiscriminator/SingleDiscriminatorTests.cs +++ b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/_Type/Model/Inheritance/SingleDiscriminator/SingleDiscriminatorTests.cs @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -using System.Collections.Generic; +using System.Reflection; using System.Threading.Tasks; using _Type.Model.Inheritance.SingleDiscriminator; using _Type.Model.Inheritance.SingleDiscriminator.Models; @@ -51,7 +51,15 @@ public Task GetRecursiveModel() => Test(async (host) => [CadlRanchTest] public Task PutRecursiveModel() => Test(async (host) => { - var body = new Eagle("eagle", 5, null, new[] { new SeaGull(2) }, new Dictionary { { "key3", new Sparrow(1) } }, new Goose(2)); + var body = new Eagle(5) + { + Friends = { new SeaGull(2) }, + Hate = + { + ["key3"] = new Sparrow(1) + }, + Partner = new Goose(2) + }; var response = await new SingleDiscriminatorClient(host, null).PutRecursiveModelAsync(body); Assert.AreEqual(204, response.GetRawResponse().Status); }); @@ -61,9 +69,13 @@ public Task GetMissingDiscriminator() => Test(async (host) => { var result = await new SingleDiscriminatorClient(host, null).GetMissingDiscriminatorAsync(); Assert.IsTrue(result.Value is Bird); - Assert.IsTrue(result.Value is UnknownBird); + + var unknownBirdType = typeof(Bird).Assembly.GetType("_Type.Model.Inheritance.SingleDiscriminator.Models.UnknownBird"); + Assert.AreEqual(unknownBirdType, result.Value.GetType()); Assert.AreEqual(1, result.Value.Wingspan); - Assert.AreEqual("unknown", result.Value.Kind); + + var kindProperty = result.Value.GetType().GetProperty("Kind", BindingFlags.Instance | BindingFlags.NonPublic); + Assert.AreEqual("unknown", kindProperty?.GetValue(result.Value)); }); [CadlRanchTest] @@ -71,9 +83,13 @@ public Task GetWrongDiscriminator() => Test(async (host) => { var result = await new SingleDiscriminatorClient(host, null).GetWrongDiscriminatorAsync(); Assert.IsTrue(result.Value is Bird); - Assert.IsTrue(result.Value is UnknownBird); + + var unknownBirdType = typeof(Bird).Assembly.GetType("_Type.Model.Inheritance.SingleDiscriminator.Models.UnknownBird"); + Assert.AreEqual(unknownBirdType, result.Value.GetType()); Assert.AreEqual(1, result.Value.Wingspan); - Assert.AreEqual("wrongKind", result.Value.Kind); + + var kindProperty = result.Value.GetType().GetProperty("Kind", BindingFlags.Instance | BindingFlags.NonPublic); + Assert.AreEqual("wrongKind", kindProperty?.GetValue(result.Value)); }); [CadlRanchTest] diff --git a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/_Type/Property/AdditionalProperties/AdditionalPropertiesTests.cs b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/_Type/Property/AdditionalProperties/AdditionalPropertiesTests.cs index bc0ec20317..13ccf4bc04 100644 --- a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/_Type/Property/AdditionalProperties/AdditionalPropertiesTests.cs +++ b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/Http/_Type/Property/AdditionalProperties/AdditionalPropertiesTests.cs @@ -10,6 +10,7 @@ using System.Threading.Tasks; using _Type.Property.AdditionalProperties.Models; using _Type.Property.AdditionalProperties; +using System.Reflection; namespace TestProjects.CadlRanch.Tests.Http._Type.Property.AdditionalProperties { @@ -314,7 +315,8 @@ public Task ExtendsUnknownDiscriminatedGet() => Test(async (host) => var value = response.Value; Assert.AreEqual(200, response.GetRawResponse().Status); Assert.AreEqual("Derived", value.Name); - Assert.AreEqual("derived", value.Kind); + var kindProperty = value.GetType().GetProperty("Kind", BindingFlags.Instance | BindingFlags.NonPublic); + Assert.AreEqual("derived", kindProperty?.GetValue(value)); var derived = value as ExtendsUnknownAdditionalPropertiesDiscriminatedDerived; Assert.IsNotNull(derived); Assert.AreEqual(314, derived!.Index); @@ -416,7 +418,8 @@ public Task IsUnknownDiscriminatedGet() => Test(async (host) => var value = response.Value; Assert.AreEqual(200, response.GetRawResponse().Status); Assert.AreEqual("Derived", value.Name); - Assert.AreEqual("derived", value.Kind); + var kindProperty = value.GetType().GetProperty("Kind", BindingFlags.Instance | BindingFlags.NonPublic); + Assert.AreEqual("derived", kindProperty?.GetValue(value)); var derived = value as IsUnknownAdditionalPropertiesDiscriminatedDerived; Assert.IsNotNull(derived); Assert.AreEqual(314, derived!.Index); diff --git a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/TestProjects.CadlRanch.Tests.csproj b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/TestProjects.CadlRanch.Tests.csproj index 5a90145fb4..bf66de5a56 100644 --- a/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/TestProjects.CadlRanch.Tests.csproj +++ b/packages/http-client-csharp/generator/TestProjects/CadlRanch.Tests/TestProjects.CadlRanch.Tests.csproj @@ -6,10 +6,6 @@ - - - - <_Parameter1>$(RepoRoot) @@ -19,6 +15,71 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +