question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Search - JsonSerializationException when using model class with non-nullable value types

See original GitHub issue

Repro

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:closed
  • Created 8 years ago
  • Comments:14 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
devignedcommented, May 4, 2015

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

0reactions
brjohnstmsftcommented, Jan 22, 2016

FYI, the fix is now available in the latest version of the SDK on NuGet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to make it fail if non-nullable string is null? ...
I want the deserialize to fail if the string has a null value. The code above succeeds silently and places a null value...
Read more >
C# 8.0 nullable references and serialization
The problem is that this class has no declared constructors, and although the compiler supplies a default constructor in these cases, that ...
Read more >
Migrate from Newtonsoft.Json to System.Text.Json - .NET
Most of this article is about how to use the JsonSerializer API, ... Deserialize JSON null literal to non-nullable value types ...
Read more >
Nullable and notnull Serialisation Properties - csMACnz's Blog
My codebase is now littered with this code hack to get it to compile because nullable says it can't guarantee that this property...
Read more >
Digging Into Nullable Reference Types in C#
But what happens when we infer the type? For value types, it is assumed to be a non-nullable type, but for reference type…nullable:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found