Skip to content

Commit e765aad

Browse files
authored
Refine how to handle nullability in model factory (#53693)
* Refine how to handle nullability in model factory * more null additionalProperties * refine
1 parent ab76c03 commit e765aad

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

eng/packages/http-client-csharp-mgmt/generator/Azure.Generator.Management/src/Visitors/FlattenPropertyVisitor.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ private ValueExpression BuildConditionExpression(List<FlattenPropertyInfo> flatt
168168
}
169169
else
170170
{
171-
result = result.Or(updatedParameter.Is(Null));
171+
result = result.And(updatedParameter.Is(Null));
172172
}
173173
}
174174
else
@@ -179,7 +179,7 @@ private ValueExpression BuildConditionExpression(List<FlattenPropertyInfo> flatt
179179
}
180180
else
181181
{
182-
result = result.Or(propertyParameter.Is(Null));
182+
result = result.And(propertyParameter.Is(Null));
183183
}
184184
}
185185
}
@@ -205,7 +205,7 @@ private ValueExpression[] BuildConstructorParameters(CSharpType propertyType, Li
205205
if (fullConstructorParameterIndex == additionalPropertyIndex)
206206
{
207207
// If the additionalProperties parameter exists, we need to pass a new instance for it.
208-
parameters.Add(New.Instance(new CSharpType(typeof(Dictionary<string, BinaryData>))));
208+
parameters.Add(Null);
209209

210210
// If the additionalProperties parameter is the last parameter, we can break the loop.
211211
if (fullConstructorParameterIndex == fullConstructorParameters.Count - 1)
@@ -232,7 +232,7 @@ private ValueExpression[] BuildConstructorParameters(CSharpType propertyType, Li
232232

233233
parameters.Add(isOverriddenValueType
234234
? parameter.Property("Value")
235-
: IsNonReadOnlyMemoryList(parameter) ? parameter.ToList() : parameter);
235+
: IsNonReadOnlyMemoryList(parameter) ? parameter.NullCoalesce(New.Instance(ManagementClientGenerator.Instance.TypeFactory.ListInitializationType.MakeGenericType(parameter.Type.Arguments))).ToList() : parameter);
236236

237237
// only increase flattenedPropertyIndex when we use a flattened property
238238
flattenedPropertyIndex++;
@@ -254,7 +254,7 @@ private ValueExpression[] BuildConstructorParameters(CSharpType propertyType, Li
254254
// If the additionalProperties parameter is missing at the end, we need to pass a new instance for it.
255255
if (parameters.Count < fullConstructorParameters.Count && additionalPropertyIndex == propertyModelType!.FullConstructor.Signature.Parameters.Count - 1)
256256
{
257-
parameters.Add(New.Instance(new CSharpType(typeof(Dictionary<string, BinaryData>))));
257+
parameters.Add(Null);
258258
}
259259
return parameters.ToArray();
260260

eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/MgmtTypeSpecTestsModelFactory.cs

Lines changed: 24 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)