Skip to content

Commit 54fc967

Browse files
russcamMpdreamz
authored andcommitted
Add important admonition about using System.Decimal POCO property types (#2495)
Bump AsciiDoctNet to support additional asciidoc syntax Closes #2463 Conflicts: src/Tests/ClientConcepts/HighLevel/Mapping/AutoMap.doc.cs
1 parent 8df8d2c commit 54fc967

File tree

9 files changed

+54
-9
lines changed

9 files changed

+54
-9
lines changed

docs/client-concepts/high-level/mapping/auto-map.asciidoc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,29 @@ var expected = new
290290
Expect(expected).WhenSerializing((ICreateIndexRequest)descriptor);
291291
----
292292

293+
[IMPORTANT]
294+
====
295+
Some .NET types do not have direct equivalent Elasticsearch types. For example, `System.Decimal` is a type
296+
commonly used to express currencies and other financial calculations that require large numbers of significant
297+
integral and fractional digits and no round-off errors. There is no equivalent type in Elasticsearch, and the
298+
nearest type is {ref_current}/number.html[``double``], a double-precision 64-bit IEEE 754 floating point.
299+
300+
When a POCO has a `System.Decimal` property, it is automapped to the Elasticsearch `double` type. With the caveat
301+
of a potential loss of precision, this is generally acceptable for a lot of use cases, but it can however cause
302+
problems in _some_ edge cases.
303+
304+
As the https://download.microsoft.com/download/3/8/8/388e7205-bc10-4226-b2a8-75351c669b09/csharp%20language%20specification.doc[C# Specification states],
305+
306+
[quote, C# Specification section 6.2.1]
307+
For a conversion from `decimal` to `float` or `double`, the `decimal` value is rounded to the nearest `double` or `float` value.
308+
While this conversion may lose precision, it never causes an exception to be thrown.
309+
310+
This conversion causes an exception to be thrown at deserialization time for `Decimal.MinValue` and `Decimal.MaxValue` because, at
311+
serialization time, the nearest `double` value that is converted to is outside of the bounds of `Decimal.MinValue` or `Decimal.MaxValue`,
312+
respectively. In these cases, it is advisable to use `double` as the POCO property type.
313+
314+
====
315+
293316
[[auto-mapping-with-overrides]]
294317
[float]
295318
== Auto mapping with overrides

docs/high-level.asciidoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ please modify the original csharp file found at the link and submit the PR with
1414
[partintro]
1515
--
1616
The high level client, `ElasticClient`, provides a strongly typed query DSL that maps one-to-one with the Elasticsearch query DSL.
17+
1718
It can be installed from the Package Manager Console inside Visual Studio using
1819

1920
[source,shell]
@@ -22,8 +23,10 @@ Install-Package NEST
2223
----
2324

2425
Or by searching for https://www.nuget.org/packages/NEST[NEST] in the Package Manager GUI.
26+
2527
NEST internally uses and still exposes the low level client, `ElasticLowLevelClient`, from <<elasticsearch-net,Elasticsearch.Net>> via
2628
the `.LowLevel` property on `ElasticClient`.
29+
2730
There are a number of conventions that NEST uses for inference of
2831

2932
* <<index-name-inference, Index Names>>

docs/low-level.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ please modify the original csharp file found at the link and submit the PR with
1515
--
1616
The low level client, `ElasticLowLevelClient`, is a low level, dependency free client that has no
1717
opinions about how you build and represent your requests and responses.
18+
1819
It can be installed from the Package Manager Console inside Visual Studio using
1920

2021
[source,shell]

paket.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
NUGET
22
remote: https://www.nuget.org/api/v2
3-
AsciiDocNet (1.0.0-alpha3)
3+
AsciiDocNet (1.0.0-alpha5)
44
Bogus (7.1.6)
55
NETStandard.Library (>= 1.6) - framework: >= netstandard13
66
Newtonsoft.Json (>= 9.0.1) - framework: >= net40, >= netstandard13

src/CodeGeneration/DocGenerator/AsciiDoc/GeneratedAsciidocVisitor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ public override void Visit(SectionTitle sectionTitle)
264264
var builder = new StringBuilder();
265265
using (var writer = new AsciiDocVisitor(new StringWriter(builder)))
266266
{
267-
writer.Visit(sectionTitle.Elements);
267+
writer.Visit((InlineContainer)sectionTitle);
268268
}
269269

270270
var title = builder.ToString().PascalToHyphen();
@@ -300,7 +300,7 @@ private bool LastSectionTitleMatches(Func<string, bool> predicate)
300300
var builder = new StringBuilder();
301301
using (var visitor = new AsciiDocVisitor(new StringWriter(builder)))
302302
{
303-
visitor.Visit(lastSectionTitle.Elements);
303+
visitor.Visit((InlineContainer)lastSectionTitle);
304304
}
305305

306306
return predicate(builder.ToString());

src/CodeGeneration/DocGenerator/Documentation/Files/CSharpDocumentationFile.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ private void CleanDocumentAndWriteToFile(string body, FileInfo destination)
200200

201201
document.Accept(new AsciiDocVisitor(file));
202202
}
203-
204203
}
205204
}
206205
}

src/CodeGeneration/DocGenerator/Documentation/Files/DocumentationFile.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.IO;
4-
using System.Reflection.Emit;
53
using System.Text.RegularExpressions;
6-
using DocGenerator;
74

85
namespace DocGenerator.Documentation.Files
96
{

src/CodeGeneration/DocGenerator/Documentation/Files/ImageDocumentationFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public override void SaveToDocumentationFolder()
1111
{
1212
var docFileName = this.CreateDocumentationLocation();
1313

14-
// copy for asciidoc to work (path is relative to file)
14+
// copy for asciidoc to work when viewing a single asciidoc in the browser (path is relative to file)
1515
this.FileLocation.CopyTo(docFileName.FullName, true);
1616

1717
// copy to the root as well, for the doc generation process (path is relative to root)

src/Tests/ClientConcepts/HighLevel/Mapping/AutoMap.doc.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,28 @@ public void UsingAutoMap()
275275

276276
Expect(expected).WhenSerializing((ICreateIndexRequest)descriptor);
277277
}
278+
/**[IMPORTANT]
279+
* ====
280+
* Some .NET types do not have direct equivalent Elasticsearch types. For example, `System.Decimal` is a type
281+
* commonly used to express currencies and other financial calculations that require large numbers of significant
282+
* integral and fractional digits and no round-off errors. There is no equivalent type in Elasticsearch, and the
283+
* nearest type is {ref_current}/number.html[``double``], a double-precision 64-bit IEEE 754 floating point.
284+
*
285+
* When a POCO has a `System.Decimal` property, it is automapped to the Elasticsearch `double` type. With the caveat
286+
* of a potential loss of precision, this is generally acceptable for a lot of use cases, but it can however cause
287+
* problems in _some_ edge cases.
288+
*
289+
* As the https://download.microsoft.com/download/3/8/8/388e7205-bc10-4226-b2a8-75351c669b09/csharp%20language%20specification.doc[C# Specification states],
290+
*
291+
* [quote, C# Specification section 6.2.1]
292+
* For a conversion from `decimal` to `float` or `double`, the `decimal` value is rounded to the nearest `double` or `float` value.
293+
* While this conversion may lose precision, it never causes an exception to be thrown.
294+
*
295+
* This conversion causes an exception to be thrown at deserialization time for `Decimal.MinValue` and `Decimal.MaxValue` because, at
296+
* serialization time, the nearest `double` value that is converted to is outside of the bounds of `Decimal.MinValue` or `Decimal.MaxValue`,
297+
* respectively. In these cases, it is advisable to use `double` as the POCO property type.
298+
* ====
299+
*/
278300

279301
/**[float]
280302
* == Auto mapping with overrides
@@ -969,7 +991,7 @@ public void PutMappingAlsoAdheresToMaxRecursion()
969991
//endhide
970992

971993
/**[float]
972-
* == Applying conventions through the Visitor pattern
994+
* == Applying conventions through the Visitor pattern
973995
* It is also possible to apply a transformation on all or specific properties.
974996
*
975997
* `.AutoMap()` internally implements the https://en.wikipedia.org/wiki/Visitor_pattern[visitor pattern]. The default visitor, `NoopPropertyVisitor`,

0 commit comments

Comments
 (0)