diff --git a/packages/Postgrest/Postgrest.Tests/Reading/ReadTests.cs b/packages/Postgrest/Postgrest.Tests/Reading/ReadTests.cs index 78ec7a87..196e5131 100644 --- a/packages/Postgrest/Postgrest.Tests/Reading/ReadTests.cs +++ b/packages/Postgrest/Postgrest.Tests/Reading/ReadTests.cs @@ -60,4 +60,11 @@ public async Task Get_ShouldReportTheFilteredCount_GivenAnExactCountRequest() .Filter("status", Operator.Equals, "ONLINE").Get(default, CountType.Exact); response.Count.Should().BeGreaterThan(-1); } + + [TestMethod] + public async Task Get_ShouldLeaveNullValuesOut_GivenStripNulls() + { + var response = await LocalStack.Client().Table().StripNulls().Get(); + response.Content.Should().NotContain(":null", "the seeded users have null columns"); + } } diff --git a/packages/Postgrest/Postgrest.Tests/Reading/StripNullsTests.cs b/packages/Postgrest/Postgrest.Tests/Reading/StripNullsTests.cs new file mode 100644 index 00000000..653821fd --- /dev/null +++ b/packages/Postgrest/Postgrest.Tests/Reading/StripNullsTests.cs @@ -0,0 +1,31 @@ +using System.Linq; +using System.Threading.Tasks; +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Postgrest.Tests.Models; +using Supabase.Postgrest; +using WireMock.RequestBuilders; +using WireMock.ResponseBuilders; +using WireMock.Server; + +namespace Postgrest.Tests.Reading; + +/// +/// StripNulls() sets the Accept header of the next request. +/// +[TestClass] +[TestCategory("Contract")] +public class StripNullsTests +{ + [TestMethod] + public async Task StripNulls_ShouldOnlyApplyToTheNextRequest() + { + using var server = WireMockServer.Start(); + server.Given(Request.Create().UsingAnyMethod()).RespondWith(Response.Create().WithBody("[]")); + var table = new Client(server.Url!).Table().StripNulls(); + await table.Get(); + await table.Get(); + server.LogEntries.Select(entry => entry.RequestMessage!.Headers!.TryGetValue("Accept", out var accept) ? accept[0] : null) + .Should().Equal("application/vnd.pgrst.array+json;nulls=stripped", null); + } +} diff --git a/packages/Postgrest/Postgrest/Interfaces/IPostgrestTable.cs b/packages/Postgrest/Postgrest/Interfaces/IPostgrestTable.cs index 729ab9fc..a7a11c12 100644 --- a/packages/Postgrest/Postgrest/Interfaces/IPostgrestTable.cs +++ b/packages/Postgrest/Postgrest/Interfaces/IPostgrestTable.cs @@ -440,6 +440,11 @@ IPostgrestTable Order(string foreignTable, string column, Constants.Orde /// IPostgrestTable Set(Expression>> keyValuePairExpression); + /// + /// Leaves null values out of the returned rows. Needs PostgREST 11.2 or later. + /// + IPostgrestTable StripNulls(); + /// /// Calls an Update function after `Set` has been called. /// diff --git a/packages/Postgrest/Postgrest/PublicAPI.Unshipped.txt b/packages/Postgrest/Postgrest/PublicAPI.Unshipped.txt index 3e84020b..d08e9c81 100644 --- a/packages/Postgrest/Postgrest/PublicAPI.Unshipped.txt +++ b/packages/Postgrest/Postgrest/PublicAPI.Unshipped.txt @@ -1,2 +1,4 @@ #nullable enable Supabase.Postgrest.Exceptions.FailureHint.Reason.UriTooLong = 8 -> Supabase.Postgrest.Exceptions.FailureHint.Reason +Supabase.Postgrest.Interfaces.IPostgrestTable.StripNulls() -> Supabase.Postgrest.Interfaces.IPostgrestTable! +Supabase.Postgrest.Table.StripNulls() -> Supabase.Postgrest.Interfaces.IPostgrestTable! diff --git a/packages/Postgrest/Postgrest/Table.cs b/packages/Postgrest/Postgrest/Table.cs index 1d602c8a..db61846d 100644 --- a/packages/Postgrest/Postgrest/Table.cs +++ b/packages/Postgrest/Postgrest/Table.cs @@ -70,6 +70,8 @@ namespace Supabase.Postgrest; private string? onConflict; + private bool stripNulls; + #endregion /// @@ -433,6 +435,14 @@ public IPostgrestTable Offset(int offset, string? foreignTableName = nul } + /// + public IPostgrestTable StripNulls() + { + this.stripNulls = true; + return this; + } + + /// public Task> Insert(TModel model, QueryOptions? options = null, CancellationToken cancellationToken = default) => @@ -908,6 +918,7 @@ public void Clear() this.offset = int.MinValue; this.offsetForeignKey = null; this.onConflict = null; + this.stripNulls = false; } @@ -970,6 +981,9 @@ private Task> Send(HttpMethod method, object? data, if (this.GetHeaders != null) requestHeaders = this.GetHeaders().MergeLeft(requestHeaders); + if (this.stripNulls) + requestHeaders.TryAdd("Accept", "application/vnd.pgrst.array+json;nulls=stripped"); + var url = this.GenerateUrl(); var preparedData = this.PrepareRequestData(data, isInsert, isUpdate, isUpsert);