Skip to content

Commit be4f04e

Browse files
chore: bump MTG version & consume oauth updates (#5365)
1 parent f2fa136 commit be4f04e

File tree

15 files changed

+156
-26
lines changed

15 files changed

+156
-26
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Collections.Generic;
5+
6+
namespace AutoRest.CSharp.Common.Input
7+
{
8+
internal record InputOAuth2Flow(IReadOnlyCollection<string> Scopes, string? AuthorizationUrl, string? TokenUrl, string? RefreshUrl);
9+
}

src/AutoRest.CSharp/Common/Input/InputTypes/Serialization/TypeSpecInputOAuth2AuthConverter.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,42 @@ private static InputOAuth2Auth CreateInputOAuth2Auth(ref Utf8JsonReader reader,
2222
{
2323
reader.Read();
2424
}
25-
IReadOnlyList<string>? scopes = null;
25+
IReadOnlyList<InputOAuth2Flow>? flows = null;
26+
2627
while (reader.TokenType != JsonTokenType.EndObject)
2728
{
28-
var isKnownProperty = reader.TryReadComplexType("scopes", options, ref scopes);
29+
var isKnownProperty = reader.TryReadComplexType("flows", options, ref flows);
30+
2931
if (!isKnownProperty)
3032
{
3133
reader.SkipProperty();
3234
}
3335
}
34-
var result = new InputOAuth2Auth(scopes ?? []);
36+
37+
var result = new InputOAuth2Auth(AggregateScopesFromFlows(flows));
3538
return result;
3639
}
40+
41+
private static IReadOnlyList<string> AggregateScopesFromFlows(IReadOnlyList<InputOAuth2Flow>? flows)
42+
{
43+
if (flows == null)
44+
{
45+
return [];
46+
}
47+
48+
var scopes = new HashSet<string>();
49+
foreach (var flow in flows)
50+
{
51+
if (flow.Scopes != null)
52+
{
53+
foreach (var scope in flow.Scopes)
54+
{
55+
scopes.Add(scope);
56+
}
57+
}
58+
}
59+
60+
return [.. scopes];
61+
}
3762
}
3863
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text.Json;
7+
using System.Text.Json.Serialization;
8+
9+
namespace AutoRest.CSharp.Common.Input
10+
{
11+
internal class TypeSpecInputOAuth2FlowConverter : JsonConverter<InputOAuth2Flow>
12+
{
13+
public override InputOAuth2Flow? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
14+
=> CreateInputOAuth2Auth(ref reader, options);
15+
16+
public override void Write(Utf8JsonWriter writer, InputOAuth2Flow value, JsonSerializerOptions options)
17+
=> throw new NotSupportedException("Writing not supported");
18+
19+
private static InputOAuth2Flow CreateInputOAuth2Auth(ref Utf8JsonReader reader, JsonSerializerOptions options)
20+
{
21+
if (reader.TokenType == JsonTokenType.StartObject)
22+
{
23+
reader.Read();
24+
}
25+
IReadOnlyList<string>? scopes = null;
26+
string? authorizationUrl = null;
27+
string? tokenUrl = null;
28+
string? refreshUrl = null;
29+
30+
while (reader.TokenType != JsonTokenType.EndObject)
31+
{
32+
var isKnownProperty = reader.TryReadComplexType("scopes", options, ref scopes)
33+
|| reader.TryReadString("authorizationUrl", ref authorizationUrl)
34+
|| reader.TryReadString("tokenUrl", ref tokenUrl)
35+
|| reader.TryReadString("refreshUrl", ref refreshUrl);
36+
37+
if (!isKnownProperty)
38+
{
39+
reader.SkipProperty();
40+
}
41+
}
42+
var result = new InputOAuth2Flow(scopes ?? [], authorizationUrl, tokenUrl, refreshUrl);
43+
44+
return result;
45+
}
46+
}
47+
}

src/AutoRest.CSharp/Common/Input/InputTypes/Serialization/TypeSpecSerialization.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ internal static class TypeSpecSerialization
6060
new TypeSpecInputLongRunningServiceMethodConverter(referenceHandler),
6161
new TypeSpecInputLongRunningServiceMetadataConverter(),
6262
new TypeSpecInputLongRunningPagingServiceMethodConverter(referenceHandler),
63+
new TypeSpecInputOAuth2FlowConverter(),
6364
}
6465
};
6566

src/TypeSpec.Extension/Emitter.Csharp/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
],
3636
"dependencies": {
3737
"@autorest/csharp": "3.0.0-beta.20240625.4",
38-
"@typespec/http-client-csharp": "1.0.0-alpha.20250703.3"
38+
"@typespec/http-client-csharp": "1.0.0-alpha.20250715.1"
3939
},
4040
"peerDependencies": {
4141
"@azure-tools/typespec-azure-core": ">=0.57.0 <0.58.0 || ~0.58.0-0",

test/CadlRanchProjects/authentication/oauth2/tspCodeModel.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,13 @@
219219
],
220220
"auth": {
221221
"oAuth2": {
222-
"scopes": [
223-
"https://security.microsoft.com/.default"
222+
"flows": [
223+
{
224+
"scopes": [
225+
"https://security.microsoft.com/.default"
226+
],
227+
"authorizationUrl": "https://login.microsoftonline.com/common/oauth2/authorize"
228+
}
224229
]
225230
}
226231
}

test/CadlRanchProjects/authentication/union/tspCodeModel.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,13 @@
134134
"in": "header"
135135
},
136136
"oAuth2": {
137-
"scopes": [
138-
"https://security.microsoft.com/.default"
137+
"flows": [
138+
{
139+
"scopes": [
140+
"https://security.microsoft.com/.default"
141+
],
142+
"authorizationUrl": "https://login.microsoftonline.com/common/oauth2/authorize"
143+
}
139144
]
140145
}
141146
}

test/CadlRanchProjects/azure/resource-manager/common-properties/tspCodeModel.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3241,8 +3241,13 @@
32413241
],
32423242
"auth": {
32433243
"oAuth2": {
3244-
"scopes": [
3245-
"user_impersonation"
3244+
"flows": [
3245+
{
3246+
"scopes": [
3247+
"user_impersonation"
3248+
],
3249+
"authorizationUrl": "https://login.microsoftonline.com/common/oauth2/authorize"
3250+
}
32463251
]
32473252
}
32483253
}

test/CadlRanchProjects/azure/resource-manager/non-resource/tspCodeModel.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,8 +1064,13 @@
10641064
],
10651065
"auth": {
10661066
"oAuth2": {
1067-
"scopes": [
1068-
"user_impersonation"
1067+
"flows": [
1068+
{
1069+
"scopes": [
1070+
"user_impersonation"
1071+
],
1072+
"authorizationUrl": "https://login.microsoftonline.com/common/oauth2/authorize"
1073+
}
10691074
]
10701075
}
10711076
}

0 commit comments

Comments
 (0)