Skip to content

Commit

Permalink
Merge dev to master (#2013)
Browse files Browse the repository at this point in the history
Bump apidoctor from 1708be5 to 1916d8c (#2004)

Add use import statements for PHP Snippets (#2000)

Bump Microsoft.OpenApi.OData from 1.6.0 to 1.6.1 (#2007)

Bump Microsoft.VisualStudio.Azure.Containers.Tools.Targets (#2008)

Bump MSTest.TestFramework from 3.2.2 to 3.3.0 (#2009)

Bump MSTest.TestFramework from 3.3.0 to 3.3.1 (#2011)

Bump Microsoft.VisualStudio.Azure.Containers.Tools.Targets (#2010)

Chore: change drop location (#2012)
  • Loading branch information
thewahome authored Apr 8, 2024
1 parent 892f922 commit a1e66e7
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 14 deletions.
1 change: 1 addition & 0 deletions CodeSnippetsReflection.OpenAPI.Test/PhpGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public async Task GeneratesTheCorrectFluentApiPathForIndexedCollections()
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1SnippetMetadata());
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains("->me()->messages()->byMessageId('message-id')", result);

}

[Fact]
Expand Down
78 changes: 78 additions & 0 deletions CodeSnippetsReflection.OpenAPI.Test/PhpImportGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using CodeSnippetsReflection.OpenAPI.LanguageGenerators;
using Xunit;

namespace CodeSnippetsReflection.OpenAPI.Test;

public class PhpImportTests : OpenApiSnippetGeneratorTestBase
{
private readonly PhpGenerator _generator = new();

[Fact]
public async Task GeneratesRequestBuilderImports()
{
using var requestPayload = new HttpRequestMessage(HttpMethod.Get, $"{ServiceRootUrl}/me/calendar/events?$filter=startsWith(subject,'All')");
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1SnippetMetadata());
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains("use Microsoft\\Graph\\GraphServiceClient;", result);
Assert.Contains("use Microsoft\\Graph\\Generated\\Users\\Item\\Calendar\\Events\\EventsRequestBuilderGetRequestConfiguration;", result);
}

[Fact]
public async Task GenerateModelImports(){
var bodyContent = @"{
""displayName"": ""New display name""
}";
using var requestPayload = new HttpRequestMessage(HttpMethod.Patch, $"{ServiceRootUrl}/applications/{{id}}")
{
Content = new StringContent(bodyContent, Encoding.UTF8, "application/json")
};
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1SnippetMetadata());
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains("use Microsoft\\Graph\\GraphServiceClient;", result);
Assert.Contains("use Microsoft\\Graph\\Generated\\Models\\Application;", result);

}
[Fact]
public async Task GenerateComplexModelImports(){
var bodyContent = @"{
""subject"": ""Annual review"",
""body"": {
""contentType"": ""HTML"",
""content"": ""You should be proud!""
},
""toRecipients"": [
{
""emailAddress"": {
""address"": ""rufus@contoso.com""
}
}
],
""extensions"": [
{
""@odata.type"": ""microsoft.graph.openTypeExtension"",
""extensionName"": ""Com.Contoso.Referral"",
""companyName"": ""Wingtip Toys"",
""expirationDate"": ""2015-12-30T11:00:00.000Z"",
""dealValue"": 10000
}
]
}";
using var requestPayload = new HttpRequestMessage(HttpMethod.Post, $"{ServiceRootUrl}/me/messages/")
{
Content = new StringContent(bodyContent, Encoding.UTF8, "application/json")
};
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1SnippetMetadata());
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains("use Microsoft\\Graph\\GraphServiceClient;", result);
Assert.Contains("use Microsoft\\Graph\\Generated\\Models\\Message;", result);
Assert.Contains("use Microsoft\\Graph\\Generated\\Models\\ItemBody;", result);
Assert.Contains("use Microsoft\\Graph\\Generated\\Models\\Recipient;", result);
Assert.Contains("use Microsoft\\Graph\\Generated\\Models\\EmailAddress;", result);
Assert.Contains("use Microsoft\\Graph\\Generated\\Models\\Extension;", result);
Assert.Contains("use Microsoft\\Graph\\Generated\\Models\\OpenTypeExtension;", result);
}
}
6 changes: 2 additions & 4 deletions CodeSnippetsReflection.OpenAPI.Test/SnippetImportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public async Task TestGenerateImportTemplatesForModelImports()
Content = new StringContent(userJsonObject, Encoding.UTF8, "application/json")
};
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1SnippetMetadata());
var importsGenerator = new ImportsGenerator();
var result = importsGenerator.GenerateImportTemplates(snippetModel);
var result = ImportsGenerator.GenerateImportTemplates(snippetModel);
Assert.NotNull(result);
Assert.IsType<List<ImportTemplate>>(result);
Assert.Equal(2, result.Count);
Expand All @@ -36,8 +35,7 @@ public async Task TestGenerateImportTemplatesForRequestBuilderImports()
{
using var requestPayload = new HttpRequestMessage(HttpMethod.Get, $"{ServiceRootUrl}/me/calendar/events?$filter=startsWith(subject,'All')");
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1SnippetMetadata());
var importsGenerator = new ImportsGenerator();
var result = importsGenerator.GenerateImportTemplates(snippetModel);
var result = ImportsGenerator.GenerateImportTemplates(snippetModel);
Assert.NotNull(result);
Assert.IsType<List<ImportTemplate>>(result);
Assert.NotNull(result[0].Path);
Expand Down
45 changes: 45 additions & 0 deletions CodeSnippetsReflection.OpenAPI/LanguageGenerators/PhpGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,54 @@ public string GenerateCodeSnippet(SnippetModel snippetModel)
WriteObjectProperty(RequestBodyVarName, payloadSb, codeGraph.Body, indentManager);
}
WriteRequestExecutionPath(codeGraph, payloadSb, indentManager);
var importStatements = GetImportStatements(snippetModel);
var phpTagIndex = payloadSb.ToString().IndexOf("<?php");
if (phpTagIndex >= 0)
{
var insertPosition = phpTagIndex + "<?php".Length + Environment.NewLine.Length;
payloadSb.Insert(insertPosition, string.Join(Environment.NewLine, importStatements) + Environment.NewLine);
}
else
{
payloadSb.Insert(0, string.Join(Environment.NewLine, importStatements) + Environment.NewLine);
}
return payloadSb.ToString().Trim();
}
private static HashSet<string> GetImportStatements(SnippetModel snippetModel)
{
const string modelImportPrefix = "use Microsoft\\Graph\\Generated\\Models";
const string requestBuilderImportPrefix = "use Microsoft\\Graph\\Generated";

var snippetImports = new HashSet<string>();

snippetImports.Add("use Microsoft\\Graph\\GraphServiceClient;");

var imports = ImportsGenerator.GenerateImportTemplates(snippetModel);
foreach (var import in imports)
{
switch (import.Kind)
{
case ImportKind.Model:
var typeDefinition = import.ModelProperty.TypeDefinition;
if (typeDefinition != null){
snippetImports.Add($"{modelImportPrefix}\\{typeDefinition};");
// check if model has a nested namespace and append it to the import statement
}
break;

case ImportKind.Path:
if (!string.IsNullOrEmpty(import.Path) && !string.IsNullOrEmpty(import.RequestBuilderName))
{
//construct path to request builder
var importPath = import.Path.Split('.')
.Select(static s => s.ToFirstCharacterUpperCase()).ToArray();
snippetImports.Add($"{requestBuilderImportPrefix}{string.Join("\\", importPath).Replace("\\Me\\", "\\Users\\Item\\")}\\{import.RequestBuilderName}{import.HttpMethod.ToLowerInvariant().ToFirstCharacterUpperCase()}RequestConfiguration;");
}
break;
}
}
return snippetImports;
}
private static void WriteRequestExecutionPath(SnippetCodeGraph codeGraph, StringBuilder payloadSb, IndentManager indentManager)
{
var method = codeGraph.HttpMethod.Method.ToLower();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ private static HashSet<string> GetImportStatements(SnippetModel snippetModel)

snippetImports.Add("from msgraph import GraphServiceClient");

var importsGenerator = new ImportsGenerator();
var imports = importsGenerator.GenerateImportTemplates(snippetModel);
var imports = ImportsGenerator.GenerateImportTemplates(snippetModel);
foreach (var import in imports)
{
switch (import.Kind)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using CodeSnippetsReflection.StringExtensions;
using Microsoft.OpenApi.Services;


namespace CodeSnippetsReflection.OpenAPI.LanguageGenerators
{
public enum ImportKind
Expand Down Expand Up @@ -33,13 +34,17 @@ public string RequestBuilderName
{
get; set;
}
public string HttpMethod
{
get; set;
}
}

public class ImportsGenerator
public static class ImportsGenerator
{
public List<ImportTemplate> imports = new();
public static List<ImportTemplate> imports = new();

public List<ImportTemplate> GenerateImportTemplates(SnippetModel snippetModel)
public static List<ImportTemplate> GenerateImportTemplates(SnippetModel snippetModel)
{
var codeGraph = new SnippetCodeGraph(snippetModel);
var imports = new List<ImportTemplate>();
Expand All @@ -55,7 +60,8 @@ public List<ImportTemplate> GenerateImportTemplates(SnippetModel snippetModel)
{
Kind = ImportKind.Path,
Path = Regex.Replace(path.Replace("\\", ".").Replace("()", ""), @"\{[^}]*-id\}", "item", RegexOptions.Compiled, TimeSpan.FromSeconds(60)),
RequestBuilderName = requestBuilderName
RequestBuilderName = requestBuilderName,
HttpMethod = codeGraph.HttpMethod.ToString()
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion ExceptionMiddleware/ExceptionMiddleware.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.OData.Edm" Version="7.20.0" />
<PackageReference Include="MSTest.TestFramework" Version="3.2.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.3.1" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
2 changes: 1 addition & 1 deletion GraphWebApi/GraphWebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="8.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.OData.Edm" Version="7.20.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
<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" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
Expand Down
2 changes: 1 addition & 1 deletion OpenAPIService/OpenAPIService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.0" />
<PackageReference Include="Microsoft.OData.Edm" Version="7.20.0" />
<PackageReference Include="Microsoft.OpenApi" Version="1.6.14" />
<PackageReference Include="Microsoft.OpenApi.OData" Version="1.6.0" />
<PackageReference Include="Microsoft.OpenApi.OData" Version="1.6.1" />
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.6.14" />
</ItemGroup>

Expand Down
1 change: 1 addition & 0 deletions pipelines/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,4 @@ extends:
parameters:
directory: 'microsoft-graph-devx-api'
repoName: ReleasePipelines
dependsOn: ['Build']

0 comments on commit a1e66e7

Please sign in to comment.