Skip to content

Commit

Permalink
Merge pull request #2030 from microsoftgraph/shem/releas_python_path_…
Browse files Browse the repository at this point in the history
…snippet_updates

Shem/releas python path snippet updates
  • Loading branch information
shemogumbe authored Apr 24, 2024
2 parents a1e66e7 + 1ad8718 commit 14f7d76
Show file tree
Hide file tree
Showing 21 changed files with 135 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "apidoctor"]
path = apidoctor
url = https://github.com/OneDrive/apidoctor.git
url = https://github.com/OneDrive/apidoctor.git
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<PackageReference Include="xunit" Version="2.7.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.22.0" />
<PackageReference Include="Microsoft.OData.Core" Version="7.20.0" />
<PackageReference Include="Microsoft.OData.Core" Version="7.21.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<PackageReference Include="xunit" Version="2.7.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.8">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
40 changes: 40 additions & 0 deletions CodeSnippetsReflection.OpenAPI.Test/PythonImportGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace CodeSnippetsReflection.OpenAPI.Test;


public class PythonImportTests : OpenApiSnippetGeneratorTestBase
{
private readonly PythonGenerator _generator = new();
Expand Down Expand Up @@ -74,4 +75,43 @@ public async Task GenerateComplexModelImports(){
Assert.Contains("from msgraph.generated.models.extension import Extension", result);
Assert.Contains("from msgraph.generated.models.open_type_extension import OpenTypeExtension", result);
}

[Fact]
public async Task GenerateNestedRequestBuilderImports()
{
using var requestPayload = new HttpRequestMessage(HttpMethod.Get, $"{ServiceRootUrl}/applications(appId={{application-id}})?$select=id,appId,displayName,requiredResourceAccess");
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1SnippetMetadata());
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains("from msgraph import GraphServiceClient", result);
Assert.Contains("from msgraph.generated.applications_with_app_id.applications_with_app_id_request_builder import ApplicationsWithAppIdRequestBuilder", result);
}
[Fact]
public async Task GenerateRequestBodyImports()
{
string bodyContent = @"
{
""passwordCredential"": {
""displayName"": ""Test-Password friendly name""
}
}";

using var requestPayload = new HttpRequestMessage(HttpMethod.Post, $"{ServiceRootUrl}/applications/{{application-id}}/addPassword"){
Content = new StringContent(bodyContent, Encoding.UTF8, "application/json")
};
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1SnippetMetadata());
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains("from msgraph import GraphServiceClient", result);
Assert.Contains("from msgraph.generated.models.password_credential import PasswordCredential", result);
Assert.Contains("from msgraph.generated.applications.item.add_password.add_password_post_request_body import AddPasswordPostRequestBody", result);
}
[Fact]
public async Task GeneratesImportsWithoutFilterAttrbutesInPath()
{
using var requestPayload = new HttpRequestMessage(HttpMethod.Get, $"{ServiceRootUrl}/servicePrincipals/$count");
requestPayload.Headers.Add("ConsistencyLevel", "eventual");
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1SnippetMetadata());
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains("from msgraph import GraphServiceClient", result);
Assert.Contains("from msgraph.generated.service_principals.count.count_request_builder import CountRequestBuilder", result);
}
}
11 changes: 11 additions & 0 deletions CodeSnippetsReflection.OpenAPI.Test/SnippetImportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,15 @@ public async Task TestGenerateImportTemplatesForRequestBuilderImports()
Assert.NotNull(result[0].Path);
Assert.NotNull(result[0].RequestBuilderName);
}
[Fact]
public async Task TestGenerateImportTemplatesForNestedRequestBuilderImports()
{
using var requestPayload = new HttpRequestMessage(HttpMethod.Get, $"{ServiceRootUrl}/applications(appId={{application-id}})?$select=id,appId,displayName,requiredResourceAccess");
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1SnippetMetadata());
var result = ImportsGenerator.GenerateImportTemplates(snippetModel);
Assert.NotNull(result);
Assert.IsType<List<ImportTemplate>>(result);
Assert.NotNull(result[0].Path);
Assert.NotNull(result[0].RequestBuilderName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using CodeSnippetsReflection.OpenAPI.ModelGraph;
using CodeSnippetsReflection.StringExtensions;
using Microsoft.OpenApi.Services;
using System.Text.RegularExpressions;


namespace CodeSnippetsReflection.OpenAPI.LanguageGenerators
{
Expand Down Expand Up @@ -89,10 +91,18 @@ private static HashSet<string> GetImportStatements(SnippetModel snippetModel)
{
case ImportKind.Model:
var typeDefinition = import.ModelProperty.TypeDefinition;
if (typeDefinition != null)
{
snippetImports.Add($"{modelImportPrefix}.{typeDefinition.ToSnakeCase()} import {typeDefinition}");
if (typeDefinition != null){
if(typeDefinition.EndsWith("RequestBody",StringComparison.OrdinalIgnoreCase)){
var namespaceParts = import.ModelProperty.NamespaceName.Split('.').Select((s, i) => i == import.ModelProperty.NamespaceName.Split('.').Length - 1 ? s.ToSnakeCase() : s.ToLowerInvariant());
var importString = $"{requestBuilderImportPrefix}.{string.Join(".", namespaceParts)}.{typeDefinition.ToSnakeCase()} import {typeDefinition}";
snippetImports.Add($"{importString.Replace(".me.", ".users.item.")}");
}
else{
snippetImports.Add($"{modelImportPrefix}.{typeDefinition.ToSnakeCase()} import {typeDefinition}");
}
}


break;
case ImportKind.RequestBuilder:
if (!string.IsNullOrEmpty(import.ModelProperty.Name))
Expand All @@ -102,11 +112,13 @@ private static HashSet<string> GetImportStatements(SnippetModel snippetModel)
snippetImports.Add(importString.Replace(".me.", ".users.item."));
}
break;


case ImportKind.Path:
if (import.Path != null && import.RequestBuilderName != null)
{
//construct path to request builder
snippetImports.Add($"{requestBuilderImportPrefix}{import.Path.Replace(".me.", ".users.item.")}.{import.RequestBuilderName.ToSnakeCase()} import {import.RequestBuilderName}");
snippetImports.Add($"{requestBuilderImportPrefix}{Regex.Replace(import.Path.Replace(".me.", ".users.item."), @"(\B[A-Z])", "_$1", RegexOptions.Compiled, TimeSpan.FromSeconds(60)).ToLower()}.{import.RequestBuilderName.ToSnakeCase()} import {import.RequestBuilderName}");
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public enum ImportKind
{
Path,
RequestBuilder,

RequestBody,
Model
}

Expand All @@ -34,6 +36,11 @@ public string RequestBuilderName
{
get; set;
}
public string RequestBodyName
{
get; set;
}

public string HttpMethod
{
get; set;
Expand All @@ -52,18 +59,19 @@ public static List<ImportTemplate> GenerateImportTemplates(SnippetModel snippet
{
var className = codeGraph.Nodes.Last().GetClassName().ToFirstCharacterUpperCase();
var itemSuffix = codeGraph.Nodes.Last().Segment.IsCollectionIndex() ? "Item" : string.Empty;
var requestBuilderName = $"{className}{itemSuffix}RequestBuilder";
var requestBuilderName = $"{className}{itemSuffix}RequestBuilder";
if (codeGraph.Nodes.Last().Path != null)
{
var path = codeGraph.Nodes.Last().Path;
imports.Add(new ImportTemplate
{
Kind = ImportKind.Path,
Path = Regex.Replace(path.Replace("\\", ".").Replace("()", ""), @"\{[^}]*-id\}", "item", RegexOptions.Compiled, TimeSpan.FromSeconds(60)),
Path = Regex.Replace(path.Replace("\\", ".").Replace("()", ""), @"\{[^}]*-id\}", "item", RegexOptions.Compiled, TimeSpan.FromSeconds(60)).CleanUpImportPath().Replace("__", "_"),
RequestBuilderName = requestBuilderName,
HttpMethod = codeGraph.HttpMethod.ToString()
});
}

}
AddModelImportTemplates(codeGraph.Body, imports);
return imports;
Expand Down
30 changes: 28 additions & 2 deletions CodeSnippetsReflection/StringExtensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Text;
using System.Linq;
using System;
using System.Text.RegularExpressions;


namespace CodeSnippetsReflection.StringExtensions
{
Expand All @@ -14,6 +16,9 @@ public static class StringExtensions
/// <param name="doubleQuoteEscapeSequence">The string value to replace double-quotes</param>
/// <param name="singleQuoteEscapeSequence">The string value to replace single-quotes</param>
/// <returns></returns>
private static readonly Regex ImportPathRegex = new Regex(@"(\w+)[A-Z]?(\w*)\((\w+Id)='(\{[^{}]+\})'\)", RegexOptions.Compiled, TimeSpan.FromMilliseconds(200));
private static readonly Regex SnakeCaseRegex = new Regex(@"(\B[A-Z])", RegexOptions.Compiled, TimeSpan.FromMilliseconds(200));

public static string EscapeQuotesInLiteral(this string stringLiteral,
string doubleQuoteEscapeSequence,
string singleQuoteEscapeSequence)
Expand Down Expand Up @@ -59,7 +64,7 @@ public static string ToPascalCase(this string str)
return pascalCaseString;
}

public static string ToSnakeCase(this string str)
public static string ToSnakeCase(this string str)
{
if (string.IsNullOrEmpty(str)) return str;
StringBuilder snakeCaseBuilder = new StringBuilder();
Expand Down Expand Up @@ -87,7 +92,28 @@ public static string ToSnakeCase(this string str)
return snakeCaseBuilder.ToString();
}



public static string CleanUpImportPath(this string input)
{
string result = StringExtensions.ImportPathRegex.Replace(input, m =>
{
string firstPart = m.Groups[1].Value;
string secondPart = m.Groups[2].Value;
string idPart = m.Groups[3].Value;

// Given Id e.g appIdd, groupID - convert to snake case
secondPart = StringExtensions.SnakeCaseRegex.Replace(secondPart, x => "_" + x.Value.ToLower());
idPart = StringExtensions.SnakeCaseRegex.Replace(idPart, x => "_" + x.Value.ToLower());

return $"{firstPart}_{secondPart}_with_{idPart}";
});

result = result.Replace("$", "");

return result;
}


public static string EscapeQuotes(this string stringValue)
{
return stringValue.Replace("\\\"", "\"")//try to unescape quotes in case the input string is already escaped to avoid double escaping.
Expand Down
6 changes: 3 additions & 3 deletions ExceptionMiddleware/ExceptionMiddleware.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.OData.Edm" Version="7.20.0" />
<PackageReference Include="Microsoft.OData.Edm" Version="7.21.0" />
<PackageReference Include="MSTest.TestFramework" Version="3.3.1" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<PackageReference Include="xunit" Version="2.7.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.8">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
4 changes: 2 additions & 2 deletions FileService.Test/FileService.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<PackageReference Include="xunit" Version="2.7.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.8">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
6 changes: 3 additions & 3 deletions GraphWebApi/GraphWebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@
<PackageReference Include="Microsoft.ApplicationInsights.Web" Version="2.22.0" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.ApplicationInsights.HostingStartup" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="8.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="8.0.4" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.OData.Edm" Version="7.20.0" />
<PackageReference Include="Microsoft.OData.Edm" Version="7.21.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.20.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.2" />
<PackageReference Include="Serilog" Version="3.1.1" />
Expand Down
4 changes: 2 additions & 2 deletions KnownIssuesService.Test/KnownIssuesService.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<PackageReference Include="xunit" Version="2.7.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.8">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
4 changes: 2 additions & 2 deletions OpenAPIService.Test/OpenAPIService.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.OpenApi" Version="1.6.14" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<PackageReference Include="xunit" Version="2.7.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
2 changes: 1 addition & 1 deletion OpenAPIService/OpenAPIService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.0" />
<PackageReference Include="Microsoft.OData.Edm" Version="7.20.0" />
<PackageReference Include="Microsoft.OData.Edm" Version="7.21.0" />
<PackageReference Include="Microsoft.OpenApi" Version="1.6.14" />
<PackageReference Include="Microsoft.OpenApi.OData" Version="1.6.1" />
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.6.14" />
Expand Down
4 changes: 2 additions & 2 deletions PermissionsService.Test/PermissionsService.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<PackageReference Include="xunit" Version="2.7.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
4 changes: 2 additions & 2 deletions SamplesService.Test/SamplesService.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<PackageReference Include="xunit" Version="2.7.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
4 changes: 2 additions & 2 deletions TelemetryService.Test/TelemetrySanitizerService.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.22.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<PackageReference Include="xunit" Version="2.7.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.8">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
4 changes: 2 additions & 2 deletions UriMatchService.Test/UriMatchingService.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<PackageReference Include="xunit" Version="2.7.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Loading

0 comments on commit 14f7d76

Please sign in to comment.