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

Update python snippet generation logic #1450

Merged
merged 86 commits into from
May 22, 2023
Merged
Changes from 1 commit
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
52a4d3c
added python generator
shemogumbe Mar 23, 2023
6c15fc3
Include python snippet generation to pipeline
shemogumbe Mar 23, 2023
b21a236
Add python dot notation to generated snippets
shemogumbe Mar 28, 2023
ddd5cc6
Python class instantiation
shemogumbe Apr 3, 2023
c7e4872
Convert to snake case and use await
shemogumbe Apr 3, 2023
462e589
convert request adapter to snake case
shemogumbe Apr 3, 2023
409c8d2
remove brackets for classnames with no parameters
shemogumbe Apr 4, 2023
429d398
Remove brackets for paths with no parameters
shemogumbe Apr 4, 2023
d2652c5
Remove trailing semi colons
shemogumbe Apr 4, 2023
af35083
Change ById to _by_id
shemogumbe Apr 5, 2023
584ca62
Change ById to _by_id
shemogumbe Apr 5, 2023
fa984b8
fix snake case for by_id
shemogumbe Apr 5, 2023
af0d4a9
fix by id
shemogumbe Apr 5, 2023
03cf7b0
fix by id dot notation
shemogumbe Apr 5, 2023
8e29a86
fix by id dot notation
shemogumbe Apr 5, 2023
db20fee
fix by_id _ after entity
shemogumbe Apr 5, 2023
8c23abf
Revert "fix by id dot notation"
shemogumbe Apr 5, 2023
1721f96
fix snake case for by_id
shemogumbe Apr 5, 2023
d1299f2
replace empty string with underscore for by_id prefix
shemogumbe Apr 5, 2023
76031cd
Update query parameters and request headers
shemogumbe Apr 5, 2023
4b02e96
Change request configurations - headers and query params
shemogumbe Apr 6, 2023
b9c5285
Update query parameters to pythonc array format
shemogumbe Apr 6, 2023
4ed0219
Revert "Update query parameters to pythonc array format"
shemogumbe Apr 6, 2023
8484ce7
Fix headers and parameters
shemogumbe Apr 6, 2023
2c841be
Fix request headers and parameters
shemogumbe Apr 11, 2023
0270093
Fixed tests on python generator
shemogumbe Apr 11, 2023
7e868e8
Remove request builder if there are no set parameters or headers
shemogumbe Apr 11, 2023
3e86925
Add request builder to request builder method as prefix
shemogumbe Apr 12, 2023
d8f3ecc
remove headers close tags if request configuration does not exist
shemogumbe Apr 12, 2023
fae5580
remove headers close tags if request configuration is not set
shemogumbe Apr 12, 2023
ac72da6
fix spacing in headers
shemogumbe Apr 12, 2023
18294a9
Added snake case string extension
shemogumbe Apr 12, 2023
976a2f8
Convert properties to Snake Case
shemogumbe Apr 12, 2023
e96c5ff
fix tests following lowercase properties
shemogumbe Apr 13, 2023
c5f38f7
fixed merge conflicts
shemogumbe Apr 13, 2023
d97ddfe
added python generator
shemogumbe Mar 23, 2023
cd86441
Include python snippet generation to pipeline
shemogumbe Mar 23, 2023
5667c8e
Add python dot notation to generated snippets
shemogumbe Mar 28, 2023
8597d15
Python class instantiation
shemogumbe Apr 3, 2023
0cbd763
Convert to snake case and use await
shemogumbe Apr 3, 2023
520d357
convert request adapter to snake case
shemogumbe Apr 3, 2023
4b367cb
remove brackets for classnames with no parameters
shemogumbe Apr 4, 2023
deefcc9
Remove brackets for paths with no parameters
shemogumbe Apr 4, 2023
83c86bf
Remove trailing semi colons
shemogumbe Apr 4, 2023
bba09e3
Change ById to _by_id
shemogumbe Apr 5, 2023
49d4825
Change ById to _by_id
shemogumbe Apr 5, 2023
47926cd
fix snake case for by_id
shemogumbe Apr 5, 2023
1b3ed2e
fix by id
shemogumbe Apr 5, 2023
8653450
fix by id dot notation
shemogumbe Apr 5, 2023
07fcc51
fix by id dot notation
shemogumbe Apr 5, 2023
7059d1e
fix by_id _ after entity
shemogumbe Apr 5, 2023
02f4172
Revert "fix by id dot notation"
shemogumbe Apr 5, 2023
722e579
fix snake case for by_id
shemogumbe Apr 5, 2023
bc8276a
replace empty string with underscore for by_id prefix
shemogumbe Apr 5, 2023
749f6a8
Update query parameters and request headers
shemogumbe Apr 5, 2023
ef07e1b
Change request configurations - headers and query params
shemogumbe Apr 6, 2023
bf20ca1
Update query parameters to pythonc array format
shemogumbe Apr 6, 2023
0ef9599
Revert "Update query parameters to pythonc array format"
shemogumbe Apr 6, 2023
d6a1062
Fix headers and parameters
shemogumbe Apr 6, 2023
6fc7a8f
Fix request headers and parameters
shemogumbe Apr 11, 2023
3f6cb25
Fixed tests on python generator
shemogumbe Apr 11, 2023
2507cdf
Remove request builder if there are no set parameters or headers
shemogumbe Apr 11, 2023
8300cce
Add request builder to request builder method as prefix
shemogumbe Apr 12, 2023
8699566
remove headers close tags if request configuration does not exist
shemogumbe Apr 12, 2023
4f342eb
remove headers close tags if request configuration is not set
shemogumbe Apr 12, 2023
f8fd895
fix spacing in headers
shemogumbe Apr 12, 2023
7bbd249
Added snake case string extension
shemogumbe Apr 12, 2023
19d3c65
Convert properties to Snake Case
shemogumbe Apr 12, 2023
9fc5e55
fix tests following lowercase properties
shemogumbe Apr 13, 2023
27254e9
Fix tests and headers and parameter flow
shemogumbe Apr 13, 2023
f5d8428
Remove unusued vars
shemogumbe Apr 25, 2023
d41be30
Merge branch 'dev' of github.com:microsoftgraph/microsoft-graph-devx-…
shemogumbe Apr 27, 2023
c9b94bd
Fix tests and headers and parameter flow
shemogumbe Apr 13, 2023
d355692
Remove unusued vars
shemogumbe Apr 25, 2023
aeb82a0
Alter snippets to acoomodate indexers
shemogumbe Apr 27, 2023
8bfb968
update snippets to include indexers
shemogumbe Apr 28, 2023
bba30ba
integrate indexers work
shemogumbe May 2, 2023
062db82
fixed
shemogumbe May 2, 2023
a9eb213
Fix tests
shemogumbe May 2, 2023
e229990
Fix snake case conversaion
shemogumbe May 2, 2023
8dccb63
fix snake case on object attributes
shemogumbe May 2, 2023
c8a6de5
Fix test on paths with different casing and snake cases
shemogumbe May 3, 2023
8eda566
fix tests complex api
shemogumbe May 3, 2023
6bfbc90
Fix tests
shemogumbe May 4, 2023
cd88729
Fix tests
shemogumbe May 4, 2023
4d88346
Fix tests
shemogumbe May 5, 2023
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
Prev Previous commit
Next Next commit
Fix tests and headers and parameter flow
shemogumbe committed Apr 27, 2023
commit c9b94bdec9a6103077975af2e65b1be22e75d1ef
6 changes: 2 additions & 4 deletions CodeSnippetsReflection.OpenAPI.Test/PythonGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -83,11 +83,9 @@ public async Task GeneratesObjectInitializationWithCallToSetters()
};
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1TreeNode());
var result = _generator.GenerateCodeSnippet(snippetModel);
// Assert.Contains(
// @"query_params = UsersRequestBuilder.UsersRequestBuilderPostQueryParameters(
// select = [""displayName"",""mailNickName""],
// Assert.Contains("select = [""displayName"",""mailNickName""],
// )", result);
Assert.Contains("query_parameters = query_params", result);
Assert.Contains("request_body.account_enabled = True", result);
}

[Fact]
Original file line number Diff line number Diff line change
@@ -43,9 +43,6 @@ private static void WriteRequestExecutionPath(SnippetCodeGraph codeGraph, String
var bodyParameter = codeGraph.HasBody()
? $"{RequestBodyVarName} = {RequestBodyVarName}"
: string.Empty;
/*
body = body
*/
var optionsParameter = codeGraph.HasOptions() ? $"options =" : string.Empty;
var returnVar = codeGraph.HasReturnedBody() ? "result = " : string.Empty;
var parameterList = GetActionParametersList(bodyParameter, configParameter, optionsParameter);
@@ -88,28 +85,30 @@ private static string GetRequestConfiguration(SnippetCodeGraph codeGraph, Indent
var classNameQueryParameters = $"{requestBuilderName}.{requestBuilderName}{codeGraph.HttpMethod.Method.ToLowerInvariant().ToFirstCharacterUpperCase()}QueryParameters";

var queryParamsPayload = GetRequestQueryParameters(codeGraph, indentManager, classNameQueryParameters);
if (codeGraph.HasParameters() || codeGraph.HasParameters()){
if (codeGraph.HasParameters() || codeGraph.HasHeaders()){
snippetBuilder.AppendLine(queryParamsPayload);
snippetBuilder.AppendLine($"{RequestConfigurationVarName} = {requestBuilderName}.{className}(");
indentManager.Indent();

var requestHeadersPayload = GetRequestHeaders(codeGraph, indentManager);
if ( codeGraph.HasParameters() && codeGraph.HasHeaders()){
snippetBuilder.AppendLine("query_parameters = query_params,");
if (codeGraph.HasHeaders()){
if(queryParamsPayload != null){
snippetBuilder.AppendLine("query_parameters = query_params,");
}
snippetBuilder.AppendLine(requestHeadersPayload);
indentManager.Unindent();
snippetBuilder.AppendLine(")");
}
if ( codeGraph.HasParameters() && !codeGraph.HasHeaders()){
}
if (!codeGraph.HasHeaders()){
snippetBuilder.AppendLine("query_parameters = query_params,");
indentManager.Unindent();
snippetBuilder.AppendLine(")");
}
if (codeGraph.HasHeaders() && !codeGraph.HasParameters()) {
snippetBuilder.AppendLine(requestHeadersPayload);
indentManager.Unindent();
snippetBuilder.AppendLine(")");
}
// if (codeGraph.HasHeaders() && !codeGraph.HasParameters()) {
// snippetBuilder.AppendLine(requestHeadersPayload);
// indentManager.Unindent();
// snippetBuilder.AppendLine(")");
// }
}
return snippetBuilder.ToString();
}
@@ -126,27 +125,22 @@ private static string GetRequestHeaders(SnippetCodeGraph snippetModel, IndentMan
var snippetBuilder = new StringBuilder();
var filteredHeaders = snippetModel.Headers?.Where(static h => !h.Name.Equals("Host", StringComparison.OrdinalIgnoreCase))
.ToList();
if (!snippetModel.HasHeaders())
return "headers = {}";

var headersvar = new StringBuilder();
headersvar.AppendLine("headers = {");
indentManager.Indent();
foreach (var header in snippetModel.Headers)
{
headersvar.AppendLine($"{indentManager.GetIndent()}'{header.Name}' : {EvaluateParameter(header)},");
}
indentManager.Unindent();
headersvar.AppendLine("}");
/*
headers= {

"Content-Type": "application/json",
"Accept": "Value",
}
*/

if (!snippetModel.HasHeaders())
headersvar.AppendLine("headers = {}");
if (filteredHeaders != null && filteredHeaders.Any()){
headersvar.AppendLine("headers = {");
indentManager.Indent();
foreach (var header in snippetModel.Headers)
{
headersvar.AppendLine($"{indentManager.GetIndent()}'{header.Name}' : {EvaluateParameter(header)},");
}
indentManager.Unindent();
headersvar.AppendLine("}");
}
return headersvar.ToString();

}
private static string NormalizeQueryParameterName(string queryParam) => queryParam?.TrimStart('$').ToFirstCharacterLowerCase();
private static void WriteObjectProperty(string propertyAssignment, StringBuilder snippetBuilder, CodeProperty codeProperty, IndentManager indentManager, string childPropertyName = default, SnippetCodeGraph codeGraph = default)
34 changes: 18 additions & 16 deletions CodeSnippetsReflection/StringExtensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace CodeSnippetsReflection.StringExtensions
using System.Text;

namespace CodeSnippetsReflection.StringExtensions
{
public static class StringExtensions
{
@@ -35,27 +37,27 @@ public static string ToFirstCharacterUpperCaseAfterCharacter(this string stringV
return stringValue[0..charIndex] + char.ToUpper(stringValue[charIndex + 1]) + stringValue[(charIndex + 2)..].ToFirstCharacterUpperCaseAfterCharacter(character);
}

public static string ToSnakeCase(this string str)
public static string ToSnakeCase(this string str)
{
StringBuilder snakeCaseBuilder = new StringBuilder();
for (int i = 0; i < str.Length; i++)
{
string snakeCaseString = "";
for (int i = 0; i < str.Length; i++)
char c = str[i];
if (char.IsUpper(c))
{
char c = str[i];
if (char.IsUpper(c))
{
if (i > 0)
{
snakeCaseString += "_";
}
snakeCaseString += char.ToLower(c);
}
else
if (i > 0)
{
snakeCaseString += c;
snakeCaseBuilder.Append('_');
}
snakeCaseBuilder.Append(char.ToLower(c));
}
else
{
snakeCaseBuilder.Append(c);
}
return snakeCaseString;
}
return snakeCaseBuilder.ToString();
}
public static string EscapeQuotes(this string stringValue)
{
return stringValue.Replace("\"", "\\\"");