-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Use a custom convertor for GeoOrientation to tolerate alternative options in Elasticsearch #3776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,45 @@ | ||
using System.Runtime.Serialization; | ||
using System; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Converters; | ||
|
||
namespace Nest | ||
{ | ||
[JsonConverter(typeof(StringEnumConverter))] | ||
[JsonConverter(typeof(GeoOrientationConverter))] | ||
public enum GeoOrientation | ||
{ | ||
[EnumMember(Value = "cw")] | ||
ClockWise, | ||
|
||
[EnumMember(Value = "ccw")] | ||
CounterClockWise | ||
} | ||
|
||
internal class GeoOrientationConverter : JsonConverter | ||
{ | ||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
var geoOrientation = (GeoOrientation)value; | ||
switch (geoOrientation) | ||
{ | ||
case GeoOrientation.ClockWise: | ||
writer.WriteValue("cw"); | ||
break; | ||
case GeoOrientation.CounterClockWise: | ||
writer.WriteValue("ccw"); | ||
break; | ||
} | ||
} | ||
|
||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
var enumString = (string)reader.Value; | ||
switch (enumString.ToLower()) | ||
{ | ||
case "left": | ||
case "cw": | ||
case "clockwise": | ||
return GeoOrientation.ClockWise; | ||
} | ||
// Default, complies with the OGC standard | ||
return GeoOrientation.CounterClockWise; | ||
} | ||
|
||
public override bool CanConvert(Type objectType) => true; | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
src/Tests/Tests/Mapping/Types/Geo/GeoShape/GeoShapeClusterMetadataApiTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System; | ||
using System.Linq; | ||
using Elasticsearch.Net; | ||
using FluentAssertions; | ||
using Nest; | ||
using Tests.Core.Extensions; | ||
using Tests.Core.ManagedElasticsearch.Clusters; | ||
using Tests.Domain; | ||
using Tests.Framework; | ||
using Tests.Framework.Integration; | ||
|
||
namespace Tests.Mapping.Types.Core.GeoShape | ||
{ | ||
public class GeoShapeClusterMetadataApiTests : ApiIntegrationTestBase<WritableCluster, IPutMappingResponse, IPutMappingRequest, PutMappingDescriptor<Project>, | ||
PutMappingRequest<Project>> | ||
{ | ||
public GeoShapeClusterMetadataApiTests(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage) { } | ||
|
||
protected override void IntegrationSetup(IElasticClient client, CallUniqueValues values) | ||
{ | ||
foreach (var index in values.Values) client.CreateIndex(index, CreateIndexSettings).ShouldBeValid(); | ||
var indices = Infer.Indices(values.Values.Select(i => (IndexName)i)); | ||
client.ClusterHealth(f => f.WaitForStatus(WaitForStatus.Yellow).Index(indices)) | ||
.ShouldBeValid(); | ||
} | ||
|
||
protected virtual ICreateIndexRequest CreateIndexSettings(CreateIndexDescriptor create) => create; | ||
|
||
protected override bool ExpectIsValid => true; | ||
protected override int ExpectStatusCode => 200; | ||
|
||
protected override Func<PutMappingDescriptor<Project>, IPutMappingRequest> Fluent => f => f | ||
.Index(CallIsolatedValue) | ||
.Properties(FluentProperties); | ||
|
||
private static Func<PropertiesDescriptor<Project>, IPromise<IProperties>> FluentProperties => f => f | ||
.GeoShape(s => s | ||
.Name(p => p.Location) | ||
.Tree(GeoTree.Quadtree) | ||
.Orientation(GeoOrientation.ClockWise) | ||
.Strategy(GeoStrategy.Recursive) | ||
.TreeLevels(3) | ||
.PointsOnly() | ||
.DistanceErrorPercentage(1.0) | ||
.Coerce() | ||
); | ||
|
||
private static IProperties InitializerProperties => new Properties | ||
{ | ||
{ | ||
"location", new GeoShapeProperty | ||
{ | ||
Tree = GeoTree.Quadtree, | ||
Orientation = GeoOrientation.ClockWise, | ||
Strategy = GeoStrategy.Recursive, | ||
TreeLevels = 3, | ||
PointsOnly = true, | ||
DistanceErrorPercentage = 1.0, | ||
Coerce = true | ||
} | ||
} | ||
}; | ||
|
||
protected override HttpMethod HttpMethod => HttpMethod.PUT; | ||
|
||
protected override PutMappingRequest<Project> Initializer => new PutMappingRequest<Project>(CallIsolatedValue, typeof(Project)) | ||
{ | ||
Properties = InitializerProperties | ||
}; | ||
|
||
protected override string UrlPath => $"/{CallIsolatedValue}/doc/_mapping"; | ||
|
||
protected override LazyResponses ClientUsage() => Calls( | ||
(client, f) => client.Map(f), | ||
(client, f) => client.MapAsync(f), | ||
(client, r) => client.Map(r), | ||
(client, r) => client.MapAsync(r) | ||
); | ||
|
||
protected override void ExpectResponse(IPutMappingResponse response) | ||
{ | ||
// Ensure metadata can be deserialised | ||
var metadata = Client.ClusterState(r => r.Metric(ClusterStateMetric.Metadata)); | ||
metadata.IsValid.Should().BeTrue(); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
think this should use
ToLowerInvariant()
, rather than use the current cultureThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't make the cut for the merge, but will fix for
7.x