Skip to content

Commit

Permalink
Support string constants in endpoint action parameter overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
C0nquistadore committed May 25, 2022
1 parent 84613a3 commit 305e848
Show file tree
Hide file tree
Showing 15 changed files with 171 additions and 129 deletions.
4 changes: 4 additions & 0 deletions src/Dibix.Http.Server/Model/HttpParameterSourceSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public void ResolveParameterFromConstant(string targetParameterName, int value)
{
this.ResolveParameter(targetParameterName, new HttpParameterConstantSource(value));
}
public void ResolveParameterFromConstant(string targetParameterName, string value)
{
this.ResolveParameter(targetParameterName, new HttpParameterConstantSource(value));
}
public void ResolveParameterFromNull(string targetParameterName)
{
this.ResolveParameter(targetParameterName, new HttpParameterConstantSource(null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public interface IHttpParameterSourceSelector
{
void ResolveParameterFromConstant(string targetParameterName, bool value);
void ResolveParameterFromConstant(string targetParameterName, int value);
void ResolveParameterFromConstant(string targetParameterName, string value);
void ResolveParameterFromNull(string targetParameterName);
void ResolveParameterFromSource(string targetParameterName, string sourceName, string sourcePropertyName);
void ResolveParameterFromSource(string targetParameterName, string sourceName, string sourcePropertyName, string converterName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,12 @@ private bool TryReadSource(JProperty property, string filePath, bool isItem, out
return true;

case JTokenType.String:
source = ReadPropertySource((JValue)property.Value, filePath);
string stringValue = (string)property.Value;
if (stringValue != null && stringValue.Contains('.'))
source = this.ReadPropertySource((JValue)property.Value, filePath);
else
source = ReadConstantSource((JValue)property.Value);

return true;

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ private static string ComputeConstantLiteral(object value)
switch (value)
{
case bool boolValue: return boolValue.ToString().ToLowerInvariant();
case string stringValue: return $"\"{stringValue}\"";
case null: return "null";
default: return value.ToString();
}
Expand Down
4 changes: 4 additions & 0 deletions src/Dibix.Sdk/Schema/dibix.endpoints.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
{
"type": "number"
},
{
"type": "string",
"pattern": "^[^.]+$"
},
{
"type": "null"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"AnotherInputContract": {
"U": "string",
"V": "string",
"W": {
"A": "string",
"B": "string",
"C": {
"type": "string",
"isOptional": true
},
"SomeIds": "#AnotherEntry*",
"X": "uuid",
"D": "uuid",
"Password": "string",
"Y": "boolean",
"Z": "int32"
"E": "boolean",
"F": "int32",
"G": "string"
}
}
13 changes: 7 additions & 6 deletions tests/Dibix.Sdk.Tests.Database/Contracts/InputContract.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"InputContract": {
"U": "string",
"V": "string",
"W": {
"A": "string",
"B": "string",
"C": {
"type": "string",
"isOptional": true
},
"Ids": "#Entry*",
"X": "uuid",
"D": "uuid",
"Password": "string",
"Y": "boolean",
"Z": "int32"
"E": "boolean",
"F": "int32",
"G": "string"
}
}
13 changes: 7 additions & 6 deletions tests/Dibix.Sdk.Tests.Database/Endpoints/GenericEndpoint.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
"target": "EmptyWithParams",
"childRoute": "{password}/Fixed",
"params": {
"u": "HEADER.User-Agent",
"v": "HEADER.Authorization.Parameter",
"w": {
"a": "HEADER.User-Agent",
"b": "HEADER.Authorization.Parameter",
"c": {
"source": "DBX.X",
"converter": "DBX"
},
"password": null,
"x": "REQUEST.Language",
"y": true,
"z": 5
"d": "REQUEST.Language",
"e": true,
"f": 5,
"g": "cake"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"method": "GET",
"target": "EmptyWithParams",
"params": {
"u": "WTF.Is.This",
"v": "ENV.MachinePassword"
"a": "WTF.Is.This",
"b": "ENV.MachinePassword"
}
},
{
Expand All @@ -15,8 +15,8 @@
"body": "Request",
"childRoute": "this/get/is/wrong",
"params": {
"v": "BODY.X",
"w": "BODY.Id.Nm",
"a": "BODY.X",
"b": "BODY.Id.Nm",
"ids": {
"source": "BODY.Ids",
"items": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
-- @Name EmptyWithParams
CREATE PROCEDURE [dbo].[dbx_tests_syntax_empty_params]
@u NVARCHAR(50)
, @v NVARCHAR(50)
, @w UNIQUEIDENTIFIER NULL
@a NVARCHAR(50)
, @b NVARCHAR(50)
, @c UNIQUEIDENTIFIER NULL
, /* @Obfuscate */ @password NVARCHAR(128)
, @ids [dbo].[dbx_codeanalysis_udt_generic] READONLY
, @x NVARCHAR(50) NULL = NULL
, @y BIT = 1
, /* @ClrType Direction */ @z INT NULL = NULL
, @d NVARCHAR(50) NULL = NULL
, @e BIT = 1
, /* @ClrType Direction */ @f INT NULL = NULL
, @g NVARCHAR(50) NULL = N'Cake'
AS
PRINT CONCAT(@u, @v, @w, @password, @ids, @x, @y, @z)
PRINT CONCAT(@a, @b, @c, @password, @ids, @d, @e, @f, @g)
43 changes: 23 additions & 20 deletions tests/Dibix.Sdk.Tests/Resources/CodeGeneration/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ namespace Dibix.Sdk.Tests.DomainModel
{
public sealed class AnotherInputContract
{
public string U { get; set; }
public string V { get; set; }
public string W { get; set; }
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
public IList<Dibix.Sdk.Tests.DomainModel.AnotherEntry> SomeIds { get; private set; }
public System.Guid X { get; set; }
public System.Guid D { get; set; }
public string Password { get; set; }
public bool Y { get; set; }
public int Z { get; set; }
public bool E { get; set; }
public int F { get; set; }
public string G { get; set; }

public AnotherInputContract()
{
Expand Down Expand Up @@ -77,14 +78,15 @@ public enum Role : int

public sealed class InputContract
{
public string U { get; set; }
public string V { get; set; }
public string W { get; set; }
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
public IList<Dibix.Sdk.Tests.DomainModel.Entry> Ids { get; private set; }
public System.Guid X { get; set; }
public System.Guid D { get; set; }
public string Password { get; set; }
public bool Y { get; set; }
public int Z { get; set; }
public bool E { get; set; }
public int F { get; set; }
public string G { get; set; }

public InputContract()
{
Expand All @@ -101,7 +103,7 @@ public interface IGenericEndpointService : IHttpService
{
Task<HttpResponse<ICollection<Dibix.Sdk.Tests.DomainModel.GenericContract>>> MultiConcreteResultAsync(CancellationToken cancellationToken = default);
Task<HttpResponseMessage> EmptyWithParams1Async(string password, string userAgent, IEnumerable<object> ids, string? acceptLanguage = null, CancellationToken cancellationToken = default);
Task<HttpResponseMessage> EmptyWithParamsAnonymousAsync(string password, string u, string v, System.Guid? w, IEnumerable<object> ids, string? x = null, bool y = true, Dibix.Sdk.Tests.DomainModel.Direction? z = null, CancellationToken cancellationToken = default);
Task<HttpResponseMessage> EmptyWithParamsAnonymousAsync(string password, string a, string b, System.Guid? c, IEnumerable<object> ids, string? d = null, bool e = true, Dibix.Sdk.Tests.DomainModel.Direction? f = null, string? g = "Cake", CancellationToken cancellationToken = default);
Task<HttpResponse<Dibix.Sdk.Tests.DomainModel.GenericContract>> SingleConrecteResultWithParamsAsync(IEnumerable<int> ids, CancellationToken cancellationToken = default);
Task<HttpResponse<System.IO.Stream>> FileResultAsync(int id, CancellationToken cancellationToken = default);
Task<HttpResponseMessage> FileUploadAsync(System.IO.Stream body, CancellationToken cancellationToken = default);
Expand Down Expand Up @@ -161,18 +163,19 @@ public async Task<HttpResponseMessage> EmptyWithParams1Async(string password, st
}
}

public async Task<HttpResponseMessage> EmptyWithParamsAnonymousAsync(string password, string u, string v, System.Guid? w, IEnumerable<object> ids, string? x = null, bool y = true, Dibix.Sdk.Tests.DomainModel.Direction? z = null, CancellationToken cancellationToken = default)
public async Task<HttpResponseMessage> EmptyWithParamsAnonymousAsync(string password, string a, string b, System.Guid? c, IEnumerable<object> ids, string? d = null, bool e = true, Dibix.Sdk.Tests.DomainModel.Direction? f = null, string? g = "Cake", CancellationToken cancellationToken = default)
{
using (HttpClient client = this._httpClientFactory.CreateClient(BaseAddress))
{
Uri uri = UriBuilder.Create($"Tests/GenericEndpoint/{password}/User", UriKind.Relative)
.AddQueryParam(nameof(u), u)
.AddQueryParam(nameof(v), v)
.AddQueryParam(nameof(w), w)
.AddQueryParam(nameof(a), a)
.AddQueryParam(nameof(b), b)
.AddQueryParam(nameof(c), c)
.AddQueryParam(nameof(ids), ids)
.AddQueryParam(nameof(x), x)
.AddQueryParam(nameof(y), y)
.AddQueryParam(nameof(z), z)
.AddQueryParam(nameof(d), d)
.AddQueryParam(nameof(e), e)
.AddQueryParam(nameof(f), f)
.AddQueryParam(nameof(g), g)
.Build();
HttpRequestMessage requestMessage = new HttpRequestMessage(new HttpMethod("GET"), uri);
HttpResponseMessage responseMessage = await client.SendAsync(requestMessage, cancellationToken).ConfigureAwait(false);
Expand Down
54 changes: 29 additions & 25 deletions tests/Dibix.Sdk.Tests/Resources/CodeGeneration/Endpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,21 @@ public static class TestAccessor
// SingleConrecteResultWithParams
private const string SingleConrecteResultWithParamsCommandText = "[dbo].[dbx_tests_syntax_singleconcreteresult_params]";

public static void EmptyWithParams(this IDatabaseAccessorFactory databaseAccessorFactory, string u, string v, System.Guid? w, string password, Dibix.Sdk.Tests.Data.GenericParameterSet ids, string? x = null, bool y = true, Dibix.Sdk.Tests.DomainModel.Direction? z = null)
public static void EmptyWithParams(this IDatabaseAccessorFactory databaseAccessorFactory, string a, string b, System.Guid? c, string password, Dibix.Sdk.Tests.Data.GenericParameterSet ids, string? d = null, bool e = true, Dibix.Sdk.Tests.DomainModel.Direction? f = null, string? g = "Cake")
{
using (IDatabaseAccessor accessor = databaseAccessorFactory.Create())
{
ParametersVisitor @params = accessor.Parameters()
.SetFromTemplate(new
{
u,
v,
w,
a,
b,
c,
ids,
x,
y,
z
d,
e,
f,
g
})
.SetString(nameof(password), password, true)
.Build();
Expand Down Expand Up @@ -151,15 +152,16 @@ namespace Dibix.Sdk.Tests.DomainModel
{
public sealed class AnotherInputContract
{
public string U { get; set; }
public string V { get; set; }
public string A { get; set; }
public string B { get; set; }
[Optional]
public string W { get; set; }
public string C { get; set; }
public IList<Dibix.Sdk.Tests.DomainModel.AnotherEntry> SomeIds { get; private set; }
public System.Guid X { get; set; }
public System.Guid D { get; set; }
public string Password { get; set; }
public bool Y { get; set; }
public int Z { get; set; }
public bool E { get; set; }
public int F { get; set; }
public string G { get; set; }

public AnotherInputContract()
{
Expand Down Expand Up @@ -219,15 +221,16 @@ public enum Role : int

public sealed class InputContract
{
public string U { get; set; }
public string V { get; set; }
public string A { get; set; }
public string B { get; set; }
[Optional]
public string W { get; set; }
public string C { get; set; }
public IList<Dibix.Sdk.Tests.DomainModel.Entry> Ids { get; private set; }
public System.Guid X { get; set; }
public System.Guid D { get; set; }
public string Password { get; set; }
public bool Y { get; set; }
public int Z { get; set; }
public bool E { get; set; }
public int F { get; set; }
public string G { get; set; }

public InputContract()
{
Expand Down Expand Up @@ -255,12 +258,13 @@ public override void Configure(IHttpApiDiscoveryContext context)
y.Method = HttpApiMethod.Get;
y.ChildRoute = "{password}/Fixed";
y.ResolveParameterFromNull("password");
y.ResolveParameterFromSource("u", "HEADER", "User-Agent");
y.ResolveParameterFromSource("v", "HEADER", "Authorization.Parameter");
y.ResolveParameterFromSource("w", "DBX", "X", "DBX");
y.ResolveParameterFromSource("x", "REQUEST", "Language");
y.ResolveParameterFromConstant("y", true);
y.ResolveParameterFromConstant("z", 5);
y.ResolveParameterFromSource("a", "HEADER", "User-Agent");
y.ResolveParameterFromSource("b", "HEADER", "Authorization.Parameter");
y.ResolveParameterFromSource("c", "DBX", "X", "DBX");
y.ResolveParameterFromSource("d", "REQUEST", "Language");
y.ResolveParameterFromConstant("e", true);
y.ResolveParameterFromConstant("f", 5);
y.ResolveParameterFromConstant("g", "cake");
});
x.AddAction(ReflectionHttpActionTarget.Create(typeof(Dibix.Sdk.Tests.Data.TestAccessor), nameof(Dibix.Sdk.Tests.Data.TestAccessor.EmptyWithParams)), y =>
{
Expand Down
Loading

0 comments on commit 305e848

Please sign in to comment.