Skip to content
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
7 changes: 7 additions & 0 deletions packages/Postgrest/Postgrest.Tests/Reading/ReadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<User>().StripNulls().Get();
response.Content.Should().NotContain(":null", "the seeded users have null columns");
}
}
31 changes: 31 additions & 0 deletions packages/Postgrest/Postgrest.Tests/Reading/StripNullsTests.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// <c>StripNulls()</c> sets the Accept header of the next request.
/// </summary>
[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<Todo>().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);
}
}
5 changes: 5 additions & 0 deletions packages/Postgrest/Postgrest/Interfaces/IPostgrestTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,11 @@ IPostgrestTable<TModel> Order(string foreignTable, string column, Constants.Orde
/// <exception cref="ArgumentException"></exception>
IPostgrestTable<TModel> Set(Expression<Func<TModel, KeyValuePair<object, object?>>> keyValuePairExpression);

/// <summary>
/// Leaves null values out of the returned rows. Needs PostgREST 11.2 or later.
/// </summary>
IPostgrestTable<TModel> StripNulls();

/// <summary>
/// Calls an Update function after `Set` has been called.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions packages/Postgrest/Postgrest/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
#nullable enable
Supabase.Postgrest.Exceptions.FailureHint.Reason.UriTooLong = 8 -> Supabase.Postgrest.Exceptions.FailureHint.Reason
Supabase.Postgrest.Interfaces.IPostgrestTable<TModel>.StripNulls() -> Supabase.Postgrest.Interfaces.IPostgrestTable<TModel!>!
Supabase.Postgrest.Table<TModel>.StripNulls() -> Supabase.Postgrest.Interfaces.IPostgrestTable<TModel!>!
14 changes: 14 additions & 0 deletions packages/Postgrest/Postgrest/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ namespace Supabase.Postgrest;

private string? onConflict;

private bool stripNulls;

#endregion

/// <summary>
Expand Down Expand Up @@ -433,6 +435,14 @@ public IPostgrestTable<TModel> Offset(int offset, string? foreignTableName = nul
}


/// <inheritdoc />
public IPostgrestTable<TModel> StripNulls()
{
this.stripNulls = true;
return this;
}


/// <inheritdoc />
public Task<ModeledResponse<TModel>> Insert(TModel model, QueryOptions? options = null,
CancellationToken cancellationToken = default) =>
Expand Down Expand Up @@ -908,6 +918,7 @@ public void Clear()
this.offset = int.MinValue;
this.offsetForeignKey = null;
this.onConflict = null;
this.stripNulls = false;
}


Expand Down Expand Up @@ -970,6 +981,9 @@ private Task<ModeledResponse<TU>> Send<TU>(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);

Expand Down
Loading