Skip to content

Commit f59b75f

Browse files
muhammad-othmandscpinheiro
authored andcommitted
Skip unknown properties when mapping EndpointTests
1 parent 6ea1dfe commit f59b75f

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

generator/ServiceClientGeneratorLib/GenerationManifest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ private ServiceConfiguration CreateServiceConfiguration(JsonData modelNode, Json
288288
throw new FileNotFoundException($"Endpoints tests are missing. Expected file suffix is .{EndpointRuleSetTestsFile}");
289289
}
290290
json = File.ReadAllText(testsFileName);
291-
config.EndpointTests = JsonMapper.ToObject<EndpointTests>(json);
291+
config.EndpointTests = JsonMapper.ToObject<EndpointTests>(json, true);
292292
}
293293

294294
if (modelNode[ModelsSectionKeys.NugetPackageTitleSuffix] != null)

generator/ServiceClientGeneratorLib/ThirdParty/Json/JsonMapper.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ private static MethodInfo GetConvOp (Type t1, Type t2)
308308
return op;
309309
}
310310

311-
private static object ReadValue (Type inst_type, JsonReader reader)
311+
private static object ReadValue (Type inst_type, JsonReader reader, bool skipUnknownProperties = false)
312312
{
313313
reader.Read ();
314314

@@ -398,7 +398,7 @@ private static object ReadValue (Type inst_type, JsonReader reader)
398398
}
399399

400400
while (true) {
401-
object item = ReadValue (elem_type, reader);
401+
object item = ReadValue (elem_type, reader, skipUnknownProperties);
402402
if (reader.Token == JsonToken.ArrayEnd)
403403
break;
404404

@@ -435,29 +435,34 @@ private static object ReadValue (Type inst_type, JsonReader reader)
435435

436436
if (prop_data.IsField) {
437437
((FieldInfo) prop_data.Info).SetValue (
438-
instance, ReadValue (prop_data.Type, reader));
438+
instance, ReadValue (prop_data.Type, reader, skipUnknownProperties));
439439
} else {
440440
PropertyInfo p_info =
441441
(PropertyInfo) prop_data.Info;
442442

443443
if (p_info.CanWrite)
444444
p_info.SetValue (
445445
instance,
446-
ReadValue (prop_data.Type, reader),
446+
ReadValue (prop_data.Type, reader, skipUnknownProperties),
447447
null);
448448
else
449-
ReadValue (prop_data.Type, reader);
449+
ReadValue (prop_data.Type, reader, skipUnknownProperties);
450450
}
451451

452452
} else {
453453
if (! t_data.IsDictionary)
454+
{
455+
if (skipUnknownProperties)
456+
continue;
457+
454458
throw new JsonException (String.Format (
455459
"The type {0} doesn't have the " +
456460
"property '{1}'", inst_type, property));
461+
}
457462

458463
((IDictionary) instance).Add (
459464
property, ReadValue (
460-
t_data.ElementType, reader));
465+
t_data.ElementType, reader, skipUnknownProperties));
461466
}
462467

463468
}
@@ -862,11 +867,11 @@ public static T ToObject<T> (TextReader reader)
862867
return (T) ReadValue (typeof (T), json_reader);
863868
}
864869

865-
public static T ToObject<T> (string json)
870+
public static T ToObject<T> (string json, bool skipUnknownProperties = false)
866871
{
867872
JsonReader reader = new JsonReader (json);
868873

869-
return (T) ReadValue (typeof (T), reader);
874+
return (T) ReadValue (typeof (T), reader, skipUnknownProperties);
870875
}
871876

872877
public static IJsonWrapper ToWrapper (WrapperFactory factory,

0 commit comments

Comments
 (0)