Skip to content

Commit ca94f2c

Browse files
authored
Merge pull request #75 from Exact-Realty/master
Fix issue with marshalling of stored procedure arguments
2 parents afca7ea + 7d2be5a commit ca94f2c

File tree

6 files changed

+52
-8
lines changed

6 files changed

+52
-8
lines changed

Postgrest/Client.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public Client(string baseUrl, ClientOptions? options = null)
8585

8686

8787
/// <inheritdoc />
88-
public Task<BaseResponse> Rpc(string procedureName, Dictionary<string, object>? parameters = null)
88+
public Task<BaseResponse> Rpc(string procedureName, object? parameters = null)
8989
{
9090
// Build Uri
9191
var builder = new UriBuilder($"{BaseUrl}/rpc/{procedureName}");
@@ -95,9 +95,9 @@ public Task<BaseResponse> Rpc(string procedureName, Dictionary<string, object>?
9595
var serializerSettings = SerializerSettings(Options);
9696

9797
// Prepare parameters
98-
Dictionary<string, string>? data = null;
98+
Dictionary<string, object>? data = null;
9999
if (parameters != null)
100-
data = JsonConvert.DeserializeObject<Dictionary<string, string>>(JsonConvert.SerializeObject(parameters, serializerSettings));
100+
data = JsonConvert.DeserializeObject<Dictionary<string, object>>(JsonConvert.SerializeObject(parameters, serializerSettings));
101101

102102
// Prepare headers
103103
var headers = Helpers.PrepareRequestHeaders(HttpMethod.Post, new Dictionary<string, string>(Options.Headers), Options);

Postgrest/Interfaces/IPostgrestClient.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public interface IPostgrestClient : IGettableHeaders
4444
/// <param name="procedureName">The function name to call</param>
4545
/// <param name="parameters">The parameters to pass to the function call</param>
4646
/// <returns></returns>
47-
Task<BaseResponse> Rpc(string procedureName, Dictionary<string, object> parameters);
47+
Task<BaseResponse> Rpc(string procedureName, object? parameters);
4848

4949
/// <summary>
5050
/// Returns a Table Query Builder instance for a defined model - representative of `USE $TABLE`

Postgrest/Postgrest.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
4+
<TargetFramework>netstandard2.0;</TargetFramework>
55
<LangVersion>9.0</LangVersion>
66
<WarningsAsErrors>CS8600;CS8602;CS8603</WarningsAsErrors>
77
<Nullable>enable</Nullable>

PostgrestTests/ClientTests.cs

+25
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,31 @@ public async Task TestStoredProcedure()
10621062
Assert.AreEqual(true, response.Content?.Contains("OFFLINE"));
10631063
}
10641064

1065+
[TestMethod("stored procedure with row param")]
1066+
public async Task TestStoredProcedureWithRowParam()
1067+
{
1068+
//Arrange
1069+
var client = new Client(BaseUrl);
1070+
1071+
//Act
1072+
var parameters = new Dictionary<string, object>
1073+
{
1074+
{
1075+
"param",
1076+
new Dictionary<string, object>
1077+
{
1078+
{ "username", "supabot" }
1079+
}
1080+
}
1081+
};
1082+
var response = await client.Rpc("get_data", parameters);
1083+
1084+
//Assert
1085+
Assert.AreEqual(true, response.ResponseMessage?.IsSuccessStatusCode);
1086+
Assert.AreEqual("null", response.Content);
1087+
}
1088+
1089+
10651090
[TestMethod("switch schema")]
10661091
public async Task TestSwitchSchema()
10671092
{

PostgrestTests/db/00-schema.sql

+19
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,15 @@ from users
110110
WHERE username = name_param;
111111
$$ LANGUAGE SQL IMMUTABLE;
112112

113+
-- STORED FUNCTION WITH ROW PARAMETER
114+
CREATE FUNCTION public.get_data(param public.users)
115+
RETURNS public.users.data%TYPE AS
116+
$$
117+
SELECT data
118+
from users u
119+
WHERE u.username = param.username;
120+
$$ LANGUAGE SQL IMMUTABLE;
121+
113122
-- SECOND SCHEMA USERS
114123
CREATE TYPE personal.user_status AS ENUM ('ONLINE', 'OFFLINE');
115124
CREATE TABLE personal.users
@@ -130,3 +139,13 @@ SELECT status
130139
from users
131140
WHERE username = name_param;
132141
$$ LANGUAGE SQL IMMUTABLE;
142+
143+
-- SECOND SCHEMA STORED FUNCTION WITH ROW PARAMETER
144+
CREATE FUNCTION personal.get_data(param personal.users)
145+
RETURNS personal.users.data%TYPE AS
146+
$$
147+
SELECT data
148+
from users u
149+
WHERE u.username = param.username;
150+
$$ LANGUAGE SQL IMMUTABLE;
151+

docker-compose.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ services:
33
rest:
44
image: postgrest/postgrest:latest
55
ports:
6-
- "3000:3000"
6+
- "127.0.0.1:3000:3000"
77
environment:
88
PGRST_DB_URI: postgres://postgres:postgres@db:5432/postgres
99
PGRST_DB_SCHEMA: public, personal
@@ -13,8 +13,8 @@ services:
1313
- db
1414
db:
1515
image: postgres:14
16-
ports:
17-
- "5432:5432"
16+
#ports:
17+
#- "127.0.0.1:5432:5432"
1818
volumes:
1919
- ./PostgrestTests/db:/docker-entrypoint-initdb.d/
2020
environment:

0 commit comments

Comments
 (0)