Skip to content

Commit d551834

Browse files
fix(http-client-csharp): emit else-if for required nullable null fallback
Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
1 parent 2b1e18b commit d551834

4 files changed

Lines changed: 15 additions & 16 deletions

File tree

.chronus/changes/fix-required-nullable-patch-duplicate-2026-6-29-14-9-14.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ packages:
44
- "@typespec/http-client-csharp"
55
---
66

7-
Avoid emitting duplicate output for required nullable properties during patch serialization. The null fallback for a required nullable property is now gated on `!Patch.Contains(...)` so patched values remain the single source of output for that property.
7+
Avoid emitting duplicate output for required nullable properties during patch serialization.

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/MrwSerializationTypeDefinition.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2560,15 +2560,20 @@ private MethodBodyStatement CreateConditionalSerializationStatement(
25602560

25612561
// When the property path is already present in the patch, the patched value is
25622562
// emitted by Patch.WriteTo(writer), so the null fallback must be gated on the patch
2563-
// check to avoid writing the property twice.
2564-
MethodBodyStatement writeNullStatement = patchCheck != null
2565-
? new IfStatement(patchCheck) { _utf8JsonWriterSnippet.WriteNull(serializedName) }
2566-
: _utf8JsonWriterSnippet.WriteNull(serializedName);
2563+
// check to avoid writing the property twice. Emit it as an "else if" so the fallback
2564+
// is skipped entirely when the path is patched.
2565+
if (patchCheck != null)
2566+
{
2567+
return new IfElseStatement(
2568+
new IfStatement(condition) { writePropertySerializationStatement },
2569+
[new IfStatement(patchCheck) { _utf8JsonWriterSnippet.WriteNull(serializedName) }],
2570+
null);
2571+
}
25672572

25682573
return new IfElseStatement(
25692574
condition,
25702575
writePropertySerializationStatement,
2571-
writeNullStatement);
2576+
_utf8JsonWriterSnippet.WriteNull(serializedName));
25722577
}
25732578

25742579
if (shouldCheckJsonPath)

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/MrwSerializationTypeDefinitions/TestData/DynamicModelSerializationTests/WriteRequiredNullableProperty.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,9 @@ protected virtual void JsonModelWriteCore(global::System.Text.Json.Utf8JsonWrite
4141
writer.WritePropertyName("foo"u8);
4242
writer.WriteStringValue(Foo);
4343
}
44-
else
44+
else if (!Patch.Contains("$.foo"u8))
4545
{
46-
if (!Patch.Contains("$.foo"u8))
47-
{
48-
writer.WriteNull("foo"u8);
49-
}
46+
writer.WriteNull("foo"u8);
5047
}
5148

5249
Patch.WriteTo(writer);

packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/Models/DynamicModel.Serialization.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,9 @@ protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWrit
221221
Patch.WriteTo(writer, "$.requiredNullableDictionary"u8);
222222
writer.WriteEndObject();
223223
}
224-
else
224+
else if (!Patch.Contains("$.requiredNullableDictionary"u8))
225225
{
226-
if (!Patch.Contains("$.requiredNullableDictionary"u8))
227-
{
228-
writer.WriteNull("requiredNullableDictionary"u8);
229-
}
226+
writer.WriteNull("requiredNullableDictionary"u8);
230227
}
231228
if (!Patch.Contains("$.primitiveDictionary"u8))
232229
{

0 commit comments

Comments
 (0)