Skip to content
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

Fix JSON parsing errors caused by spaces and special characters in Lu… #16981

Merged
merged 6 commits into from
Nov 9, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ await _luceneIndexManager.SearchAsync(model.IndexName, async searcher =>

try
{
var parameterizedQuery = JsonNode.Parse(tokenizedContent).AsObject();
var parameterizedQuery = JsonNode.Parse(tokenizedContent, JOptions.Node, JOptions.Document).AsObject();
var luceneTopDocs = await _queryService.SearchAsync(context, parameterizedQuery);

if (luceneTopDocs != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Nodes;
using Fluid;
using Fluid.Values;
Expand Down Expand Up @@ -60,7 +61,7 @@ await _luceneIndexManager.SearchAsync(metadata.Index, async searcher =>
{
var tokenizedContent = await _liquidTemplateManager.RenderStringAsync(metadata.Template, _javaScriptEncoder, parameters.Select(x => new KeyValuePair<string, FluidValue>(x.Key, FluidValue.Create(x.Value, _templateOptions))));

var parameterizedQuery = JsonNode.Parse(tokenizedContent).AsObject();
var parameterizedQuery = JsonNode.Parse(tokenizedContent, JOptions.Node, JOptions.Document).AsObject();

var analyzer = _luceneAnalyzerManager.CreateAnalyzer(await _luceneIndexSettingsService.GetIndexAnalyzerAsync(metadata.Index));
var context = new LuceneQueryContext(searcher, LuceneSettings.DefaultVersion, analyzer);
Expand Down
22 changes: 22 additions & 0 deletions test/OrchardCore.Tests/Apis/Lucene/LuceneQueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,26 @@ public async Task TwoWildcardQueriesWithBoostHasResults()
Assert.Contains("Orchard", contentItems.ElementAt(3).DisplayText, StringComparison.OrdinalIgnoreCase);
};
}

[Fact]
public async Task LuceneQueryTemplateWithSpecialCharactersShouldNotThrowError()
{
using var context = new LuceneContext();
await context.InitializeAsync();

// Act
var index = "ArticleIndex";
var queryTemplate = "\r\r\n{% assign testVariable = \"48yvsghn194eft8axztaves25h\" %}\n\n{\n \"query\": {\n \"bool\": {\n \"must\": [\n { \"term\" : { \"Content.ContentItem.ContentType\" : \"Article\" } },\n { \"term\": { \"Content.ContentItem.Published\" : \"true\" } },\n ]\n }\n }\n}";

var content = await context.Client.GetAsync($"api/lucene/content?indexName={index}&query={queryTemplate}");
var queryResults = await content.Content.ReadAsAsync<LuceneQueryResults>();

// Assert
Assert.NotNull(queryResults);
Assert.NotEmpty(queryResults.Items);

var contentItems = queryResults.Items.Select(x => JObject.FromObject(x).Deserialize<ContentItem>());

Assert.Contains("Orchard", contentItems.First().DisplayText, StringComparison.OrdinalIgnoreCase);
}
}