Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate case-insensitive PowerShell hashtable for request bodies #1460

Merged
merged 1 commit into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ public async Task GeneratesSnippetForRequestWithBody()
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1TreeNode());
var result = _generator.GenerateCodeSnippet(snippetModel);
var expectedParams = $"$params = @{{{Environment.NewLine}\t" +
$"DisplayName = \"Melissa Darrow\"{Environment.NewLine}\t" +
$"City = \"Seattle\"{Environment.NewLine}}}";
$"displayName = \"Melissa Darrow\"{Environment.NewLine}\t" +
$"city = \"Seattle\"{Environment.NewLine}}}";
Assert.Contains(expectedParams, result);
Assert.Contains("-BodyParameter $params", result);
}
Expand All @@ -302,8 +302,8 @@ public async Task GeneratesSnippetForRequestWithNestedBody()
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1TreeNode());
var result = _generator.GenerateCodeSnippet(snippetModel);
var expectedParams = $"$params = @{{{Environment.NewLine}\t" +
$"DisplayName = \"Melissa Darrow\"{Environment.NewLine}\t" +
$"City = \"Seattle\"{Environment.NewLine}\t" +
$"displayName = \"Melissa Darrow\"{Environment.NewLine}\t" +
$"city = \"Seattle\"{Environment.NewLine}\t" +
$"PasswordProfile = @{{{Environment.NewLine}\t\t" +
$"Password = \"2d79ba3a-b03a-9ed5-86dc-79544e262664\"{Environment.NewLine}\t\t" +
$"ForceChangePasswordNextSignIn = $false{Environment.NewLine}\t" +
Expand All @@ -326,8 +326,8 @@ public async Task GeneratesSnippetForRequestWithArrayInBody()
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1TreeNode());
var result = _generator.GenerateCodeSnippet(snippetModel);
var expectedParams = $"$params = @{{{Environment.NewLine}\t" +
$"DisplayName = \"Library Assist\"{Environment.NewLine}\t" +
$"GroupTypes = @({Environment.NewLine}\t\t" +
$"displayName = \"Library Assist\"{Environment.NewLine}\t" +
$"groupTypes = @({Environment.NewLine}\t\t" +
$"\"Unified\"{Environment.NewLine}\t\t" +
$"\"DynamicMembership\"{Environment.NewLine}\t" +
$"){Environment.NewLine}" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ public class PowerShellGenerator : ILanguageGenerator<SnippetModel, OpenApiUrlTr
private const string modulePrefix = "Microsoft.Graph";
private const string mgCommandMetadataUrl = "https://raw.githubusercontent.com/microsoftgraph/msgraph-sdk-powershell/dev/src/Authentication/Authentication/custom/common/MgCommandMetadata.json";
private readonly Lazy<IList<PowerShellCommandInfo>> psCommands = new(
() => {
() =>
{
using var httpClient = new HttpClient();
using var stream = httpClient.GetStreamAsync(mgCommandMetadataUrl).GetAwaiter().GetResult();
return JsonSerializer.Deserialize<IList<PowerShellCommandInfo>>(stream);
},
LazyThreadSafetyMode.PublicationOnly
);
private static Regex meSegmentRegex = new("^/me($|(?=/))", RegexOptions.Compiled, TimeSpan.FromSeconds(5));
private static readonly Regex meSegmentRegex = new("^/me($|(?=/))", RegexOptions.Compiled, TimeSpan.FromSeconds(5));
public string GenerateCodeSnippet(SnippetModel snippetModel)
{
var indentManager = new IndentManager();
Expand Down Expand Up @@ -59,7 +60,7 @@ public string GenerateCodeSnippet(SnippetModel snippetModel)
var commandParameters = GetCommandParameters(snippetModel, payloadVarName);
if (!string.IsNullOrEmpty(commandParameters))
snippetBuilder.Append($"{commandParameters}");
if (RequiresMIMEContentOutPut(snippetModel,path))
if (RequiresMIMEContentOutPut(snippetModel, path))
{
//Allows genration of an output file for MIME content of the message
snippetBuilder.Append($" -OutFile $outFileId");
Expand Down Expand Up @@ -189,7 +190,8 @@ private static string GetQueryParameterValue(string normalizedParameterName, str
private static string NormalizeQueryParameterName(string queryParam)
{
string psParameterName = queryParam.TrimStart('$').ToLower().ToFirstCharacterUpperCase();
return psParameterName switch {
return psParameterName switch
{
"Select" => "Property",
"Expand" => "ExpandProperty",
"Count" => "CountVariable",
Expand All @@ -198,7 +200,7 @@ private static string NormalizeQueryParameterName(string queryParam)
};
}

private static Regex nestedStatementRegex = new(@"(\w+|\w+\/\w+)(\([^)]+\))", RegexOptions.IgnoreCase | RegexOptions.Compiled, TimeSpan.FromSeconds(5));
private static readonly Regex nestedStatementRegex = new(@"(\w+|\w+\/\w+)(\([^)]+\))", RegexOptions.IgnoreCase | RegexOptions.Compiled, TimeSpan.FromSeconds(5));
private static (string, Dictionary<string, string>) ReplaceNestedOdataQueryParameters(string queryParams)
{
var replacements = new Dictionary<string, string>();
Expand All @@ -214,7 +216,7 @@ private static (string, Dictionary<string, string>) ReplaceNestedOdataQueryParam
return (queryParams, replacements);
}

private static Regex keyIndexRegex = new(@"(?<={)(.*?)(?=})", RegexOptions.Compiled, TimeSpan.FromSeconds(5));
private static readonly Regex keyIndexRegex = new(@"(?<={)(.*?)(?=})", RegexOptions.Compiled, TimeSpan.FromSeconds(5));
private IList<PowerShellCommandInfo> GetCommandForRequest(string path, string method, string apiVersion)
{
if (psCommands.Value.Count == 0)
Expand Down Expand Up @@ -258,7 +260,7 @@ private static void WriteJsonObjectValue(StringBuilder payloadSB, JsonElement va
.Select(x => new Tuple<JsonProperty, OpenApiSchema>(x, schema?.GetPropertySchema(x.Name)));
foreach (var propertyAndSchema in propertiesAndSchema)
{
var propertyName = propertyAndSchema.Item1.Name.ToFirstCharacterUpperCase();
var propertyName = propertyAndSchema.Item1.Name;
// Enclose in quotes if property name contains a non-word character.
if (Regex.IsMatch(propertyName, "\\W", RegexOptions.None, TimeSpan.FromSeconds(5))) { propertyName = $"\"{propertyName}\""; }
var propertyAssignment = includePropertyAssignment ? $"{indentManager.GetIndent()}{propertyName} = " : string.Empty;
Expand Down