Skip to content

Commit

Permalink
Update ModelFactory List/Dictionary Instantiation (#4188)
Browse files Browse the repository at this point in the history
Resolves: #4129

---------

Co-authored-by: Dapeng Zhang <[email protected]>
  • Loading branch information
nisha-bhatia and ArcturusZhang authored Aug 15, 2024
1 parent 1dc5711 commit 5661859
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,9 @@ private IReadOnlyList<MethodBodyStatement> GetCollectionInitialization(MethodSig
var statements = new List<MethodBodyStatement>();
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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnbrandedTypeSpec;

namespace UnbrandedTypeSpec.Models
{
Expand All @@ -28,8 +29,8 @@ public static partial class UnbrandedTypeSpecModelFactory
/// <returns> A new <see cref="Models.Thing"/> instance for mocking. </returns>
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<int> optionalNullableList = default, IEnumerable<int> requiredNullableList = default)
{
optionalNullableList = new List<int>();
requiredNullableList = new List<int>();
optionalNullableList = new ChangeTrackingList<int>();
requiredNullableList = new ChangeTrackingList<int>();

return new Thing(
name,
Expand Down Expand Up @@ -76,16 +77,16 @@ public static Thing Thing(string name = default, BinaryData requiredUnion = defa
/// <returns> A new <see cref="Models.RoundTripModel"/> instance for mocking. </returns>
public static RoundTripModel RoundTripModel(string requiredString = default, int requiredInt = default, IEnumerable<StringFixedEnum> requiredCollection = default, IDictionary<string, StringExtensibleEnum> requiredDictionary = default, Thing requiredModel = default, IntExtensibleEnum? intExtensibleEnum = default, IEnumerable<IntExtensibleEnum> intExtensibleEnumCollection = default, FloatExtensibleEnum? floatExtensibleEnum = default, FloatExtensibleEnumWithIntValue? floatExtensibleEnumWithIntValue = default, IEnumerable<FloatExtensibleEnum> floatExtensibleEnumCollection = default, FloatFixedEnum? floatFixedEnum = default, FloatFixedEnumWithIntValue? floatFixedEnumWithIntValue = default, IEnumerable<FloatFixedEnum> floatFixedEnumCollection = default, IntFixedEnum? intFixedEnum = default, IEnumerable<IntFixedEnum> intFixedEnumCollection = default, StringFixedEnum? stringFixedEnum = default, BinaryData requiredUnknown = default, BinaryData optionalUnknown = default, IDictionary<string, BinaryData> requiredRecordUnknown = default, IDictionary<string, BinaryData> optionalRecordUnknown = default, IReadOnlyDictionary<string, BinaryData> readOnlyRequiredRecordUnknown = default, IReadOnlyDictionary<string, BinaryData> readOnlyOptionalRecordUnknown = default, ModelWithRequiredNullableProperties modelWithRequiredNullable = default, BinaryData requiredBytes = default)
{
requiredCollection = new List<StringFixedEnum>();
requiredDictionary = new Dictionary<string, StringExtensibleEnum>();
intExtensibleEnumCollection = new List<IntExtensibleEnum>();
floatExtensibleEnumCollection = new List<FloatExtensibleEnum>();
floatFixedEnumCollection = new List<FloatFixedEnum>();
intFixedEnumCollection = new List<IntFixedEnum>();
requiredRecordUnknown = new Dictionary<string, BinaryData>();
optionalRecordUnknown = new Dictionary<string, BinaryData>();
readOnlyRequiredRecordUnknown = new Dictionary<string, BinaryData>();
readOnlyOptionalRecordUnknown = new Dictionary<string, BinaryData>();
requiredCollection = new ChangeTrackingList<StringFixedEnum>();
requiredDictionary = new ChangeTrackingDictionary<string, StringExtensibleEnum>();
intExtensibleEnumCollection = new ChangeTrackingList<IntExtensibleEnum>();
floatExtensibleEnumCollection = new ChangeTrackingList<FloatExtensibleEnum>();
floatFixedEnumCollection = new ChangeTrackingList<FloatFixedEnum>();
intFixedEnumCollection = new ChangeTrackingList<IntFixedEnum>();
requiredRecordUnknown = new ChangeTrackingDictionary<string, BinaryData>();
optionalRecordUnknown = new ChangeTrackingDictionary<string, BinaryData>();
readOnlyRequiredRecordUnknown = new ChangeTrackingDictionary<string, BinaryData>();
readOnlyOptionalRecordUnknown = new ChangeTrackingDictionary<string, BinaryData>();

return new RoundTripModel(
requiredString,
Expand Down Expand Up @@ -143,8 +144,8 @@ public static ModelWithRequiredNullableProperties ModelWithRequiredNullablePrope
/// <returns> A new <see cref="Models.AnonymousBodyRequest"/> instance for mocking. </returns>
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<int> optionalNullableList = default, IEnumerable<int> requiredNullableList = default)
{
optionalNullableList = new List<int>();
requiredNullableList = new List<int>();
optionalNullableList = new ChangeTrackingList<int>();
requiredNullableList = new ChangeTrackingList<int>();

return new AnonymousBodyRequest(
name,
Expand Down

0 comments on commit 5661859

Please sign in to comment.