Skip to content

Commit

Permalink
Added parameters to WriteJson and added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
kwestground authored and oskardudycz committed Nov 22, 2023
1 parent 3fc9c44 commit 1439a3e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/IssueService/Controllers/JsonController.cs
Original file line number Diff line number Diff line change
@@ -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});

}
}
17 changes: 17 additions & 0 deletions src/Marten.AspNetCore.Testing/web_service_streaming_tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SimpleProperty[]>();

read.Length.ShouldBe(2);
read[0].Property.ShouldBe(value1);
read[1].Property.ShouldBe(value2);
}

internal record SimpleProperty(string Property);
}
5 changes: 3 additions & 2 deletions src/Marten.AspNetCore/QueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,12 @@ public static class QueryableExtensions
string sql,
HttpContext context,
string contentType = "application/json",
int onFoundStatus = 200
int onFoundStatus = 200,
params object[] parameters
)
{
var stream = new MemoryStream();
_ = await session.StreamJson<int>(stream, context.RequestAborted, sql).ConfigureAwait(false);
_ = await session.StreamJson<int>(stream, context.RequestAborted, sql, parameters).ConfigureAwait(false);

context.Response.StatusCode = onFoundStatus;
context.Response.ContentLength = stream.Length;
Expand Down

0 comments on commit 1439a3e

Please sign in to comment.