From 5661859338ea396acf620a2ae1314de414160fd6 Mon Sep 17 00:00:00 2001 From: Nisha Bhatia <67986960+nisha-bhatia@users.noreply.github.com> Date: Thu, 15 Aug 2024 12:40:19 -0700 Subject: [PATCH] Update ModelFactory List/Dictionary Instantiation (#4188) Resolves: https://github.com/microsoft/typespec/issues/4129 --------- Co-authored-by: Dapeng Zhang --- .../src/Providers/ModelFactoryProvider.cs | 8 ++--- .../UnbrandedTypeSpecModelFactory.cs | 29 ++++++++++--------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ModelFactoryProvider.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ModelFactoryProvider.cs index d0a98fd7ed..43b7ad0393 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ModelFactoryProvider.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ModelFactoryProvider.cs @@ -124,13 +124,9 @@ private IReadOnlyList GetCollectionInitialization(MethodSig var statements = new List(); foreach (var param in signature.Parameters) { - if (param.Type.IsList) - { - statements.Add(param.Assign(New.Instance(new CSharpType(typeof(List<>), param.Type.Arguments))).Terminate()); - } - else if (param.Type.IsDictionary) + if (param.Type.IsList || param.Type.IsDictionary) { - statements.Add(param.Assign(New.Instance(new CSharpType(typeof(Dictionary<,>), param.Type.Arguments))).Terminate()); + statements.Add(param.Assign(New.Instance(param.Type.PropertyInitializationType)).Terminate()); } } return [.. statements]; diff --git a/packages/http-client-csharp/generator/TestProjects/Local/Unbranded-TypeSpec/src/Generated/UnbrandedTypeSpecModelFactory.cs b/packages/http-client-csharp/generator/TestProjects/Local/Unbranded-TypeSpec/src/Generated/UnbrandedTypeSpecModelFactory.cs index af5d4685e3..9bb2595eff 100644 --- a/packages/http-client-csharp/generator/TestProjects/Local/Unbranded-TypeSpec/src/Generated/UnbrandedTypeSpecModelFactory.cs +++ b/packages/http-client-csharp/generator/TestProjects/Local/Unbranded-TypeSpec/src/Generated/UnbrandedTypeSpecModelFactory.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; +using UnbrandedTypeSpec; namespace UnbrandedTypeSpec.Models { @@ -28,8 +29,8 @@ public static partial class UnbrandedTypeSpecModelFactory /// A new instance for mocking. public static Thing Thing(string name = default, BinaryData requiredUnion = default, ThingRequiredLiteralString requiredLiteralString = default, ThingRequiredLiteralInt requiredLiteralInt = default, ThingRequiredLiteralFloat requiredLiteralFloat = default, bool requiredLiteralBool = default, ThingOptionalLiteralString? optionalLiteralString = default, ThingOptionalLiteralInt? optionalLiteralInt = default, ThingOptionalLiteralFloat? optionalLiteralFloat = default, bool? optionalLiteralBool = default, string requiredBadDescription = default, IEnumerable optionalNullableList = default, IEnumerable requiredNullableList = default) { - optionalNullableList = new List(); - requiredNullableList = new List(); + optionalNullableList = new ChangeTrackingList(); + requiredNullableList = new ChangeTrackingList(); return new Thing( name, @@ -76,16 +77,16 @@ public static Thing Thing(string name = default, BinaryData requiredUnion = defa /// A new instance for mocking. public static RoundTripModel RoundTripModel(string requiredString = default, int requiredInt = default, IEnumerable requiredCollection = default, IDictionary requiredDictionary = default, Thing requiredModel = default, IntExtensibleEnum? intExtensibleEnum = default, IEnumerable intExtensibleEnumCollection = default, FloatExtensibleEnum? floatExtensibleEnum = default, FloatExtensibleEnumWithIntValue? floatExtensibleEnumWithIntValue = default, IEnumerable floatExtensibleEnumCollection = default, FloatFixedEnum? floatFixedEnum = default, FloatFixedEnumWithIntValue? floatFixedEnumWithIntValue = default, IEnumerable floatFixedEnumCollection = default, IntFixedEnum? intFixedEnum = default, IEnumerable intFixedEnumCollection = default, StringFixedEnum? stringFixedEnum = default, BinaryData requiredUnknown = default, BinaryData optionalUnknown = default, IDictionary requiredRecordUnknown = default, IDictionary optionalRecordUnknown = default, IReadOnlyDictionary readOnlyRequiredRecordUnknown = default, IReadOnlyDictionary readOnlyOptionalRecordUnknown = default, ModelWithRequiredNullableProperties modelWithRequiredNullable = default, BinaryData requiredBytes = default) { - requiredCollection = new List(); - requiredDictionary = new Dictionary(); - intExtensibleEnumCollection = new List(); - floatExtensibleEnumCollection = new List(); - floatFixedEnumCollection = new List(); - intFixedEnumCollection = new List(); - requiredRecordUnknown = new Dictionary(); - optionalRecordUnknown = new Dictionary(); - readOnlyRequiredRecordUnknown = new Dictionary(); - readOnlyOptionalRecordUnknown = new Dictionary(); + requiredCollection = new ChangeTrackingList(); + requiredDictionary = new ChangeTrackingDictionary(); + intExtensibleEnumCollection = new ChangeTrackingList(); + floatExtensibleEnumCollection = new ChangeTrackingList(); + floatFixedEnumCollection = new ChangeTrackingList(); + intFixedEnumCollection = new ChangeTrackingList(); + requiredRecordUnknown = new ChangeTrackingDictionary(); + optionalRecordUnknown = new ChangeTrackingDictionary(); + readOnlyRequiredRecordUnknown = new ChangeTrackingDictionary(); + readOnlyOptionalRecordUnknown = new ChangeTrackingDictionary(); return new RoundTripModel( requiredString, @@ -143,8 +144,8 @@ public static ModelWithRequiredNullableProperties ModelWithRequiredNullablePrope /// A new instance for mocking. public static AnonymousBodyRequest AnonymousBodyRequest(string name = default, BinaryData requiredUnion = default, AnonymousBodyRequestRequiredLiteralString requiredLiteralString = default, AnonymousBodyRequestRequiredLiteralInt requiredLiteralInt = default, AnonymousBodyRequestRequiredLiteralFloat requiredLiteralFloat = default, bool requiredLiteralBool = default, AnonymousBodyRequestOptionalLiteralString? optionalLiteralString = default, AnonymousBodyRequestOptionalLiteralInt? optionalLiteralInt = default, AnonymousBodyRequestOptionalLiteralFloat? optionalLiteralFloat = default, bool? optionalLiteralBool = default, string requiredBadDescription = default, IEnumerable optionalNullableList = default, IEnumerable requiredNullableList = default) { - optionalNullableList = new List(); - requiredNullableList = new List(); + optionalNullableList = new ChangeTrackingList(); + requiredNullableList = new ChangeTrackingList(); return new AnonymousBodyRequest( name,