Azure-sdk-for-net: [QUERY] SearchAsync<T> Deserializes to empty object

Created on 2 Oct 2020  路  3Comments  路  Source: Azure/azure-sdk-for-net

Query/Question

There is a weird issue that I could not figure out yet why this happens. When I hit the search URL with the necessary parameters in Postman, I receive an array of data. But when hitting the endpoint with the same values in code, the results get deserialized to empty objects and Console.Writeline outputs empty strings.

Here is the code I use:

ProductSearchResult.cs:

  using System.Text.Json.Serialization;

  public class ProductSearchResult
  {
    [JsonPropertyName("id")]
    public string Id { get; }

    [JsonPropertyName("rid")]
    public string Rid { get; }

    [JsonPropertyName("company")]
    public string Company { get; }

  }
static void Main(string[] args)
{
  SearchResults<ProductSearchResult> s = SearchAsync();
  Console.WriteLine(s.GetResults().First().Document.Company);
}

private static SearchResults<ProductSearchResult> SearchAsync()
{
  Uri SearchEndpoint = new Uri("https://some-search.search.windows.net");
  string IndexName = "products";
  AzureKeyCredential AzCredential = new AzureKeyCredential("SECRET");
  SearchClient client = new SearchClient(SearchEndpoint, IndexName, AzCredential);
  var s = client.SearchAsync<ProductSearchResult>("test").Result.Value;

  return s;
}

The query in Postman returns:

    "value": [
        {
            "@search.score": 3.8673322,
            "company": "Test Comp",
            "id": "replace_with_new_document_id",
            "rid": "some_rid"
        }
    ]

Environment:

  • netcoreapp3.1

    • Azure.Search.Documents 11.1.1

    • .NET Core SDK:

      Version: 3.1.402

      Commit: 9b5de826fd

Runtime:
OS Name: Windows
OS Version: 10.0.19041
OS Platform: Windows
RID: win10-x64
Base Path: C:Program Filesdotnetsdk3.1.402

Host (useful for support):
Version: 3.1.8
Commit: 9c1330dedd

.NET Core SDKs installed:
3.1.402

.NET Core runtimes installed:
Microsoft.AspNetCore.App 3.1.7
Microsoft.AspNetCore.App 3.1.8
Microsoft.NETCore.App 3.1.7
Microsoft.NETCore.App 3.1.8
Microsoft.WindowsDesktop.App 3.1.7
Microsoft.WindowsDesktop.App 3.1.8

  • IDE and version : VS Code 1.49.2
Client Search customer-reported question

All 3 comments

Thank you for your feedback. Tagging and routing to the team member best able to assist.

This looks like System.Text.Json behavior:

If the JSON contains a value for a read-only property, the value is ignored and no exception is thrown.

Could you try adding setters for your properties?

This looks like System.Text.Json behavior:

If the JSON contains a value for a read-only property, the value is ignored and no exception is thrown.

Could you try adding setters for your properties?

That was indeed it. After adding setter to my class it worked! Thank you very much!

Was this page helpful?
0 / 5 - 0 ratings