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.

Unable to serialize double value

See original GitHub issue

Elastic.Clients.Elasticsearch version: 8.1.1

Elasticsearch version: 8.7.1

.NET runtime version: .NET FW 4.7.2

Operating system version: Win10 22H2

Description of the problem including expected versus actual behavior: Cannot index an instance of a class containing a public property of type double. Elastic.Transport.UnexpectedTransportException: Unable to serialize double value.

Steps to reproduce:

  1. Execute following code/unit test:
using System;
using System.Diagnostics;
using Elastic.Clients.Elasticsearch;
using Elastic.Transport;
using Xunit;

namespace SearchESSequenceResearch.V1
{
	public class MyDecimalClass
	{
		public decimal MyDecimal { get; set; }
	}

	public class MyDoubleClass
	{
		public double MyDouble { get; set; }
	}

	public class TestIndexDouble
	{
		private const string TestMachineUriString = "https://docker.mydomain.com:9280"; // ES 8 in docker
		private readonly Uri _testMachineUri = new Uri(TestMachineUriString);

		[Fact]
		public void TestIndexingDouble()
		{
			ElasticsearchClient client = GetClient();
			var decimalInstance = new MyDecimalClass
			{
				MyDecimal = 123.45m
			};
			try
			{
				client.Index(decimalInstance, "mytestindex");
			}
			catch (Exception e)
			{
				Debug.WriteLine(e);
				throw;
			}
			
			var doubleInstance = new MyDoubleClass
			{
				MyDouble = 123.45d
			};
			try
			{
				client.Index(doubleInstance, "mytestindex");
			}
			catch (Exception e)
			{
				Debug.WriteLine(e);
				throw;
			}
			
		}

		private ElasticsearchClient GetClient()
		{
			var settings = new ElasticsearchClientSettings(_testMachineUri)
				.Authentication(new BasicAuthentication("elastic", "passwordhere"))
				.ServerCertificateValidationCallback((o, cert, chain, errors) => true);
			settings.DisableDirectStreaming();
			var client = new ElasticsearchClient(settings);
			return client;
		}
	}
}```

**Expected behavior**
The item should be serialized and addet to the index.

**Provide `DebugInformation` (if relevant)**:
Elastic.Transport.UnexpectedTransportException: Unable to serialize double value.

Elastic.Transport.UnexpectedTransportException
Unable to serialize double value.
   bei Elastic.Transport.DefaultHttpTransport`1.ThrowUnexpectedTransportException[TResponse](Exception killerException, List`1 seenExceptions, RequestData requestData, TResponse response, RequestPipeline pipeline) in /home/runner/work/elastic-transport-net/elastic-transport-net/src/Elastic.Transport/DefaultHttpTransport.cs:Zeile 328.
   bei Elastic.Transport.DefaultHttpTransport`1.Request[TResponse](HttpMethod method, String path, PostData data, RequestParameters requestParameters) in /home/runner/work/elastic-transport-net/elastic-transport-net/src/Elastic.Transport/DefaultHttpTransport.cs:Zeile 176.
   bei Elastic.Clients.Elasticsearch.ElasticsearchClient.DoRequest[TRequest,TResponse,TRequestParameters](TRequest request, Action`1 forceConfiguration) in /_/src/Elastic.Clients.Elasticsearch/Client/ElasticsearchClient.cs:Zeile 217.

Issue Analytics

  • State:closed
  • Created 3 months ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
karl-sjogrencommented, Jun 21, 2023

I submitted a PR for this last week, #7753. It is a bug that only happens when running .NET Framework, not .NET Core or later.

As a temporary workaround you can remove DoubleWithFractionalPortionConverter and FloatWithFractionalPortionConverter from the serializer converters list when constructing the client.

var elasticClientSettings = new ElasticsearchClientSettings(
    new SingleNodePool(new Uri("http://localhost:9200")),
    (defaultSerializer, settings) =>
    new DefaultSourceSerializer(
        settings,
        options => options.Converters.Remove(options.Converters.First(converter => converter.GetType().Name == "DoubleWithFractionalPortionConverter")))) // Need to match on type name since the actual type is internal
1reaction
karl-sjogrencommented, Jul 18, 2023

Yeah looking at this another minute shows that the .NET Framework code is completely broken (or rather, incomplete). Since there were not tests to verify it I missunderstood how severe this was. It didn’t just fall through to always throw an exception, the .NET Framework code copies the value into a Span<byte>, but it never writes it to the Utf8JsonWriter.

I can have a look at fixing this in the coming days and when #7769 is done we can add proper tests for this as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jackson fails to serialize a null Double - java
I am trying to serialize an object into an array and Jackson fails to do so with NPE. I am expecting something like...
Read more >
Serialize a Nullable double property - best way
Hello! private double? _Width; [XmlAttribute("width")] public double? Width { get { return _Width; } set { if (value != null) _Width = Math....
Read more >
Parsing Double in Mono (C#) fails
I need to serialize and dictionary or an object where some properties are double numbers, but I dont know how to fix this...
Read more >
Efficient JSON serialization with Jackson and Java
JSON provides for a very restricted set of possible value types. ... Number is essentially Java's double with some corner cases.
Read more >
Graphql custom query error: Can't convert '9' to double - Help
If I create a document through GraphQL and type in a value like 9 into the query, it gets serialized correctly as 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