From 95aacb9fdddfe4aa08563becb14803771fe7e971 Mon Sep 17 00:00:00 2001 From: Steve Gordon Date: Mon, 31 Jan 2022 13:43:29 +0000 Subject: [PATCH] Support deserialise of simple scripts (#6090) --- .../Scripting/ScriptFormatter.cs | 6 ++++ tests/Tests.Reproduce/GitHubIssue5684.cs | 35 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 tests/Tests.Reproduce/GitHubIssue5684.cs diff --git a/src/Nest/CommonOptions/Scripting/ScriptFormatter.cs b/src/Nest/CommonOptions/Scripting/ScriptFormatter.cs index f1a8a777e12..5c00bade56e 100644 --- a/src/Nest/CommonOptions/Scripting/ScriptFormatter.cs +++ b/src/Nest/CommonOptions/Scripting/ScriptFormatter.cs @@ -23,6 +23,12 @@ internal class ScriptFormatter : IJsonFormatter public IScript Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) { + if (reader.GetCurrentJsonToken() == JsonToken.String) + { + var scriptValue = reader.ReadString(); + return new InlineScript(scriptValue); + } + if (reader.GetCurrentJsonToken() != JsonToken.BeginObject) { reader.ReadNextBlock(); diff --git a/tests/Tests.Reproduce/GitHubIssue5684.cs b/tests/Tests.Reproduce/GitHubIssue5684.cs new file mode 100644 index 00000000000..f6143128ec7 --- /dev/null +++ b/tests/Tests.Reproduce/GitHubIssue5684.cs @@ -0,0 +1,35 @@ +// Licensed to Elasticsearch B.V under one or more agreements. +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. +// See the LICENSE file in the project root for more information + +using System.Text; +using Nest; +using System.Runtime.Serialization; +using System.IO; +using FluentAssertions; +using Elastic.Elasticsearch.Xunit.XunitPlumbing; + +namespace Tests.Reproduce +{ + public class GitHubIssue5684 + { + private static readonly byte[] ResponseBytes = Encoding.UTF8.GetBytes(@"{ + ""script"": ""doc['sales_price'].value * 2"" +}"); + + [U] + public void DeserialiseSimpleScript() + { + var client = new ElasticClient(); + var result = client.RequestResponseSerializer.Deserialize(new MemoryStream(ResponseBytes)); + result.Should().NotBeNull(); + result.Script.Should().BeOfType().Subject.Source.Should().Be("doc['sales_price'].value * 2"); + } + + private class Sample + { + [DataMember(Name = "script")] + public IScript Script { get;set; } + } + } +}