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

Fix typescript snippets #2278

Merged
merged 2 commits into from
Jan 9, 2025
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
124 changes: 86 additions & 38 deletions CodeSnippetsReflection.OpenAPI.Test/TypeScriptGeneratorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public async Task GeneratesTheCorrectFluentAPIPathAsync()
var result = _generator.GenerateCodeSnippet(snippetModel);

var expected = @"
const graphServiceClient = GraphServiceClient.init({authProvider});

// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=typescript

const result = async () => {
await graphServiceClient.me.messages.get();
Expand All @@ -44,20 +43,24 @@ public async Task GeneratesClassWithDefaultBodyWhenSchemaNotPresentAsync()
var result = _generator.GenerateCodeSnippet(snippetModel);

var expected = @"
const graphServiceClient = GraphServiceClient.init({authProvider});
// Dependencies
import ""@microsoft/msgraph-sdk-identitygovernance"";
import { BatchRecordDecisionsPostRequestBody } from ""@microsoft/msgraph-sdk-identitygovernance/identityGovernance/accessReviews/definitions/item/instances/item/batchRecordDecisions"";
//other-imports

const requestBody : BatchRecordDecisionsPostRequestBody = {
decision : ""Approve"",
justification : ""All principals with access need continued access to the resource (Marketing Group) as all the principals are on the marketing team"",
resourceId : ""a5c51e59-3fcd-4a37-87a1-835c0c21488a"",
};

// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=typescript

const result = async () => {
await graphServiceClient.identityGovernance.accessReviews.definitionsById(""accessReviewScheduleDefinition-id"").instancesById(""accessReviewInstance-id"").batchRecordDecisions.post(requestBody);
await graphServiceClient.identityGovernance.accessReviews.definitions.byAccessReviewScheduleDefinitionId(""accessReviewScheduleDefinition-id"").instances.byAccessReviewInstanceId(""accessReviewInstance-id"").batchRecordDecisions.post(requestBody);
}
";


AssertExtensions.ContainsIgnoreWhiteSpace(expected, result);
}

Expand All @@ -70,26 +73,28 @@ public async Task GeneratesTheCorrectFluentAPIPathForIndexedCollectionsAsync()
var result = _generator.GenerateCodeSnippet(snippetModel);

var expected = @"
const graphServiceClient = GraphServiceClient.init({authProvider});
// Dependencies
import ""@microsoft/msgraph-sdk-users"";
//other-imports

// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=typescript

const result = async () => {
await graphServiceClient.me.messagesById(""message-id"").get();
await graphServiceClient.me.messages.byMessageId(""message-id"").get();
}
";


AssertExtensions.ContainsIgnoreWhiteSpace(expected, result);
}

[Fact]
public async Task GeneratesTheSnippetInitializationStatementAsync()
public async Task GeneratesTheSnippetInitializationDeclarationAsync()
{
using var requestPayload = new HttpRequestMessage(HttpMethod.Get, $"{ServiceRootUrl}/me/messages");
var snippetModel = new SnippetModel(requestPayload, ServiceRootUrl, await GetV1SnippetMetadataAsync());
await snippetModel.InitializeModelAsync(requestPayload);
var result = _generator.GenerateCodeSnippet(snippetModel);
Assert.Contains("const graphServiceClient = GraphServiceClient.init({authProvider});", result);
Assert.Contains("// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=typescript", result);
}

[Fact]
Expand All @@ -100,15 +105,17 @@ public async Task GeneratesTheGetMethodCallAsync()
await snippetModel.InitializeModelAsync(requestPayload);
var result = _generator.GenerateCodeSnippet(snippetModel);
var expected = @"
const graphServiceClient = GraphServiceClient.init({authProvider});
// Dependencies
import ""@microsoft/msgraph-sdk-users"";
//other-imports

// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=typescript

const result = async () => {
await graphServiceClient.me.messages.get();
}
";


AssertExtensions.ContainsIgnoreWhiteSpace(expected, result);
}

Expand Down Expand Up @@ -137,8 +144,14 @@ public async Task GeneratesThePatchMethodCallAsync()
await snippetModel.InitializeModelAsync(requestPayload);
var result = _generator.GenerateCodeSnippet(snippetModel);
var expected = @"
const result = async () => {
await graphServiceClient.me.messagesById(""message-id"").patch();
// Dependencies
import ""@microsoft/msgraph-sdk-users"";
//other-imports

// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=typescript

const result = async () => {
await graphServiceClient.me.messages.byMessageId(""message-id"").patch();
}
";

Expand All @@ -164,15 +177,17 @@ public async Task GeneratesTheDeleteMethodCallAsync()
await snippetModel.InitializeModelAsync(requestPayload);
var result = _generator.GenerateCodeSnippet(snippetModel);
var expected = @"
const graphServiceClient = GraphServiceClient.init({authProvider});
// Dependencies
import ""@microsoft/msgraph-sdk-users"";
//other-imports

// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=typescript

const result = async () => {
await graphServiceClient.me.messagesById(""message-id"").delete();
await graphServiceClient.me.messages.byMessageId(""message-id"").delete();
}
";


AssertExtensions.ContainsIgnoreWhiteSpace(expected, result);
}

Expand Down Expand Up @@ -201,7 +216,11 @@ public async Task WritesTheRequestPayloadAsync()
var result = _generator.GenerateCodeSnippet(snippetModel);

var expected = @"
const graphServiceClient = GraphServiceClient.init({authProvider});
//THIS SNIPPET IS A PREVIEW FOR THE KIOTA BASED SDK. NON-PRODUCTION USE ONLY
// Dependencies
import ""@microsoft/msgraph-sdk-users"";
import { User } from ""@microsoft/msgraph-sdk/models"";
//other-imports

const requestBody : User = {
accountEnabled : true,
Expand All @@ -216,6 +235,8 @@ public async Task WritesTheRequestPayloadAsync()
},
};

// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=typescript

const result = async () => {
await graphServiceClient.users.post(requestBody);
}
Expand Down Expand Up @@ -259,12 +280,18 @@ public async Task WritesADoubleAsync()
var result = _generator.GenerateCodeSnippet(snippetModel);

var expected = @"
const graphServiceClient = GraphServiceClient.init({authProvider});
//THIS SNIPPET IS A PREVIEW FOR THE KIOTA BASED SDK. NON-PRODUCTION USE ONLY
// Dependencies
import ""@microsoft/msgraph-sdk-users"";
import { FindMeetingTimesPostRequestBody } from ""@microsoft/msgraph-sdk-users/users/item/findMeetingTimes"";
//other-imports

const requestBody : FindMeetingTimesPostRequestBody = {
minimumAttendeePercentage : 10,
};

// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=typescript

const result = async () => {
await graphServiceClient.me.findMeetingTimes.post(requestBody);
}
Expand All @@ -286,12 +313,13 @@ public async Task GeneratesABinaryPayloadAsync()
var result = _generator.GenerateCodeSnippet(snippetModel);

var expected = @"
const graphServiceClient = GraphServiceClient.init({authProvider});

const requestBody = new ArrayBuffer(16);

// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=typescript

async () => {
await graphServiceClient.applicationsById(""application-id"").logo.put(requestBody);
await graphServiceClient.applications.byApplicationId(""application-id"").logo.put(requestBody);
}
";

Expand All @@ -311,14 +339,19 @@ public async Task GeneratesABase64UrlPayloadAsync()
var result = _generator.GenerateCodeSnippet(snippetModel);

var expected = @"
const graphServiceClient = GraphServiceClient.init({authProvider});
// Dependencies
import ""@microsoft/msgraph-sdk-chats"";
import { ChatMessageHostedContent} from ""@microsoft/msgraph-sdk/models"";
//other-imports

const requestBody : ChatMessageHostedContent = {
contentBytes : ""wiubviuwbegviwubiu"",
};

// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=typescript

const result = async () => {
await graphServiceClient.chatsById(""chat-id"").messagesById(""chatMessage-id"").hostedContents.post(requestBody);
await graphServiceClient.chats.byChatId(""chat-id"").messages.byChatMessageId(""chatMessage-id"").hostedContents.post(requestBody);
}
";

Expand All @@ -339,12 +372,13 @@ public async Task GeneratesADatePayloadAsync()
var result = _generator.GenerateCodeSnippet(snippetModel);

var expected = @"
const graphServiceClient = GraphServiceClient.init({authProvider});

const requestBody : Message = {
receivedDateTime : new Date(""2021-08-30T20:00:00:00Z""),
};

// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=typescript

const result = async () => {
await graphServiceClient.me.messages.post(requestBody);
}
Expand Down Expand Up @@ -376,7 +410,6 @@ public async Task GeneratesAnArrayPayloadInAdditionalDataAsync()
var result = _generator.GenerateCodeSnippet(snippetModel);

var expected = @"
const graphServiceClient = GraphServiceClient.init({authProvider});

const requestBody : Group = {
additionalData : {
Expand Down Expand Up @@ -405,7 +438,6 @@ public async Task GeneratesAnArrayOfObjectsPayloadDataAsync()
var result = _generator.GenerateCodeSnippet(snippetModel);

var expected = @"
const graphServiceClient = GraphServiceClient.init({authProvider});

const requestBody : Group = {
extensions : [
Expand All @@ -422,8 +454,10 @@ public async Task GeneratesAnArrayOfObjectsPayloadDataAsync()
},
};

// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=typescript

const result = async () => {
await graphServiceClient.groupsById(""group-id"").patch(requestBody);
await graphServiceClient.groups.byGroupId(""group-id"").patch(requestBody);
}
";

Expand All @@ -439,14 +473,15 @@ public async Task GeneratesSelectQueryParametersAsync()
var result = _generator.GenerateCodeSnippet(snippetModel);

var expected = @"
const graphServiceClient = GraphServiceClient.init({authProvider});

const configuration = {
queryParameters : {
select: [""displayName"",""id""],
}
};

// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=typescript

const result = async () => {
await graphServiceClient.me.get(configuration);
}
Expand All @@ -464,7 +499,6 @@ public async Task GeneratesCountBooleanQueryParametersAsync()
var result = _generator.GenerateCodeSnippet(snippetModel);

var expected = @"
const graphServiceClient = GraphServiceClient.init({authProvider});

const configuration = {
queryParameters : {
Expand All @@ -473,6 +507,8 @@ public async Task GeneratesCountBooleanQueryParametersAsync()
}
};

// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=typescript

const result = async () => {
await graphServiceClient.users.get(configuration);
}
Expand All @@ -491,14 +527,15 @@ public async Task GeneratesSkipQueryParametersAsync()
var result = _generator.GenerateCodeSnippet(snippetModel);

var expected = @"
const graphServiceClient = GraphServiceClient.init({authProvider});

const configuration = {
queryParameters : {
skip: 10,
}
};

// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=typescript

const result = async () => {
await graphServiceClient.users.get(configuration);
}
Expand Down Expand Up @@ -529,14 +566,15 @@ public async Task GeneratesRequestHeadersAsync()
var result = _generator.GenerateCodeSnippet(snippetModel);

var expected = @"
const graphServiceClient = GraphServiceClient.init({authProvider});

const configuration = {
headers : {
""ConsistencyLevel"": ""eventual"",
}
};

// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=typescript

const result = async () => {
await graphServiceClient.groups.get(configuration);
}
Expand Down Expand Up @@ -574,7 +612,10 @@ public async Task GenerateAdditionalDataAsync()
var result = _generator.GenerateCodeSnippet(snippetModel);

var expected = @"
const graphServiceClient = GraphServiceClient.init({authProvider});
// Dependencies
import ""@microsoft/msgraph-sdk-teams"";
import { ChatMessage, BodyTypeObject } from ""@microsoft/msgraph-sdk/models"";
//other-imports

const requestBody : ChatMessage = {
createdDateTime : new Date(""2019-02-04T19:58:15.511Z""),
Expand All @@ -588,13 +629,15 @@ public async Task GenerateAdditionalDataAsync()
},
},
body : {
contentType : BodyType.Html,
contentType : BodyTypeObject.Html,
content : ""Hello World"",
},
};

// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=typescript

const result = async () => {
await graphServiceClient.teamsById(""team-id"").channelsById(""channel-id"").messages.post(requestBody);
await graphServiceClient.teams.byTeamId(""team-id"").channels.byChannelId(""channel-id"").messages.post(requestBody);
}
";

Expand Down Expand Up @@ -628,24 +671,29 @@ public async Task GeneratesEnumsWhenVariableIsEnumAsync()
var result = _generator.GenerateCodeSnippet(snippetModel);

var expected = @"
const graphServiceClient = GraphServiceClient.init({authProvider});
import ""@microsoft/msgraph-sdk-identitygovernance"";
import { AccessReviewScheduleDefinition, RecurrencePatternTypeObject, RecurrenceRangeTypeObject } from ""@microsoft/msgraph-sdk/models"";
import { DateOnly } from ""@microsoft/kiota-abstractions"";
//other-imports

const requestBody : AccessReviewScheduleDefinition = {
displayName : ""Test create"",
settings : {
recurrence : {
pattern : {
type : RecurrencePatternType.Weekly,
type : RecurrencePatternTypeObject.Weekly,
interval : 1,
},
range : {
type : RecurrenceRangeType.NoEnd,
startDate : ""2020-09-08T12:02:30.667Z"",
type : RecurrenceRangeTypeObject.NoEnd,
startDate : DateOnly.parse(""2020-09-08T12:02:30.667Z""),
},
},
},
};

// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=typescript

const result = async () => {
await graphServiceClient.identityGovernance.accessReviews.definitions.post(requestBody);
}
Expand Down
Loading
Loading