Skip to content

Commit e04d12a

Browse files
committed
Update UnionFormatter to attempt TSecond if TFirst returns a null
This is to account for a Union<GeoCoordinate, DateMath> in the distance_query feature, which can result in an unwanted null in the GeoCoordinateFormatter
1 parent 52b405e commit e04d12a

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

src/Nest/CommonAbstractions/Union/UnionFormatter.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,37 @@ namespace Nest
55
{
66
internal class UnionFormatter<TFirst, TSecond> : IJsonFormatter<Union<TFirst, TSecond>>
77
{
8+
private readonly bool _attemptTSecondIfTFirstIsNull;
9+
10+
public UnionFormatter()
11+
{
12+
_attemptTSecondIfTFirstIsNull = false;
13+
}
14+
15+
public UnionFormatter(bool attemptTSecondIfTFirstIsNull)
16+
{
17+
_attemptTSecondIfTFirstIsNull = attemptTSecondIfTFirstIsNull;
18+
}
19+
820
public Union<TFirst, TSecond> Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
921
{
10-
Union<TFirst, TSecond> union = null;
1122
var segment = reader.ReadNextBlockSegment();
1223
if (TryRead(ref segment, formatterResolver, out TFirst first))
13-
union = first;
24+
{
25+
if (first == null && _attemptTSecondIfTFirstIsNull)
26+
{
27+
if (TryRead(ref segment, formatterResolver, out TSecond second))
28+
return second;
29+
}
30+
else
31+
{
32+
return first;
33+
}
34+
}
1435
else if (TryRead(ref segment, formatterResolver, out TSecond second))
15-
union = second;
36+
return second;
1637

17-
return union;
38+
return null;
1839
}
1940

2041
public void Serialize(ref JsonWriter writer, Union<TFirst, TSecond> value, IJsonFormatterResolver formatterResolver)

src/Nest/QueryDsl/Specialized/DistanceFeature/DistanceFeatureQuery.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public DistanceFeatureQueryDescriptor<T> Pivot(Distance pivot) =>
7777

7878
internal class DistanceFeatureQueryFormatter : IJsonFormatter<IDistanceFeatureQuery>
7979
{
80-
private static readonly UnionFormatter<GeoCoordinate, DateMath> OriginUnionFormatter = new UnionFormatter<GeoCoordinate, DateMath> ();
80+
private static readonly UnionFormatter<GeoCoordinate, DateMath> OriginUnionFormatter = new UnionFormatter<GeoCoordinate, DateMath>(true);
8181
private static readonly UnionFormatter<Distance, Time> PivotUnionFormatter = new UnionFormatter<Distance, Time>();
8282

8383
public void Serialize(ref JsonWriter writer, IDistanceFeatureQuery value, IJsonFormatterResolver formatterResolver)
@@ -101,7 +101,6 @@ public void Serialize(ref JsonWriter writer, IDistanceFeatureQuery value, IJsonF
101101

102102
writer.WritePropertyName("pivot");
103103
PivotUnionFormatter.Serialize(ref writer, value.Pivot, formatterResolver);
104-
105104
writer.WriteValueSeparator();
106105

107106
if (value.Boost.HasValue)

src/Tests/Tests/QueryDsl/Specialized/DistanceFeature/DistanceFeatureTimeQueryUsageTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public DistanceFeatureDistanceQueryUsageTests(ReadOnlyCluster i, EndpointUsage u
7373
};
7474

7575
protected override object QueryJson =>
76-
new { distance_feature = new { boost = 1.1, field = "startedOn", origin = new { lat = 70.0, lon = -70.0 }, pivot = "100mi" } };
76+
new { distance_feature = new { boost = 1.1, field = "leadDeveloper.location", origin = new [] { -70.0, 70.0 }, pivot = "100mi" } };
7777

7878
protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q
7979
.DistanceFeature(rf => rf

0 commit comments

Comments
 (0)