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 go snippet execution path #1502

Merged
merged 2 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
20 changes: 10 additions & 10 deletions CodeSnippetsReflection.OpenAPI.Test/GoGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public async Task GeneratesTheCorrectFluentAPIPathForIndexedCollections() {
using var requestPayload = new HttpRequestMessage(HttpMethod.Get, $"{ServiceRootUrl}/me/messages/{{message-id}}");
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1TreeNode());
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains(".Me().MessagesById(\"message-id\")", result);
Assert.Contains(".Me().Messages().WithMessageId(\"message-id\")", result);
}
[Fact]
public async Task GeneratesTheCorrectFluentAPIPathForIndexedCollectionsWithMultipleParams() {
Expand All @@ -64,7 +64,7 @@ public async Task GeneratesTheCorrectFluentAPIPathForIndexedCollectionsWithMulti
};
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1TreeNode());
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains(".DrivesById(\"drive-id\").ItemsById(\"driveItem-id\").Checkin().", result);
Assert.Contains("Drives().WithDriveId(\"drive-id\").Items().WithItemId(\"driveItem-id\").Checkin().", result);
}
[Fact]
public async Task IgnoreOdataTypeWhenGenerating() {
Expand Down Expand Up @@ -122,7 +122,7 @@ public async Task GeneratesThePatchMethodCall() {
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains("Patch", result);
Assert.DoesNotContain("WithRequestConfigurationAndResponseHandler", result);
Assert.Contains("graphClient.Me().MessagesById(\"message-id\").Patch(context.Background(), requestBody, nil)", result);
Assert.Contains("graphClient.Me().Messages().WithMessageId(\"message-id\").Patch(context.Background(), requestBody, nil)", result);
}
[Fact]
public async Task GeneratesThePutMethodCall() {
Expand All @@ -137,7 +137,7 @@ public async Task GeneratesThePutMethodCall() {
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains("Put", result);
Assert.DoesNotContain("WithRequestConfigurationAndResponseHandler", result);
Assert.Contains("graphClient.ApplicationsById(\"application-id\").Logo().Put(context.Background(), requestBody, nil)", result);
Assert.Contains("graphClient.Applications().WithApplicationId(\"application-id\").Logo().Put(context.Background(), requestBody, nil)", result);
}
[Fact]
public async Task GeneratesTheDeleteMethodCall() {
Expand All @@ -147,7 +147,7 @@ public async Task GeneratesTheDeleteMethodCall() {
Assert.Contains("Delete", result);
Assert.DoesNotContain("result, err :=", result);
Assert.DoesNotContain("WithRequestConfigurationAndResponseHandler", result);
Assert.Contains("graphClient.Me().MessagesById(\"message-id\").Delete(context.Background(), nil)", result);
Assert.Contains("graphClient.Me().Messages().WithMessageId(\"message-id\").Delete(context.Background(), nil)", result);
}
[Fact]
public async Task WritesTheRequestPayload() {
Expand Down Expand Up @@ -263,7 +263,7 @@ public async Task GeneratesNestedParameterNames()
Assert.Contains("&graphconfig.UserItemRequestBuilderGetRequestConfiguration", result);
Assert.Contains("configuration :=", result);
Assert.Contains("requestParameters :=", result);
Assert.Contains("result, err := graphClient.UsersById(\"user-id\").Get(context.Background(), configuration)", result);
Assert.Contains("result, err := graphClient.Users().WithUserId(\"user-id\").Get(context.Background(), configuration)", result);
}
[Fact]
public async Task GeneratesODataTypesAreEscaped()
Expand All @@ -282,7 +282,7 @@ public async Task GeneratesODataTypesAreEscaped()
Assert.Contains("requestBody := graphmodels.NewReferenceCreate()", result);
Assert.Contains("requestBody.SetOdataId(&odataId)", result);
Assert.Contains("odataId := \"https://graph.microsoft.com/v1.0/directoryObjects/{id}\"", result);
Assert.Contains("graphClient.GroupsById(\"group-id\").Members().Ref().Post(context.Background(), requestBody, nil)", result);
Assert.Contains("graphClient.Groups().WithGroupId(\"group-id\").Members().Ref().Post(context.Background(), requestBody, nil)", result);
}
[Fact]
public async Task GeneratesCountBooleanQueryParameters() {
Expand Down Expand Up @@ -339,7 +339,7 @@ public async Task SupportsODataOldIndexFormat() {
};
var snippetModel = new SnippetModel(requestPayload, ServiceRootBetaUrl, await GetBetaTreeNode());
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains("graphClient.Me().EventsById(\"event-id\").Attachments().Post(context.Background(), requestBody, nil)", result);
Assert.Contains("graphClient.Me().Events().WithEventId(\"event-id\").Attachments().Post(context.Background(), requestBody, nil)", result);
}
[Fact]
public async Task ParsesThePayloadEvenIfContentTypeIsMissing() {
Expand All @@ -349,7 +349,7 @@ public async Task ParsesThePayloadEvenIfContentTypeIsMissing() {
};
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetBetaTreeNode());
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains("graphClient.TeamsById(\"team-id\").ChannelsById(\"channel-id\").Messages().Post(context.Background(), requestBody, nil)", result);
Assert.Contains("graphClient.Teams().WithTeamId(\"team-id\").Channels().WithChannelId(\"channel-id\").Messages().Post(context.Background(), requestBody, nil)", result);
}
[Fact]
public async Task WritesEmptyPrimitiveArrays() {
Expand Down Expand Up @@ -397,7 +397,7 @@ public async Task DoesntFailOnTerminalSlash() {
using var requestPayload = new HttpRequestMessage(HttpMethod.Get, $"{ServiceRootBetaUrl}/me/messages/AAMkADYAAAImV_jAAA=/?$expand=microsoft.graph.eventMessage/event");
var snippetModel = new SnippetModel(requestPayload, ServiceRootBetaUrl, await GetBetaTreeNode());
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains("graphClient.Me().MessagesById(\"message-id\").Get(context.Background(), configuration)", result);
Assert.Contains("graphClient.Me().Messages().WithMessageId(\"message-id\").Get(context.Background(), configuration)", result);
}
[Fact]
public async Task IncludesRequestBodyClassName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,22 +482,27 @@ private static void WriteCodePropertyObject(string propertyAssignment, StringBui
private static string GetFluentApiPath(IEnumerable<OpenApiUrlTreeNode> nodes)
{
if (!(nodes?.Any() ?? false)) return string.Empty;
return nodes.Select(static x =>
var elements = nodes.Select(static (x, i) =>
{
if (x.Segment.IsCollectionIndex())
return $"ById{x.Segment.Replace("{", "(\"").Replace("}", "\")")}.";
return $"WithTypeId{x.Segment.Replace("{", "(\"").Replace("}", "\")")}.";
else if (x.Segment.IsFunction())
return x.Segment.Split('.')
.Select(static s => s.ToFirstCharacterUpperCase())
.Aggregate(static (a, b) => $"{a}{b}") + "().";
return x.Segment.ToFirstCharacterUpperCase() + "().";
})
.Aggregate(static (x, y) =>
.Aggregate(new List<String>(), (current, next) =>
{
return $"{x}{y.Replace("$", string.Empty, StringComparison.OrdinalIgnoreCase).ToFirstCharacterUpperCase()}";
})
.Replace("().ById(", "ById(")
.Replace("()()", "()");
var element = next.Contains("WithTypeId",StringComparison.OrdinalIgnoreCase) ?
next.Replace("WithTypeId", $"With{current.Last().Replace("s().", string.Empty, StringComparison.OrdinalIgnoreCase)}Id") :
$"{next.Replace("$", string.Empty, StringComparison.OrdinalIgnoreCase).ToFirstCharacterUpperCase()}";

current.Add(element);
return current;
});

return string.Join("", elements).Replace("()()", "()");
}
}
}