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.

[BUG] Cannot deserialize Microsoft.Azure.Search.Models.SearchResult

See original GitHub issue

Describe the bug The latest version of the the Microsoft.Azure.Search.Models.SearchResult class has Document with no public setter, and no JsonProperty attribute for deserialization. As a result, the Document property cannot be deserialized. Score and Highlights deserialize correctly.

We return a list of Microsoft.Azure.Search.Models.SearchResult<T> from an API, and our integration tests deserialize the returned JSON to the C# type to validate equivalence.

Expected behavior Microsoft.Azure.Search.Models.SearchResult should be deserializable from JSON, as well as serializable.

Actual behavior (include Exception or Stack Trace) The Document property is null after deserialization.

To Reproduce

    public class TestClass
    {
        public TestClass(string testString)
        {
            this.TestString = testString;
        }

        public TestClass()
        {
        }

        public string TestString { get; private set; }
    }


    public class Program
    {
        public static void Main(string[] args)
        {
            var test = new List<SearchResult<TestClass>>()
            {
                new SearchResult<TestClass>(new TestClass("Foo"), 45.0),
                new SearchResult<TestClass>(new TestClass("Bar"), 22.7)
            };

            var serialized = JsonConvert.SerializeObject(test, new JsonSerializerSettings() { ContractResolver = new DefaultContractResolver() });
            var deserialized = JsonConvert.DeserializeObject<List<SearchResult<TestClass>>>(serialized, new JsonSerializerSettings() { ContractResolver = new DefaultContractResolver() });

            Console.WriteLine(test.ToString());
            Console.WriteLine(serialized);
            Console.WriteLine(deserialized);
        }
    }

Environment: Microsoft.Azure.Search 10.1.0

  • Hosting platform or OS and .NET runtime version (dotnet --info output for .NET Core projects): Windows 10 .Net Core 3.1 in ASP.NET Core 3.1 web project
  • IDE and version : Visual Studio 2019 16.4.5

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
brjohnstmsftcommented, May 28, 2021

@dauv The model classes for Microsoft.Azure.Search were designed in tandem with custom serialization logic specific to that library; There was never an intention to support serialization/deserialization of these types by 3rd parties. That’s what I meant earlier when I said this was an unsupported scenario. We made no promise that this would work, so I view it more as an intentionally missing feature than a bug.

I guess the only way around it is to depend on the SDK models?

It’s the opposite – write your own models instead of depending on the SDK models. That approach worked for @rcbevans.

That said, I’m curious why you need to represent the search result payload as strongly-typed objects in a caching layer. Do you have code that manipulates the results before returning them to the client?

Also please note that Microsoft.Azure.Search has been deprecated; Please consider upgrading to Azure.Search.Documents if possible. There’s a migration guide here.

1reaction
brjohnstmsftcommented, Feb 17, 2020

@rcbevans Glad to hear you’re unblocked. I’m closing this issue since it’s not a bug per se. If external serialization of models is something you think Azure .NET SDKs should support, please open a new issue to track it as a feature request.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use Azure.Search.Documents (v11) in .NET
Learn how to create and manage search objects in a .NET application using C# and the Azure.Search.Documents (v11) client library.
Read more >
Is it possible to deserialize the results from an Azure Search?
I can't figure out a structure that would deserialize "@search.score" (or similar parameters if the query is more complex). I've tried using ...
Read more >
Class SearchIndexClient | Azure SDK for Net
Azure Cognitive Search client that can be used to query an index and upload, ... Object, unless the values cannot all be deserialized...
Read more >
Azure Search - Develop Cloud Connected Mobile Apps ...
In Azure Search, the model for the objects going into the store need to have a type and ... You can't sort by...
Read more >
Azure Search for Jekyll - Hutch Codes
json from the blog, deserializes it into a List<Page> and uploads that list to the Search Index. Last I looked there was a...
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