Search - JsonSerializationException when using model class with non-nullable value types
See original GitHub issueRepro
Create a custom model class with a property of non-nullable value type. For example, add a public UnitCount property of type int instead of int?
If you index a document with the default value of that type (e.g. – 0 for int), the field will be null in Azure Search. If you subsequently search for that document, the Search call will throw JsonSerializationException complaining that it can’t convert null to int.
The expected behavior is that SearchIndexClient
shouldn’t allow model classes with non-nullable primitive types. Any call that takes such a type as a parameter should throw ArgumentException if the type contains any primitive public properties.
Notes
This bug is caused by two things: 1.We don’t validate user-defined model classes early. 2.We use DefaultValueHandling.Ignore in JSON.NET to elide nulls in Indexing payloads. This is not a problem per se; We just need to ensure that user-defined model classes don’t use non-nullable primitive types.
http://www.newtonsoft.com/json/help/html/SerializationSettings.htm
Issue Analytics
- State:
- Created 8 years ago
- Comments:14 (11 by maintainers)
Top GitHub Comments
I’m just suggesting you should store the value the user put into the document rather than nulling it out.
0 == 0 not 0 == null
FYI, the fix is now available in the latest version of the SDK on NuGet.