From b2973bf22dd28fc6c8cb52a48ecfc53093f52a49 Mon Sep 17 00:00:00 2001 From: Kenny Westermark Date: Thu, 16 Nov 2023 10:24:29 +0100 Subject: [PATCH] Added parameters to WriteJson and added tests. --- src/IssueService/Controllers/JsonController.cs | 15 +++++++++++++++ .../web_service_streaming_tests.cs | 17 +++++++++++++++++ src/Marten.AspNetCore/QueryableExtensions.cs | 5 +++-- 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 src/IssueService/Controllers/JsonController.cs diff --git a/src/IssueService/Controllers/JsonController.cs b/src/IssueService/Controllers/JsonController.cs new file mode 100644 index 0000000000..6148b5246c --- /dev/null +++ b/src/IssueService/Controllers/JsonController.cs @@ -0,0 +1,15 @@ +using System.Threading.Tasks; +using Marten; +using Marten.AspNetCore; +using Microsoft.AspNetCore.Mvc; + +namespace IssueService.Controllers +{ + public class JsonController : ControllerBase + { + [HttpGet("/json/sql/{value1}/{value2}")] + public Task GetJsonFromSql([FromServices] IQuerySession store, string value1, string value2) + => store.WriteJson("SELECT jsonb_build_object('Property', ?) UNION SELECT jsonb_build_object('Property', ?);", HttpContext, contentType: "application/json", onFoundStatus: 200, parameters: new object[]{value1, value2}); + + } +} diff --git a/src/Marten.AspNetCore.Testing/web_service_streaming_tests.cs b/src/Marten.AspNetCore.Testing/web_service_streaming_tests.cs index 24ef79a079..ff57a1db0f 100644 --- a/src/Marten.AspNetCore.Testing/web_service_streaming_tests.cs +++ b/src/Marten.AspNetCore.Testing/web_service_streaming_tests.cs @@ -215,5 +215,22 @@ public async Task stream_an_array_of_documents_with_compiled_query(int? onFoundS read.Length.ShouldBe(issues.Count(x => x.Open)); } + [Theory] + [InlineData("value1", "value2")] + public async Task stream_a_json_with_raw_sql(string value1, string value2) + { + var result = await theHost.Scenario(s => + { + s.Get.Url($"/json/sql/{value1}/{value2}"); + s.ContentTypeShouldBe("application/json"); + }); + + var read = result.ReadAsJson(); + + read.Length.ShouldBe(2); + read[0].Property.ShouldBe(value1); + read[1].Property.ShouldBe(value2); + } + internal record SimpleProperty(string Property); } diff --git a/src/Marten.AspNetCore/QueryableExtensions.cs b/src/Marten.AspNetCore/QueryableExtensions.cs index eda35972a0..b971c743f1 100644 --- a/src/Marten.AspNetCore/QueryableExtensions.cs +++ b/src/Marten.AspNetCore/QueryableExtensions.cs @@ -291,11 +291,12 @@ public static async Task WriteJson( string sql, HttpContext context, string contentType = "application/json", - int onFoundStatus = 200 + int onFoundStatus = 200, + params object[] parameters ) { var stream = new MemoryStream(); - _ = await session.StreamJson(stream, context.RequestAborted, sql).ConfigureAwait(false); + _ = await session.StreamJson(stream, context.RequestAborted, sql, parameters).ConfigureAwait(false); context.Response.StatusCode = onFoundStatus; context.Response.ContentLength = stream.Length;