Skip to content

Commit

Permalink
2.8.3
Browse files Browse the repository at this point in the history
  • Loading branch information
vbilopav committed Jun 11, 2024
1 parent 3616047 commit 951935c
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 56 deletions.
65 changes: 37 additions & 28 deletions NpgsqlRest/MiddlewareExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public static IApplicationBuilder UseNpgsqlRest(this IApplicationBuilder builder
List<NpgsqlRestParameter> paramsList = new(routine.ParamCount);
bool hasNulls = false;
int? headerParameterIndex = null;
NpgsqlRestParameter? headerParam = null;
if (routine.ParamCount > 0)
{
if (endpoint.RequestParamType == RequestParamType.QueryString)
Expand Down Expand Up @@ -155,6 +155,15 @@ public static IApplicationBuilder UseNpgsqlRest(this IApplicationBuilder builder
ActualName = routine.ParamNames[i],
TypeDescriptor = descriptor
};
if (endpoint.RequestHeadersMode == RequestHeadersMode.Parameter)
{
if (string.Equals(p, endpoint.RequestHeadersParameterName, StringComparison.Ordinal) ||
string.Equals(routine.ParamNames[i], endpoint.RequestHeadersParameterName, StringComparison.Ordinal))
{
headerParam = parameter;
}
}
if (context.Request.Query.TryGetValue(p, out var qsValue))
{
if (TryParseParameter(ref qsValue, ref descriptor, ref parameter, endpoint.QueryStringNullHandling))
Expand Down Expand Up @@ -245,18 +254,8 @@ public static IApplicationBuilder UseNpgsqlRest(this IApplicationBuilder builder
return;
}
}
if (parameter.Value is null)
{
if (endpoint.RequestHeadersMode == RequestHeadersMode.Parameter)
{
if (string.Equals(p, endpoint.RequestHeadersParameterName, StringComparison.Ordinal) ||
string.Equals(routine.ParamNames[i], endpoint.RequestHeadersParameterName, StringComparison.Ordinal))
{
headerParameterIndex = i;
}
}
}
if (parameter.Value is not null || headerParameterIndex is not null)
if (parameter.Value is not null)
{
paramsList.Add(parameter);
setCount++;
Expand Down Expand Up @@ -313,6 +312,14 @@ public static IApplicationBuilder UseNpgsqlRest(this IApplicationBuilder builder
ActualName = routine.ParamNames[i],
TypeDescriptor = descriptor
};
if (endpoint.RequestHeadersMode == RequestHeadersMode.Parameter)
{
if (string.Equals(p, endpoint.RequestHeadersParameterName, StringComparison.Ordinal) ||
string.Equals(routine.ParamNames[i], endpoint.RequestHeadersParameterName, StringComparison.Ordinal))
{
headerParam = parameter;
}
}
if (bodyDict.TryGetValue(p, out var value))
{
if (TryParseParameter(ref value, ref descriptor, ref parameter))
Expand Down Expand Up @@ -385,18 +392,8 @@ public static IApplicationBuilder UseNpgsqlRest(this IApplicationBuilder builder
return;
}
}
if (parameter.Value is null)
{
if (endpoint.RequestHeadersMode == RequestHeadersMode.Parameter)
{
if (string.Equals(p, endpoint.RequestHeadersParameterName, StringComparison.Ordinal) ||
string.Equals(routine.ParamNames[i], endpoint.RequestHeadersParameterName, StringComparison.Ordinal))
{
headerParameterIndex = i;
}
}
}
if (parameter.Value is not null || headerParameterIndex is not null)
if (parameter.Value is not null)
{
paramsList.Add(parameter);
setCount++;
Expand Down Expand Up @@ -487,11 +484,23 @@ public static IApplicationBuilder UseNpgsqlRest(this IApplicationBuilder builder
PgConverters.SerializeString(ref value));
}
headers = string.Concat(headers, "}");
if (endpoint.RequestHeadersMode == RequestHeadersMode.Parameter)
if (endpoint.RequestHeadersMode == RequestHeadersMode.Parameter && headerParam is not null)
{
if (headerParameterIndex.HasValue)
var headerAdded = false;
for (var ip = 0; ip < paramsList.Count; ip++)
{
var p = paramsList[ip];
if (p == headerParam)
{
//p.Value = headers;
headerAdded = true;
break;
}
}
if (headerAdded is false)
{
paramsList[headerParameterIndex.Value].Value = headers;
headerParam.Value = headers;
paramsList.Add(headerParam);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions NpgsqlRest/NpgsqlRest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageReadmeFile>README.MD</PackageReadmeFile>
<DocumentationFile>bin\$(Configuration)\$(AssemblyName).xml</DocumentationFile>
<Version>2.8.2</Version>
<AssemblyVersion>2.8.2</AssemblyVersion>
<FileVersion>2.8.2</FileVersion>
<PackageVersion>2.8.2</PackageVersion>
<Version>2.8.3</Version>
<AssemblyVersion>2.8.3</AssemblyVersion>
<FileVersion>2.8.3</FileVersion>
<PackageVersion>2.8.3</PackageVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
Expand Down
2 changes: 1 addition & 1 deletion NpgsqlRestClient/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@
//
// See https://vb-consulting.github.io/npgsqlrest/options/#requestheadersparametername
//
"RequestHeadersParameterName": "headers",
"RequestHeadersParameterName": "_headers",
//
// See https://vb-consulting.github.io/npgsqlrest/options/#returnnpgsqlexceptionmessage
//
Expand Down
11 changes: 2 additions & 9 deletions NpgsqlRestTests/RequestHeadersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ public static void RequestHeadersTests()
RequestHeaders Context
';
create function get_req_headers_parameter1(_headers text = null) returns text language sql as 'select _headers';
comment on function get_req_headers_parameter1(text) is '
HTTP
Expand All @@ -34,16 +32,13 @@ RequestHeaders Context
request-headers-parameter-name h
';
create function get_req_headers_param_not_default(_not_default text) returns text language sql as 'select _not_default';
comment on function get_req_headers_param_not_default(text) is '
HTTP
request-headers parameter
request-headers-parameter-name _not_default
';
create function req_headers_parameter1(_headers text = null) returns text language sql as 'select _headers';
comment on function req_headers_parameter1(text) is '
HTTP
Expand Down Expand Up @@ -142,8 +137,7 @@ public async Task Test_get_req_headers_param_not_default()
using var response = await test.Client.GetAsync("/api/get-req-headers-param-not-default");
var content = await response.Content.ReadAsStringAsync();

response?.StatusCode.Should().Be(HttpStatusCode.OK);
content.Should().Be("{\"Host\":\"localhost\",\"custom-header1\":\"custom-header1-value\"}");
response?.StatusCode.Should().Be(HttpStatusCode.NotFound);
}

[Fact]
Expand Down Expand Up @@ -193,7 +187,6 @@ public async Task Test_req_headers_param_not_default()
using var response = await test.Client.PostAsync("/api/req-headers-param-not-default", null);
var content = await response.Content.ReadAsStringAsync();

response?.StatusCode.Should().Be(HttpStatusCode.OK);
content.Should().Be("{\"Host\":\"localhost\",\"custom-header1\":\"custom-header1-value\"}");
response?.StatusCode.Should().Be(HttpStatusCode.NotFound);
}
}
22 changes: 20 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,31 @@ Note: For a changelog for a client application [see the client application page

---

## Version [2.8.3](https://github.com/vb-consulting/NpgsqlRest/tree/2.8.3) (2024-06-11)

[Full Changelog](https://github.com/vb-consulting/NpgsqlRest/compare/2.8.2...2.8.3)

Fix inconsistency with sending request parameters by routine parameters.

Previously it was possible to send request parameters to parameters without default values. To use request parameters in routine parameters, that parameter has to have a default value always.

This inconsistency is actually a bug in cases when the request header parameter name wasn't provided a value.

This is fixed now.

TsClient 1.8.1:

- If all routines are skipped, don't write any files.

---

## Version [2.8.2](https://github.com/vb-consulting/NpgsqlRest/tree/2.8.2) (2024-06-09)

[Full Changelog](https://github.com/vb-consulting/NpgsqlRest/compare/2.8.1...2.8.2)

### Fixed bug with default parameters

Using a routine that has a default parameters and supplying one of the parameters would sometimes caused mixing of the parameter order. For example, if the routine is defined as:
Using a routine that has default parameters and supplying one of the parameters would sometimes cause mixing of the parameter order. For example, if the routine is defined like this:

```sql
create function get_two_default_params(
Expand All @@ -25,7 +43,7 @@ select _p1 || _p2;
$$;
```

And invoking it with only second parameter parameter (p2) would mix p1 and p2 and wrongly assume that the first parameter is the second parameter and vice versa.
Invoking it with only the second parameter parameter (p2) would mix p1 and p2 and wrongly assume that the first parameter is the second parameter and vice versa.

This is now fixed and the parameters are correctly assigned.

Expand Down
1 change: 0 additions & 1 deletion options.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ This is the group of options used for authentication:
- Type: `string?`
- Default: `null`


Authentication type used with the Login endpoints to set the authentication type for the new `ClaimsIdentity` created by the login.

This value must be set to non-null when using login endpoints, otherwise, the following error will raise: `SignInAsync when principal.Identity.IsAuthenticated is false is not allowed when AuthenticationOptions.RequireAuthenticatedSignIn is true.`
Expand Down
8 changes: 4 additions & 4 deletions plugins/NpgsqlRest.TsClient/NpgsqlRest.TsClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageReadmeFile>README.MD</PackageReadmeFile>
<DocumentationFile>bin\$(Configuration)\$(AssemblyName).xml</DocumentationFile>
<Version>1.8.0</Version>
<AssemblyVersion>1.8.0</AssemblyVersion>
<FileVersion>1.8.0</FileVersion>
<PackageVersion>1.8.0</PackageVersion>
<Version>1.8.1</Version>
<AssemblyVersion>1.8.1</AssemblyVersion>
<FileVersion>1.8.1</FileVersion>
<PackageVersion>1.8.1</PackageVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
Expand Down
27 changes: 20 additions & 7 deletions plugins/NpgsqlRest.TsClient/TsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,33 @@ private void Run((Routine routine, RoutineEndpoint endpoint)[] endpoints, string
string.Format("const baseUrl = \"{0}\";", GetHost()));
}

bool handled = false;
foreach (var (routine, endpoint) in endpoints
.Where(e => e.routine.Type == RoutineType.Table || e.routine.Type == RoutineType.View)
.OrderBy(e => e.routine.Schema)
.ThenBy(e => e.routine.Type)
.ThenBy(e => e.routine.Name))
{
Handle(routine, endpoint);
if (Handle(routine, endpoint) && handled is false)
{
handled = true;
}
}

foreach (var (routine, endpoint) in endpoints
.Where(e => (e.routine.Type == RoutineType.Table || e.routine.Type == RoutineType.View) is false)
.OrderBy(e => e.routine.Schema)
.ThenBy(e => e.routine.Name))
{
Handle(routine, endpoint);
if (Handle(routine, endpoint) && handled is false)
{
handled = true;
}
}

if (handled is false)
{
return;
}

if (!options.FileOverwrite && File.Exists(fileName))
Expand Down Expand Up @@ -166,19 +178,19 @@ void AddHeader(StringBuilder sb)
sb.Insert(0, string.Join(Environment.NewLine, options.HeaderLines.Select(l => string.Format(l, now))));
}

void Handle(Routine routine, RoutineEndpoint endpoint)
bool Handle(Routine routine, RoutineEndpoint endpoint)
{
if (options.SkipRoutineNames.Contains(routine.Name))
{
return;
return false;
}
if (options.SkipSchemas.Contains(routine.Schema))
{
return;
return false;
}
if (options.SkipPaths.Contains(endpoint.Url))
{
return;
return false;
}

var name = string.IsNullOrEmpty(_npgsqlRestoptions?.UrlPathPrefix) ? endpoint.Url : endpoint.Url[_npgsqlRestoptions.UrlPathPrefix.Length..];
Expand Down Expand Up @@ -226,7 +238,7 @@ void Handle(Routine routine, RoutineEndpoint endpoint)

if (options.SkipFunctionNames.Contains(camel))
{
return;
return false;
}

content.AppendLine();
Expand Down Expand Up @@ -456,6 +468,7 @@ string GetReturnExp(string responseExp)
funcBody));
content.AppendLine("}");

return true;
} // void Handle
}

Expand Down

0 comments on commit 951935c

Please sign in to comment.