Skip to content

Commit

Permalink
Added code to validate query parameter partial resolution with multiv…
Browse files Browse the repository at this point in the history
…ariable parameters. (#80)
  • Loading branch information
mwadams authored Sep 16, 2023
1 parent 1d3a705 commit fa7ee75
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,55 @@ public void PartiallyParametersFromAnObjectFromInvalidUrl()
})
.Resolve();

Assert.Equal("http://dev.example.org/v2/customers{?active,country}", url);
Assert.Equal("http://dev.example.org/v2/customers{?active}{&country}", url);
}

[Fact]
public void PartiallyParametersFromAnObject()
{

var url = new UriTemplate("http://{environment}.example.org/{version}/customers{?active,country}", resolvePartially: true)
.AddParameters(new {
environment = "dev",
version = "v2",
active = "foo"
})
.Resolve();

Assert.Equal("http://dev.example.org/v2/customers?active=foo{&country}", url);
}

[Fact]
public void PartiallyParametersFromAnObjectMissingSecondOfThree()
{

var url = new UriTemplate("http://{environment}.example.org/{version}/customers{?active,country,thing}", resolvePartially: true)
.AddParameters(new {
environment = "dev",
version = "v2",
active = "foo",
thing = "bar"
})
.Resolve();

Assert.Equal("http://dev.example.org/v2/customers?active=foo{&country}&thing=bar", url);
}


[Fact]
public void PartiallyParametersFromAnObjectMissingThirdOfThree()
{

var url = new UriTemplate("http://{environment}.example.org/{version}/customers{?active,country,thing}", resolvePartially: true)
.AddParameters(new {
environment = "dev",
version = "v2",
active = "foo",
country = "bar"
})
.Resolve();

Assert.Equal("http://dev.example.org/v2/customers?active=foo&country=bar{&thing}", url);
}


Expand Down
2 changes: 1 addition & 1 deletion Solutions/Corvus.UriTemplate.TavisApi.Tests/UsageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void ShouldAllowPartialUriTemplateWithQueryParamsButNoValues()
//template.SetParameter("bar", "yo");
//template.SetParameter("blar", "yuck");
var uriString = template.Resolve();
Assert.Equal("http://example.org/foo{?bar,baz}", uriString);
Assert.Equal("http://example.org/foo{?bar}{&baz}", uriString);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ private static bool ProcessExpression<TState>(ReadOnlySpan<char> currentExpressi
bool multivariableExpression = false;
int varNameStart = -1;
int varNameEnd = -1;
VariableProcessingState success = VariableProcessingState.Success;

var varSpec = new VariableSpecification(op, ReadOnlySpan<char>.Empty);
for (int i = firstChar; i < currentExpression.Length; i++)
Expand Down Expand Up @@ -295,10 +296,11 @@ private static bool ProcessExpression<TState>(ReadOnlySpan<char> currentExpressi
case ',':
varSpec.VarName = currentExpression[varNameStart..varNameEnd];
multivariableExpression = true;

#if NET6_0
VariableProcessingState success = ProcessVariable(parameterProvider, ref varSpec, output, multivariableExpression, resolvePartially, parameters, parameterNameCallback, ref state);
success = ProcessVariable(parameterProvider, ref varSpec, output, multivariableExpression, resolvePartially, parameters, parameterNameCallback, ref state);
#else
VariableProcessingState success = ProcessVariable(ref varSpec, output, multivariableExpression, resolvePartially, parameters, parameterNameCallback, ref state);
success = ProcessVariable(ref varSpec, output, multivariableExpression, resolvePartially, parameters, parameterNameCallback, ref state);
#endif
bool isFirst = varSpec.First;

Expand All @@ -311,11 +313,6 @@ private static bool ProcessExpression<TState>(ReadOnlySpan<char> currentExpressi
varSpec.First = false;
}

if ((success == VariableProcessingState.NotProcessed) && resolvePartially)
{
output.Write(',');
}

break;

default:
Expand Down Expand Up @@ -353,11 +350,6 @@ private static bool ProcessExpression<TState>(ReadOnlySpan<char> currentExpressi
return false;
}

if (multivariableExpression && resolvePartially)
{
output.Write('}');
}

return true;
}

Expand Down Expand Up @@ -386,10 +378,16 @@ private static VariableProcessingState ProcessVariable<TState>(ref VariableSpeci
{
if (varSpec.First)
{
output.Write('{');
output.Write("{");
}
else
{
output.Write("{&");
}

varSpec.CopyTo(output);

output.Write('}');
}
else
{
Expand Down
31 changes: 20 additions & 11 deletions Solutions/Sandbox/Program.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
using System.Text.Json;
using Corvus.UriTemplates;
using Corvus.UriTemplates.TavisApi;

const string uriTemplate = "http://example.org/location{?value*}";
var template = new UriTemplate("https://example.org/{?foo,bar}", true);
template.AddParameter("foo", 42);
Console.WriteLine(template.Resolve());

using var jsonValues = JsonDocument.Parse("{\"value\": { \"foo\": \"bar\", \"bar\": 3.4, \"baz\": null }}");
Dictionary<string, string> value = new() { { "foo", "bar" }, { "bar", "baz" }, { "baz", "bob" } };
Dictionary<string, object?> parameters = new() { { "value", value } };
Tavis.UriTemplates.UriTemplate template2 = new("https://example.org/{?foo,bar}", true);
template2.SetParameter("foo", 42);
Console.WriteLine(template2.Resolve());

object? nullState = default;
////const string uriTemplate = "http://example.org/location{?value*}";

JsonUriTemplateResolver.TryResolveResult(uriTemplate.AsSpan(), false, jsonValues.RootElement, HandleResult, ref nullState);
DictionaryUriTemplateResolver.TryResolveResult(uriTemplate.AsSpan(), false, parameters, HandleResult, ref nullState);
////using var jsonValues = JsonDocument.Parse("{\"value\": { \"foo\": \"bar\", \"bar\": 3.4, \"baz\": null }}");
////Dictionary<string, string> value = new() { { "foo", "bar" }, { "bar", "baz" }, { "baz", "bob" } };
////Dictionary<string, object?> parameters = new() { { "value", value } };

static void HandleResult(ReadOnlySpan<char> resolvedTemplate, ref object? state)
{
Console.WriteLine(resolvedTemplate.ToString());
}
////object? nullState = default;

////JsonUriTemplateResolver.TryResolveResult(uriTemplate.AsSpan(), false, jsonValues.RootElement, HandleResult, ref nullState);
////DictionaryUriTemplateResolver.TryResolveResult(uriTemplate.AsSpan(), false, parameters, HandleResult, ref nullState);

////static void HandleResult(ReadOnlySpan<char> resolvedTemplate, ref object? state)
////{
//// Console.WriteLine(resolvedTemplate.ToString());
////}
4 changes: 4 additions & 0 deletions Solutions/Sandbox/Sandbox.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Tavis.UriTemplates" Version="2.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Corvus.UriTemplates.Resolvers.DictionaryOfObject\Corvus.UriTemplates.Resolvers.DictionaryOfObject.csproj" />
<ProjectReference Include="..\Corvus.UriTemplates.Resolvers.Json\Corvus.UriTemplates.Resolvers.Json.csproj" />
Expand Down

0 comments on commit fa7ee75

Please sign in to comment.