Skip to content

Commit

Permalink
Lookup QueryParameters from schema (#1575)
Browse files Browse the repository at this point in the history
* Fixes parameter passing and query name/type handling

* Fix tests
  • Loading branch information
andrueastman authored May 31, 2023
1 parent 4d0ba04 commit d169b5c
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 24 deletions.
4 changes: 2 additions & 2 deletions CodeSnippetsReflection.OData.Test/CSharpGeneratorShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public void DoesNotFlattenNestedODataQueries()
// Act by generating the code snippet
var result = new CSharpGenerator(_edmModel.Value).GenerateCodeSnippet(snippetModel, expressions);

Assert.Contains(".Expand(\"singleValueExtendedProperties($filter=id%20eq%20'%7Bid_value%7D')\")", result);
Assert.Contains(".Expand(\"singleValueExtendedProperties($filter=id eq '{id_value}')\")", result);
Assert.DoesNotContain(".Filter(", result);
}

Expand All @@ -242,7 +242,7 @@ public void GeneratesTopLevelExpandAndFilterTogether()
// Act by generating the code snippet
var result = new CSharpGenerator(_edmModel.Value).GenerateCodeSnippet(snippetModel, expressions);

Assert.Contains(".Expand(\"singleValueExtendedProperties($filter=id%20eq%20'%7Bid_value1%7D')\")", result);
Assert.Contains(".Expand(\"singleValueExtendedProperties($filter=id eq '{id_value1}')\")", result);
Assert.Contains(".Filter(\"id eq '{id_value2}'\")", result);
}

Expand Down
30 changes: 29 additions & 1 deletion CodeSnippetsReflection.OpenAPI.Test/CSharpGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Net.Http;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using CodeSnippetsReflection.OpenAPI.LanguageGenerators;
Expand Down Expand Up @@ -215,6 +215,24 @@ public async Task GeneratesFilterParameters() {
Assert.Contains("requestConfiguration.QueryParameters.Orderby", result);
}
[Fact]
public async Task GeneratesFilterParametersWithSpecialCharacters() {
using var requestPayload = new HttpRequestMessage(HttpMethod.Get, $"{ServiceRootUrl}/users?$filter=imAddresses/any(i:i eq 'admin@contoso.com')");
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1TreeNode());
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains("requestConfiguration.QueryParameters.Filter", result);
Assert.Contains("imAddresses/any(i:i eq 'admin@contoso.com')", result);
}
[Fact]
public async Task GeneratesSnippetForRequestWithSearchQueryOptionWithANDLogicalConjuction()
{
using var requestPayload = new HttpRequestMessage(HttpMethod.Get, $"{ServiceRootUrl}/users?$search=\"displayName:di\" AND \"displayName:al\"");
requestPayload.Headers.Add("ConsistencyLevel", "eventual");
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1TreeNode());
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains("requestConfiguration.QueryParameters.Search", result);
Assert.Contains("requestConfiguration.QueryParameters.Search = \"\\\"displayName:di\\\" AND \\\"displayName:al\\\"\";", result);
}
[Fact]
public async Task HandlesOdataTypeWhenGenerating() {
var sampleJson = @"
{
Expand Down Expand Up @@ -640,6 +658,16 @@ public async Task CorrectlyHandlesDateInUrl()

Assert.Contains("await graphClient.Reports.GetYammerGroupsActivityDetailWithDate(new Date(DateTime.Parse(\"{date}\"))).GetAsync();", result);
}

[Fact]
public async Task CorrectlyHandlesTypeFromInUrl()
{
using var requestPayload = new HttpRequestMessage(HttpMethod.Get, $"{ServiceRootUrl}/me/mailFolders/?includehiddenfolders=true");
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1TreeNode());
var result = _generator.GenerateCodeSnippet(snippetModel);

Assert.Contains("requestConfiguration.QueryParameters.IncludeHiddenFolders = \"true\";", result);
}

[Fact]
public async Task MatchesPathWithPathParameter()
Expand Down
13 changes: 8 additions & 5 deletions CodeSnippetsReflection.OpenAPI.Test/SnippetCodeGraphTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
Expand Down Expand Up @@ -218,7 +218,7 @@ public async Task ParsesParametersWithExpressionsCorrectly()
Assert.True(result.HasParameters());
var param = result.Parameters.First();
Assert.Equal("filter",param.Name);
Assert.Equal("groupTypes/any",param.Value); // TODO check if result should be `groupTypes/any(c:c+eq+'Unified')`
Assert.Equal("groupTypes/any(c:c+eq+'Unified')",param.Value);
}

[Fact]
Expand Down Expand Up @@ -273,12 +273,15 @@ public async Task ParsesQueryParametersWithSpaces()
Assert.True(result.HasParameters());
Assert.Equal(2, result.Parameters.Count());

var expectedProperty1 = new CodeProperty { Name = "filter" , Value = "roleDefinitionId eq '62e90394-69f5-4237-9190-012177145e10'", PropertyType = PropertyType.String };
var expectedProperty1 = new CodeProperty { Name = "filter" , Value = "roleDefinitionId eq '62e90394-69f5-4237-9190-012177145e10'", PropertyType = PropertyType.String, Children = new List<CodeProperty>()};
var actualParam1 = result.Parameters.First();

Assert.Equal(expectedProperty1, actualParam1);
Assert.Equal(expectedProperty1.Name, actualParam1.Name);
Assert.Equal(expectedProperty1.Value, actualParam1.Value);
Assert.Equal(expectedProperty1.PropertyType, actualParam1.PropertyType);
Assert.Equal(expectedProperty1.Children.Count, actualParam1.Children.Count);

var expectedProperty2 = new CodeProperty { Value = "principal", PropertyType = PropertyType.String };
var expectedProperty2 = new CodeProperty { Value = "principal", PropertyType = PropertyType.String};
var actualParam2 = result.Parameters.Skip(1).First();

Assert.Equal("expand", actualParam2.Name);
Expand Down
62 changes: 48 additions & 14 deletions CodeSnippetsReflection.OpenAPI/ModelGraph/SnippetCodeGraph.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.Specialized;
Expand All @@ -17,7 +17,7 @@ namespace CodeSnippetsReflection.OpenAPI.ModelGraph
public record SnippetCodeGraph
{

private static readonly Regex nestedStatementRegex = new(@"(\w+)(\([^)]+\))", RegexOptions.IgnoreCase | RegexOptions.Compiled, TimeSpan.FromSeconds(5));
private static readonly Regex nestedStatementRegex = new(@"([/\w]+)(\([^)]+\))", RegexOptions.IgnoreCase | RegexOptions.Compiled, TimeSpan.FromSeconds(5));

private static readonly Regex splitCommasExcludingBracketsRegex = new(@"([^,\(\)]+(\(.*?\))*)+", RegexOptions.IgnoreCase | RegexOptions.Compiled, TimeSpan.FromSeconds(5));

Expand Down Expand Up @@ -153,6 +153,13 @@ private static IEnumerable<CodeProperty> parseHeaders(SnippetModel snippetModel)
private static List<CodeProperty> parseParameters(SnippetModel snippetModel)
{

var queryParameters = snippetModel.EndPathNode
.PathItems
.SelectMany(static pathItem => pathItem.Value.Operations)
.Where(operation => operation.Key.ToString().Equals(snippetModel.Method.ToString(), StringComparison.OrdinalIgnoreCase)) // get the operations that match the method
.SelectMany(static operation => operation.Value.Parameters)
.Where(static parameter => parameter.In == ParameterLocation.Query); // find the parameters in the path

var ArrayParameters = ImmutableHashSet.Create(StringComparer.OrdinalIgnoreCase,"select", "expand", "orderby");
var NumberParameters = ImmutableHashSet.Create(StringComparer.OrdinalIgnoreCase,"skip", "top");

Expand All @@ -164,20 +171,47 @@ private static List<CodeProperty> parseParameters(SnippetModel snippetModel)
NameValueCollection queryCollection = HttpUtility.ParseQueryString(queryString);
foreach (String key in queryCollection.AllKeys)
{
//try to lookup the parameter from the schema
var queryParam = queryParameters.FirstOrDefault(param =>
NormalizeQueryParameterName(param.Name).Equals(NormalizeQueryParameterName(key),
StringComparison.OrdinalIgnoreCase));

//setup defaults
var name = NormalizeQueryParameterName(key).Trim();
var value = GetQueryParameterValue(queryCollection[key], replacements);
if(ArrayParameters.Contains(name)){
var children = splitCommasExcludingBracketsRegex.Split(value)
.Where(x => !String.IsNullOrEmpty(x) && !x.StartsWith("(") && !x.Equals(","))
.Select(x => new CodeProperty() { Name = null, Value = x, PropertyType = PropertyType.String }).ToList();

parameters.Add(new() { Name = name.ToLower(), Value = null, PropertyType = PropertyType.Array , Children = children});
}else if (value.Equals("true", StringComparison.OrdinalIgnoreCase) || value.Equals("false", StringComparison.OrdinalIgnoreCase)){
parameters.Add(new() { Name = name.ToLower(), Value = value, PropertyType = PropertyType.Boolean });
}else if (NumberParameters.Contains(name)){
parameters.Add(new() { Name = name.ToLower(), Value = value, PropertyType = PropertyType.Int32 });
}else{
parameters.Add(new() { Name = name, Value = value, PropertyType = PropertyType.String });
var schema = "string";
if (NumberParameters.Contains(name))
schema = "integer";
else if(ArrayParameters.Contains(name))
schema = "array";

// use values from the schema if match is found
if (queryParam != null)
{
schema = queryParam.Schema.Type.ToLowerInvariant();
name = NormalizeQueryParameterName(queryParam.Name).Trim();
}

switch (schema)
{
case "string":
parameters.Add(evaluateStringProperty(name, value, queryParam?.Schema));
break;
case "integer":
parameters.Add(new CodeProperty { Name = name, Value = int.TryParse(value, out _) ? value : "1", PropertyType = PropertyType.Int32, Children = new List<CodeProperty>() });
break;
case "double":
parameters.Add(new CodeProperty { Name = name, Value = double.TryParse(value, out _) ? value : "1.0d", PropertyType = PropertyType.Double, Children = new List<CodeProperty>() });
break;
case "boolean":
parameters.Add(new CodeProperty { Name = name, Value = bool.TryParse(value, out _) ? value : "false", PropertyType = PropertyType.Boolean, Children = new List<CodeProperty>() });
break;
case "array":
var children = splitCommasExcludingBracketsRegex.Split(GetQueryParameterValue(queryCollection[key], replacements))
.Where(x => !String.IsNullOrEmpty(x) && !x.StartsWith("(") && !x.Equals(","))
.Select(x => new CodeProperty() { Name = null, Value = x, PropertyType = PropertyType.String }).ToList();
parameters.Add(new CodeProperty { Name = name, Value = null, PropertyType = PropertyType.Array, Children = children });
break;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion CodeSnippetsReflection/SnippetBaseModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected SnippetBaseModel(HttpRequestMessage requestPayload, string serviceRoot

this.Method = requestPayload.Method;
this.Path = Uri.UnescapeDataString(requestPayload.RequestUri.AbsolutePath.Substring(5));
this.QueryString = requestPayload.RequestUri.Query;
this.QueryString = Uri.UnescapeDataString(requestPayload.RequestUri.Query);
this.ApiVersion = serviceRootUrl.Substring(serviceRootUrl.Length - 4);
this.SelectFieldList = new List<string>();
this.FilterFieldList = new List<string>();
Expand Down
3 changes: 2 additions & 1 deletion CodeSnippetsReflection/StringExtensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public static string ToSnakeCase(this string str)

public static string EscapeQuotes(this string stringValue)
{
return stringValue.Replace("\"", "\\\"");
return stringValue.Replace("\\\"", "\"")//try to unescape quotes in case the input string is already escaped to avoid double escaping.
.Replace("\"", "\\\"");
}

public static string AddQuotes(this string stringValue)
Expand Down

0 comments on commit d169b5c

Please sign in to comment.