Skip to content

[Backport 7.x] Support deserialisation of simple scripts #6092

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 1 commit into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Nest/CommonOptions/Scripting/ScriptFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ internal class ScriptFormatter : IJsonFormatter<IScript>

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();
Expand Down
35 changes: 35 additions & 0 deletions tests/Tests.Reproduce/GitHubIssue5684.cs
Original file line number Diff line number Diff line change
@@ -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<Sample>(new MemoryStream(ResponseBytes));
result.Should().NotBeNull();
result.Script.Should().BeOfType<InlineScript>().Subject.Source.Should().Be("doc['sales_price'].value * 2");
}

private class Sample
{
[DataMember(Name = "script")]
public IScript Script { get;set; }
}
}
}