From 73429c1b2450ac2f414dad94f424910a2fa8a3a0 Mon Sep 17 00:00:00 2001 From: Shem Ogumbe Date: Thu, 9 May 2024 11:57:04 +0300 Subject: [PATCH] update request configuration class (#2034) * Merge dev to master (#2013) 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) * Remove built request configiguation name * Update unit tests for updated request configuration name * Update imports to inlcude base request configuration * update tests --------- Co-authored-by: Millicent Achieng Co-authored-by: Charles Wahome --- .../PythonGeneratorTests.cs | 16 +++++++++------- .../PythonImportGeneratorTests.cs | 1 + .../LanguageGenerators/PythonGenerator.cs | 7 ++++++- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/CodeSnippetsReflection.OpenAPI.Test/PythonGeneratorTests.cs b/CodeSnippetsReflection.OpenAPI.Test/PythonGeneratorTests.cs index a56a4e569..6a9d86942 100644 --- a/CodeSnippetsReflection.OpenAPI.Test/PythonGeneratorTests.cs +++ b/CodeSnippetsReflection.OpenAPI.Test/PythonGeneratorTests.cs @@ -169,7 +169,7 @@ public async Task GeneratesSelectQueryParameters() var result = _generator.GenerateCodeSnippet(snippetModel); Assert.Contains("RequestBuilderGetQueryParameters(", result); Assert.Contains("select = [\"displayName\",\"id\"]", result); - Assert.Contains("RequestBuilderGetRequestConfiguration(", result); + Assert.Contains("RequestConfiguration(", result); Assert.Contains("query_parameters = query_params,", result); } [Fact] @@ -182,7 +182,7 @@ public async Task GeneratesCountBooleanQueryParameters() Assert.Contains("count = True", result); Assert.Contains("select = [\"displayName\",\"id\"]", result); Assert.DoesNotContain("\"true\"", result); - Assert.Contains("RequestBuilderGetRequestConfiguration(", result); + Assert.Contains("RequestConfiguration(", result); Assert.Contains("query_parameters = query_params,", result); } [Fact] @@ -211,7 +211,8 @@ public async Task GeneratesRequestHeaders() requestPayload.Headers.Add("ConsistencyLevel", "eventual"); var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1SnippetMetadata()); var result = _generator.GenerateCodeSnippet(snippetModel); - Assert.Contains("request_configuration = GroupsRequestBuilder.GroupsRequestBuilderGetRequestConfiguration()", result); + Assert.Contains("request_configuration = RequestConfiguration()", result); + Assert.Contains("from kiota_abstractions.base_request_configuration import RequestConfiguration", result); Assert.Contains("request_configuration.headers.add(\"ConsistencyLevel\", \"eventual\")", result); } [Fact] @@ -244,7 +245,8 @@ public async Task GeneratesSnippetForRequestWithDeltaAndSkipToken() var result = _generator.GenerateCodeSnippet(snippetModel); Assert.Contains("DeltaRequestBuilderGetQueryParameters(", result); Assert.Contains("skiptoken = \"R0usmcCM996atia_s\",", result); - Assert.Contains("request_configuration = DeltaRequestBuilder.DeltaRequestBuilderGetRequestConfiguration(", result); + Assert.Contains("request_configuration = RequestConfiguration(", result); + Assert.Contains("from kiota_abstractions.base_request_configuration import RequestConfiguration", result); Assert.Contains("query_parameters = query_params,", result); Assert.Contains("request_configuration.headers.add(\"Prefer\", \"odata.maxpagesize=2\")", result); Assert.Contains("result = await graph_client.me.calendar_view.delta.get", result); @@ -651,7 +653,7 @@ public async Task CorrectlyHandlesOdataFunction() Assert.Contains("await graph_client.users.delta.get(request_configuration = request_configuration)", result); Assert.Contains("query_params = DeltaRequestBuilder.DeltaRequestBuilderGetQueryParameters(", result); Assert.Contains("select = [\"displayName\",\"jobTitle\",\"mobilePhone\"]", result); - Assert.Contains("request_configuration = DeltaRequestBuilder.DeltaRequestBuilderGetRequestConfiguration(", result); + Assert.Contains("request_configuration = RequestConfiguration(", result); } [Fact] public async Task CorrectlyHandlesDateTimeOffsetInUrl() @@ -886,7 +888,7 @@ public async Task CorrectlyHandlesTypeFromInUrl() Assert.Contains("query_params = MailFoldersRequestBuilder.MailFoldersRequestBuilderGetQueryParameters(", result); Assert.Contains("include_hidden_folders = \"true\"", result); - Assert.Contains("request_configuration = MailFoldersRequestBuilder.MailFoldersRequestBuilderGetRequestConfiguration(", result); + Assert.Contains("request_configuration = RequestConfiguration(", result); } [Fact] public async Task MatchesPathWithPathParameter() @@ -1197,6 +1199,6 @@ public async Task GeneratesCorrectRequestBuilderNameForIndexedCollections() var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1SnippetMetadata()); var result = _generator.GenerateCodeSnippet(snippetModel); Assert.Contains("query_params = UserItemRequestBuilder.UserItemRequestBuilderGetQueryParameters(", result); - Assert.Contains("request_configuration = UserItemRequestBuilder.UserItemRequestBuilderGetRequestConfiguration(", result); + Assert.Contains("request_configuration = RequestConfiguration(", result); } } diff --git a/CodeSnippetsReflection.OpenAPI.Test/PythonImportGeneratorTests.cs b/CodeSnippetsReflection.OpenAPI.Test/PythonImportGeneratorTests.cs index 832c02877..fa049c6b1 100644 --- a/CodeSnippetsReflection.OpenAPI.Test/PythonImportGeneratorTests.cs +++ b/CodeSnippetsReflection.OpenAPI.Test/PythonImportGeneratorTests.cs @@ -18,6 +18,7 @@ public async Task GeneratesRequestBuilderImports() var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1SnippetMetadata()); var result = _generator.GenerateCodeSnippet(snippetModel); Assert.Contains("from msgraph import GraphServiceClient", result); + Assert.Contains("from kiota_abstractions.base_request_configuration import RequestConfiguration", result); Assert.Contains("from msgraph.generated.users.item.calendar.events.events_request_builder import EventsRequestBuilder", result); } diff --git a/CodeSnippetsReflection.OpenAPI/LanguageGenerators/PythonGenerator.cs b/CodeSnippetsReflection.OpenAPI/LanguageGenerators/PythonGenerator.cs index 4ca42749a..f7857c177 100644 --- a/CodeSnippetsReflection.OpenAPI/LanguageGenerators/PythonGenerator.cs +++ b/CodeSnippetsReflection.OpenAPI/LanguageGenerators/PythonGenerator.cs @@ -18,6 +18,7 @@ public class PythonGenerator : ILanguageGenerator ReservedTypeNames = new(StringComparer.OrdinalIgnoreCase) @@ -79,6 +80,7 @@ private static HashSet GetImportStatements(SnippetModel snippetModel) { const string modelImportPrefix = "from msgraph.generated.models"; const string requestBuilderImportPrefix = "from msgraph.generated"; + const string BaseRequestConfigImport = "from kiota_abstractions.base_request_configuration import RequestConfiguration"; var snippetImports = new HashSet(); @@ -96,6 +98,7 @@ private static HashSet GetImportStatements(SnippetModel snippetModel) 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}"); @@ -119,6 +122,8 @@ private static HashSet GetImportStatements(SnippetModel snippetModel) { //construct path to request builder 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}"); + snippetImports.Add($"{BaseRequestConfigImport}"); + } break; } @@ -188,7 +193,7 @@ private static string GetRequestConfiguration(SnippetCodeGraph codeGraph, Indent if (codeGraph.HasParameters() || codeGraph.HasHeaders()) { snippetBuilder.AppendLine(queryParamsPayload); - snippetBuilder.Append($"{RequestConfigurationVarName} = {requestBuilderName}.{requestConfigurationName}("); + snippetBuilder.Append($"{RequestConfigurationVarName} = {RequestConfigurationType}("); if (codeGraph.HasParameters()) {