Skip to content

Commit

Permalink
Merge pull request #1968 from microsoftgraph/dev
Browse files Browse the repository at this point in the history
Merge dev into master
  • Loading branch information
millicentachieng committed Feb 29, 2024
2 parents a000322 + a755933 commit 8f49bed
Show file tree
Hide file tree
Showing 64 changed files with 518 additions and 514 deletions.
2 changes: 1 addition & 1 deletion CodeSnippetsPipeline.Test/CodeSnippetsPipeline.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion CodeSnippetsReflection.OData.Test/CSharpGeneratorShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class CSharpGeneratorShould
private const string ServiceRootUrlBeta = "https://graph.microsoft.com/beta";
private readonly Lazy<IEdmModel> _edmModel = new Lazy<IEdmModel>(() => CsdlReader.Parse(XmlReader.Create(CommonGeneratorShould.CleanV1Metadata)));
private readonly Lazy<IEdmModel> _edmModelBeta = new Lazy<IEdmModel>(() => CsdlReader.Parse(XmlReader.Create(CommonGeneratorShould.CleanBetaMetadata)));
private const string AuthProviderPrefix = "GraphServiceClient graphClient = new GraphServiceClient( authProvider );\r\n\r\n";
private const string AuthProviderPrefix = "GraphServiceClient graphClient = new GraphServiceClient(authProvider);\r\n\r\n";

[Fact]
//This tests asserts that we can generate snippets from json objects with nested objects inside them.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
4 changes: 2 additions & 2 deletions CodeSnippetsReflection.OData.Test/JavaGeneratorShould.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using System.Net.Http;
using System.Text.RegularExpressions;
Expand All @@ -14,7 +14,7 @@ public class JavaGeneratorShould
{
private const string ServiceRootUrl = "https://graph.microsoft.com/v1.0";
private readonly IEdmModel _edmModel = CsdlReader.Parse(XmlReader.Create(CommonGeneratorShould.CleanV1Metadata));
private const string AuthProviderPrefix = "GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();\r\n\r\n";
private const string AuthProviderPrefix = "GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider(authProvider).buildClient();\r\n\r\n";

[Fact]
public void MapCorrectTypeForGuidReturnType()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

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

<ItemGroup>
Expand Down
263 changes: 135 additions & 128 deletions CodeSnippetsReflection.OData/LanguageGenerators/CSharpGenerator.cs

Large diffs are not rendered by default.

72 changes: 33 additions & 39 deletions CodeSnippetsReflection.OData/LanguageGenerators/CommonGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
using Microsoft.OData.Edm;
using Microsoft.OData.UriParser;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using Newtonsoft.Json.Linq;
using CodeSnippetsReflection.StringExtensions;
using System.Text.Json;

namespace CodeSnippetsReflection.OData.LanguageGenerators
{
public class CommonGenerator
/// <summary>
/// Common Generator constructor
/// </summary>
/// <param name="model">Edm model of metadata</param>
public class CommonGenerator(IEdmModel model)
{
/// <summary>
/// Edm model of metadata
/// </summary>
private readonly IEdmModel Model;

/// <summary>
/// Common Generator constructor
/// </summary>
/// <param name="model">Edm model of metadata</param>
public CommonGenerator(IEdmModel model)
{
Model = model;
}

/// <summary>
/// Language agnostic function to generate Query section of code snippet
Expand All @@ -48,7 +38,7 @@ public static string GenerateQuerySection(SnippetModel snippetModel, LanguageExp
snippetBuilder.Append(string.Format(languageExpressions.HeaderExpression, key, valueString));
}
//Append any filter queries
if (snippetModel.FilterFieldList.Any())
if (snippetModel.FilterFieldList.Count != 0)
{
var filterResult = string.Join(languageExpressions.FilterExpressionDelimiter, snippetModel.FilterFieldList)
.EscapeQuotesInLiteral(languageExpressions.DoubleQuotesEscapeSequence, languageExpressions.SingleQuotesEscapeSequence);
Expand All @@ -59,7 +49,7 @@ public static string GenerateQuerySection(SnippetModel snippetModel, LanguageExp
//Append any search queries
if (!string.IsNullOrEmpty(snippetModel.SearchExpression))
{
snippetBuilder.Append(string.Format(languageExpressions.SearchExpression,
snippetBuilder.Append(string.Format(languageExpressions.SearchExpression,
snippetModel.SearchExpression
.EscapeQuotesInLiteral(languageExpressions.DoubleQuotesEscapeSequence, languageExpressions.SingleQuotesEscapeSequence)));
}
Expand All @@ -68,13 +58,13 @@ public static string GenerateQuerySection(SnippetModel snippetModel, LanguageExp
if (!string.IsNullOrEmpty(snippetModel.ExpandFieldExpression))
{
//append the expand result to the snippet
snippetBuilder.Append(string.Format(languageExpressions.ExpandExpression,
snippetBuilder.Append(string.Format(languageExpressions.ExpandExpression,
snippetModel.ExpandFieldExpression
.EscapeQuotesInLiteral(languageExpressions.DoubleQuotesEscapeSequence, languageExpressions.SingleQuotesEscapeSequence)));
}

//Append any select queries
if (snippetModel.SelectFieldList.Any())
if (snippetModel.SelectFieldList.Count != 0)
{
var selectResult = string.Join(languageExpressions.SelectExpressionDelimiter, snippetModel.SelectFieldList)
.EscapeQuotesInLiteral(languageExpressions.DoubleQuotesEscapeSequence, languageExpressions.SingleQuotesEscapeSequence);
Expand All @@ -83,7 +73,7 @@ public static string GenerateQuerySection(SnippetModel snippetModel, LanguageExp
}

//Append any orderby queries
if (snippetModel.OrderByFieldList.Any())
if (snippetModel.OrderByFieldList.Count != 0)
{
var orderByResult = string.Join(languageExpressions.OrderByExpressionDelimiter, snippetModel.OrderByFieldList)
.EscapeQuotesInLiteral(languageExpressions.DoubleQuotesEscapeSequence, languageExpressions.SingleQuotesEscapeSequence);
Expand All @@ -100,7 +90,7 @@ public static string GenerateQuerySection(SnippetModel snippetModel, LanguageExp
//Append any skip token queries
if (!string.IsNullOrEmpty(snippetModel.ODataUri.SkipToken))
{
snippetBuilder.Append(string.Format(languageExpressions.SkipTokenExpression,
snippetBuilder.Append(string.Format(languageExpressions.SkipTokenExpression,
snippetModel.ODataUri.SkipToken
.EscapeQuotesInLiteral(languageExpressions.DoubleQuotesEscapeSequence, languageExpressions.SingleQuotesEscapeSequence)));
}
Expand Down Expand Up @@ -143,26 +133,25 @@ public IEdmType GetEdmTypeFromIdentifier(ODataPathSegment oDataPathSegment, ICol
/// <returns></returns>
public bool CanGetServiceCollectionNavigationPropertyForProperty(NavigationPropertyLinkSegment navigationPropertyLinkSegment)
{
if (navigationPropertyLinkSegment == null)
throw new ArgumentNullException(nameof(navigationPropertyLinkSegment));
ArgumentNullException.ThrowIfNull(navigationPropertyLinkSegment);

if (navigationPropertyLinkSegment.NavigationProperty.ContainsTarget)
return true;

// Check if its defined directly in an the entitySet
var isDirectlyInEntitySet = Model.EntityContainer.EntitySets()
var isDirectlyInEntitySet = model.EntityContainer.EntitySets()
.Any(entitySet => entitySet.EntityType().FullName().Equals(navigationPropertyLinkSegment.NavigationProperty.ToEntityType().FullName(), StringComparison.OrdinalIgnoreCase));

if (isDirectlyInEntitySet)
return true;

// check the navBindings/nav Properties on singletons
var isImplicitFromSingleton = Model.EntityContainer.Singletons()
var isImplicitFromSingleton = model.EntityContainer.Singletons()
.SelectMany(singleton => singleton.NavigationPropertyBindings.Select(navPropertyBindings => navPropertyBindings.NavigationProperty)// get the nav propertyBinding from the singleton
.Concat( singleton.EntityType().NavigationProperties())) // Append the nav properties from the singleton type
.Concat(singleton.EntityType().NavigationProperties())) // Append the nav properties from the singleton type
.Any(property => property.ContainsTarget && property.ToEntityType().FullName().Equals(navigationPropertyLinkSegment.NavigationProperty.ToEntityType().FullName(), StringComparison.OrdinalIgnoreCase));
return isImplicitFromSingleton;

return isImplicitFromSingleton;
}

/// <summary>
Expand Down Expand Up @@ -242,7 +231,7 @@ public bool CanGetServiceCollectionNavigationPropertyForProperty(NavigationPrope
IEnumerable<IEdmStructuredType> derivedTypes = null;
if (!searchingParent && elementDefinition is IEdmStructuredType structuredTypeDefinition)
{
derivedTypes = Model?.FindAllDerivedTypes(structuredTypeDefinition);
derivedTypes = model?.FindAllDerivedTypes(structuredTypeDefinition);
}

// the second element in the path is the property we are searching for
Expand Down Expand Up @@ -360,16 +349,18 @@ public static string LowerCaseFirstLetter(string s)
public static IEnumerable<KeyValuePair<string, string>> GetParameterListFromOperationSegmentWithNames(OperationSegment operationSegment, SnippetModel snippetModel, bool returnEnumTypeIfEnum, string collectionSuffix = "")
{
var parametersProvided = new List<string>();
if (!string.IsNullOrEmpty(snippetModel.RequestBody)
&& JsonConvert.DeserializeObject(snippetModel.RequestBody) is JObject testObj)
if (!string.IsNullOrEmpty(snippetModel.RequestBody))
{
foreach (var (key, _) in testObj)
var jsonObject = JsonSerializer.Deserialize<JsonElement>(snippetModel.RequestBody, JsonHelper.JsonSerializerOptions);
if (jsonObject.ValueKind == JsonValueKind.Object)
{
parametersProvided.Add(key);
foreach (var property in jsonObject.EnumerateObject())
{
parametersProvided.Add(property.Name);
}
}
}


if (snippetModel.Method == HttpMethod.Post)
{
//use the order from the metadata
Expand Down Expand Up @@ -405,12 +396,15 @@ public static string LowerCaseFirstLetter(string s)
{
//read parameters from request body since this is an odata action
var parametersProvided = new List<string>();
if (!string.IsNullOrEmpty(snippetModel.RequestBody)
&& JsonConvert.DeserializeObject(snippetModel.RequestBody) is JObject testObj)
if (!string.IsNullOrEmpty(snippetModel.RequestBody))
{
foreach (var (key, _) in testObj)
var jsonObject = JsonSerializer.Deserialize<JsonElement>(snippetModel.RequestBody, JsonHelper.JsonSerializerOptions);
if (jsonObject.ValueKind == JsonValueKind.Object)
{
parametersProvided.Add(key);
foreach (var property in jsonObject.EnumerateObject())
{
parametersProvided.Add(property.Name);
}
}
}

Expand Down

0 comments on commit 8f49bed

Please sign in to comment.