Skip to content

Commit 0629fab

Browse files
authored
fix: gracefully handle unknown properties for C# (#2239)
1 parent 7383e9a commit 0629fab

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

examples/csharp-generate-json-serializer/__snapshots__/index.spec.ts.snap

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ internal class RootConverter : JsonConverter<Root>
3636
{
3737
if (reader.TokenType != JsonTokenType.StartObject)
3838
{
39-
throw new JsonException();
39+
throw new JsonException(\\"Unexpected start of JSON\\");
4040
}
4141
4242
var instance = new Root();
@@ -51,7 +51,7 @@ internal class RootConverter : JsonConverter<Root>
5151
// Get the key.
5252
if (reader.TokenType != JsonTokenType.PropertyName)
5353
{
54-
throw new JsonException();
54+
throw new JsonException(\\"Unexpected property name token in JSON\\");
5555
}
5656
5757
string propertyName = reader.GetString();
@@ -61,9 +61,11 @@ internal class RootConverter : JsonConverter<Root>
6161
instance.Email = value;
6262
continue;
6363
}
64+
// Gracefully handle/ignore unknown fields
65+
JsonSerializer.Deserialize<JsonElement>(ref reader, options);
6466
}
6567
66-
throw new JsonException();
68+
throw new JsonException(\\"Unexpected end of JSON\\");
6769
}
6870
public override void Write(Utf8JsonWriter writer, Root value, JsonSerializerOptions options)
6971
{

src/generators/csharp/presets/JsonSerializerPreset.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ function renderDeserialize({
177177
{
178178
if (reader.TokenType != JsonTokenType.StartObject)
179179
{
180-
throw new JsonException();
180+
throw new JsonException("Unexpected start of JSON");
181181
}
182182
183183
var instance = new ${model.name}();
@@ -192,14 +192,16 @@ function renderDeserialize({
192192
// Get the key.
193193
if (reader.TokenType != JsonTokenType.PropertyName)
194194
{
195-
throw new JsonException();
195+
throw new JsonException("Unexpected property name token in JSON");
196196
}
197197
198198
string propertyName = reader.GetString();
199199
${renderer.indent(deserializeProperties, 4)}
200+
// Gracefully handle/ignore unknown fields
201+
JsonSerializer.Deserialize<JsonElement>(ref reader, options);
200202
}
201203
202-
throw new JsonException();
204+
throw new JsonException("Unexpected end of JSON");
203205
}`;
204206
}
205207

test/generators/csharp/presets/__snapshots__/JsonSerializerPreset.spec.ts.snap

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ internal class TestConverter : JsonConverter<Test>
6969
{
7070
if (reader.TokenType != JsonTokenType.StartObject)
7171
{
72-
throw new JsonException();
72+
throw new JsonException(\\"Unexpected start of JSON\\");
7373
}
7474
7575
var instance = new Test();
@@ -84,7 +84,7 @@ internal class TestConverter : JsonConverter<Test>
8484
// Get the key.
8585
if (reader.TokenType != JsonTokenType.PropertyName)
8686
{
87-
throw new JsonException();
87+
throw new JsonException(\\"Unexpected property name token in JSON\\");
8888
}
8989
9090
string propertyName = reader.GetString();
@@ -116,9 +116,11 @@ internal class TestConverter : JsonConverter<Test>
116116
var deserializedValue = JsonSerializer.Deserialize<Dictionary<string, dynamic>?>(ref reader, options);
117117
instance.AdditionalProperties.Add(propertyName, deserializedValue);
118118
continue;
119+
// Gracefully handle/ignore unknown fields
120+
JsonSerializer.Deserialize<JsonElement>(ref reader, options);
119121
}
120122
121-
throw new JsonException();
123+
throw new JsonException(\\"Unexpected end of JSON\\");
122124
}
123125
public override void Write(Utf8JsonWriter writer, Test value, JsonSerializerOptions options)
124126
{
@@ -258,7 +260,7 @@ internal class NestedTestConverter : JsonConverter<NestedTest>
258260
{
259261
if (reader.TokenType != JsonTokenType.StartObject)
260262
{
261-
throw new JsonException();
263+
throw new JsonException(\\"Unexpected start of JSON\\");
262264
}
263265
264266
var instance = new NestedTest();
@@ -273,7 +275,7 @@ internal class NestedTestConverter : JsonConverter<NestedTest>
273275
// Get the key.
274276
if (reader.TokenType != JsonTokenType.PropertyName)
275277
{
276-
throw new JsonException();
278+
throw new JsonException(\\"Unexpected property name token in JSON\\");
277279
}
278280
279281
string propertyName = reader.GetString();
@@ -287,9 +289,11 @@ internal class NestedTestConverter : JsonConverter<NestedTest>
287289
var deserializedValue = JsonSerializer.Deserialize<Dictionary<string, dynamic>?>(ref reader, options);
288290
instance.AdditionalProperties.Add(propertyName, deserializedValue);
289291
continue;
292+
// Gracefully handle/ignore unknown fields
293+
JsonSerializer.Deserialize<JsonElement>(ref reader, options);
290294
}
291295
292-
throw new JsonException();
296+
throw new JsonException(\\"Unexpected end of JSON\\");
293297
}
294298
public override void Write(Utf8JsonWriter writer, NestedTest value, JsonSerializerOptions options)
295299
{

0 commit comments

Comments
 (0)