Skip to content

Commit c6353a7

Browse files
authored
add bearer auth support to C# 2.0 client (#5978)
1 parent a15da6f commit c6353a7

File tree

12 files changed

+86
-41
lines changed

12 files changed

+86
-41
lines changed

bin/csharp-dotnet2-petstore.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ fi
2727

2828
# if you've executed sbt assembly previously it will use that instead.
2929
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
30-
ags="generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g csharp-dotnet2 -o samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient --additional-properties hideGenerationTimestamp=true $@"
30+
ags="generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g csharp-dotnet2 -t modules/openapi-generator/src/main/resources/csharp-dotnet2 -o samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient --additional-properties hideGenerationTimestamp=true $@"
3131

3232
java $JAVA_OPTS -jar $executable $ags

bin/windows/csharp-dotnet2-petstore.bat

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ If Not Exist %executable% (
55
)
66

77
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
8-
set ags=generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g csharp-dotnet2 -o samples/client/petstore/csharp-dotnet2/OpenApiClientTest/Lib/OpenApiClient --additional-properties hideGenerationTimestamp=true
8+
set ags=generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g csharp-dotnet2 -t modules/openapi-generator/src/main/resources/csharp-dotnet2 -o samples/client/petstore/csharp-dotnet2/OpenApiClientTest/Lib/OpenApiClient --additional-properties hideGenerationTimestamp=true
99

1010
java %JAVA_OPTS% -jar %executable% %ags%

modules/openapi-generator/src/main/resources/csharp-dotnet2/ApiClient.mustache

+19-2
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,25 @@ namespace {{clientPackage}}
261261
{
262262
{{#authMethods}}
263263
case "{{name}}":
264-
{{#isApiKey}}{{#isKeyInHeader}}headerParams["{{keyParamName}}"] = GetApiKeyWithPrefix("{{keyParamName}}");{{/isKeyInHeader}}{{#isKeyInQuery}}queryParams["{{keyParamName}}"] = GetApiKeyWithPrefix("{{keyParamName}}");{{/isKeyInQuery}}{{/isApiKey}}{{#isBasic}}headerParams["Authorization"] = "Basic " + Base64Encode(Configuration.Username + ":" + Configuration.Password);{{/isBasic}}
265-
{{#isOAuth}}//TODO support oauth{{/isOAuth}}
264+
{{#isApiKey}}
265+
{{#isKeyInHeader}}
266+
headerParams["{{keyParamName}}"] = GetApiKeyWithPrefix("{{keyParamName}}");
267+
{{/isKeyInHeader}}
268+
{{#isKeyInQuery}}
269+
queryParams["{{keyParamName}}"] = GetApiKeyWithPrefix("{{keyParamName}}");
270+
{{/isKeyInQuery}}
271+
{{/isApiKey}}
272+
{{#isBasic}}
273+
{{#isBasicBasic}}
274+
headerParams["Authorization"] = "Basic " + Base64Encode(Configuration.Username + ":" + Configuration.Password);
275+
{{/isBasicBasic}}
276+
{{#isBasicBearer}}
277+
headerParams["Authorization"] = "Bearer " + Configuration.AccessToken;
278+
{{/isBasicBearer}}
279+
{{/isBasic}}
280+
{{#isOAuth}}
281+
headerParams["Authorization"] = "Bearer " + Configuration.AccessToken;
282+
{{/isOAuth}}
266283
break;
267284
{{/authMethods}}
268285
default:

modules/openapi-generator/src/main/resources/csharp-dotnet2/Configuration.mustache

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ namespace {{clientPackage}}
3737
/// <value>The password.</value>
3838
public static String Password { get; set; }
3939

40+
/// <summary>
41+
/// Gets or sets the access token (Bearer/OAuth authentication).
42+
/// </summary>
43+
/// <value>The access token.</value>
44+
public static String AccessToken { get; set; }
45+
4046
/// <summary>
4147
/// Gets or sets the API key based on the authentication name.
4248
/// </summary>

modules/openapi-generator/src/main/resources/csharp-dotnet2/README.mustache

+32-9
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,31 @@ namespace Example
5656
{
5757
public void main()
5858
{
59-
{{#apiInfo}}{{#apis}}{{#-first}}{{#operations}}{{#operation}}{{#-first}}{{#hasAuthMethods}}{{#authMethods}}{{#isBasic}}
59+
{{#apiInfo}}{{#apis}}{{#-first}}{{#operations}}{{#operation}}{{#-first}}
60+
{{#hasAuthMethods}}
61+
{{#authMethods}}
62+
{{#isBasic}}
63+
{{#isBasicBasic}}
6064
// Configure HTTP basic authorization: {{{name}}}
6165
Configuration.Default.Username = "YOUR_USERNAME";
62-
Configuration.Default.Password = "YOUR_PASSWORD";{{/isBasic}}{{#isApiKey}}
66+
Configuration.Default.Password = "YOUR_PASSWORD";
67+
{{/isBasicBasic}}
68+
{{#isBasicBearer}}
69+
// Configure Bearer access token for authorization: {{{name}}}
70+
Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN";
71+
{{/isBasicBearer}}
72+
{{/isBasic}}
73+
{{#isApiKey}}
6374
// Configure API key authorization: {{{name}}}
6475
Configuration.Default.ApiKey.Add("{{{keyParamName}}}", "YOUR_API_KEY");
6576
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
66-
// Configuration.Default.ApiKeyPrefix.Add("{{{keyParamName}}}", "Bearer");{{/isApiKey}}{{#isOAuth}}
77+
// Configuration.Default.ApiKeyPrefix.Add("{{{keyParamName}}}", "Bearer");
78+
{{/isApiKey}}
79+
{{#isOAuth}}
6780
// Configure OAuth2 access token for authorization: {{{name}}}
68-
Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN";{{/isOAuth}}{{/authMethods}}
81+
Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN";
82+
{{/isOAuth}}
83+
{{/authMethods}}
6984
{{/hasAuthMethods}}
7085

7186
var apiInstance = new {{classname}}();
@@ -123,21 +138,29 @@ No model defined in this package
123138
All endpoints do not require authorization.
124139
{{/authMethods}}
125140
{{#authMethods}}
126-
{{#last}}
141+
{{#-last}}
127142
Authentication schemes defined for the API:
128-
{{/last}}
143+
{{/-last}}
129144
{{/authMethods}}
130145
{{#authMethods}}
131146
<a name="{{name}}"></a>
132147
### {{name}}
133148

134-
{{#isApiKey}}- **Type**: API key
149+
{{#isApiKey}}
150+
- **Type**: API key
135151
- **API key parameter name**: {{keyParamName}}
136152
- **Location**: {{#isKeyInQuery}}URL query string{{/isKeyInQuery}}{{#isKeyInHeader}}HTTP header{{/isKeyInHeader}}
137153
{{/isApiKey}}
138-
{{#isBasic}}- **Type**: HTTP basic authentication
154+
{{#isBasic}}
155+
{{#isBasicBasic}}
156+
- **Type**: HTTP basic authentication
157+
{{/isBasicBasic}}
158+
{{#isBasicBearer}}
159+
- **Type**: HTTP bearer authentication
160+
{{/isBasicBearer}}
139161
{{/isBasic}}
140-
{{#isOAuth}}- **Type**: OAuth
162+
{{#isOAuth}}
163+
- **Type**: OAuth
141164
- **Flow**: {{flow}}
142165
- **Authorization URL**: {{authorizationUrl}}
143166
- **Scopes**: {{^scopes}}N/A{{/scopes}}

modules/openapi-generator/src/main/resources/csharp-dotnet2/api_doc.mustache

+19-5
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,32 @@ namespace Example
3232
{
3333
public void main()
3434
{
35-
{{#hasAuthMethods}}{{#authMethods}}{{#isBasic}}
35+
{{#hasAuthMethods}}
36+
{{#authMethods}}
37+
{{#isBasic}}
38+
{{#isBasicBasic}}
3639
// Configure HTTP basic authorization: {{{name}}}
3740
Configuration.Default.Username = "YOUR_USERNAME";
38-
Configuration.Default.Password = "YOUR_PASSWORD";{{/isBasic}}{{#isApiKey}}
41+
Configuration.Default.Password = "YOUR_PASSWORD";
42+
{{/isBasicBasic}}
43+
{{#isBasicBearer}}
44+
// Configure Bearer access token: {{{name}}}
45+
Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN";
46+
{{/isBasicBearer}}
47+
{{/isBasic}}
48+
{{#isApiKey}}
3949
// Configure API key authorization: {{{name}}}
4050
Configuration.Default.ApiKey.Add("{{{keyParamName}}}", "YOUR_API_KEY");
4151
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
42-
// Configuration.Default.ApiKeyPrefix.Add("{{{keyParamName}}}", "Bearer");{{/isApiKey}}{{#isOAuth}}
52+
// Configuration.Default.ApiKeyPrefix.Add("{{{keyParamName}}}", "Bearer");
53+
{{/isApiKey}}
54+
{{#isOAuth}}
4355
// Configure OAuth2 access token for authorization: {{{name}}}
44-
Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN";{{/isOAuth}}{{/authMethods}}
45-
{{/hasAuthMethods}}
56+
Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN";
57+
{{/isOAuth}}
58+
{{/authMethods}}
4659

60+
{{/hasAuthMethods}}
4761
var apiInstance = new {{classname}}();
4862
{{#allParams}}
4963
{{#isPrimitiveType}}

samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ Class | Method | HTTP request | Description
112112
<a name="documentation-for-authorization"></a>
113113
## Documentation for Authorization
114114

115+
Authentication schemes defined for the API:
115116
<a name="api_key"></a>
116117
### api_key
117118

samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/docs/PetApi.md

-8
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ namespace Example
3434
{
3535
public void main()
3636
{
37-
3837
// Configure OAuth2 access token for authorization: petstore_auth
3938
Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN";
4039

@@ -96,7 +95,6 @@ namespace Example
9695
{
9796
public void main()
9897
{
99-
10098
// Configure OAuth2 access token for authorization: petstore_auth
10199
Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN";
102100

@@ -162,7 +160,6 @@ namespace Example
162160
{
163161
public void main()
164162
{
165-
166163
// Configure OAuth2 access token for authorization: petstore_auth
167164
Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN";
168165

@@ -227,7 +224,6 @@ namespace Example
227224
{
228225
public void main()
229226
{
230-
231227
// Configure OAuth2 access token for authorization: petstore_auth
232228
Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN";
233229

@@ -292,7 +288,6 @@ namespace Example
292288
{
293289
public void main()
294290
{
295-
296291
// Configure API key authorization: api_key
297292
Configuration.Default.ApiKey.Add("api_key", "YOUR_API_KEY");
298293
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
@@ -357,7 +352,6 @@ namespace Example
357352
{
358353
public void main()
359354
{
360-
361355
// Configure OAuth2 access token for authorization: petstore_auth
362356
Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN";
363357

@@ -419,7 +413,6 @@ namespace Example
419413
{
420414
public void main()
421415
{
422-
423416
// Configure OAuth2 access token for authorization: petstore_auth
424417
Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN";
425418

@@ -485,7 +478,6 @@ namespace Example
485478
{
486479
public void main()
487480
{
488-
489481
// Configure OAuth2 access token for authorization: petstore_auth
490482
Configuration.Default.AccessToken = "YOUR_ACCESS_TOKEN";
491483

samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/docs/StoreApi.md

-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ namespace Example
3232
{
3333
public void main()
3434
{
35-
3635
var apiInstance = new StoreApi();
3736
var orderId = orderId_example; // string | ID of the order that needs to be deleted
3837
@@ -93,7 +92,6 @@ namespace Example
9392
{
9493
public void main()
9594
{
96-
9795
// Configure API key authorization: api_key
9896
Configuration.Default.ApiKey.Add("api_key", "YOUR_API_KEY");
9997
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
@@ -156,7 +154,6 @@ namespace Example
156154
{
157155
public void main()
158156
{
159-
160157
var apiInstance = new StoreApi();
161158
var orderId = 789; // long? | ID of pet that needs to be fetched
162159
@@ -216,7 +213,6 @@ namespace Example
216213
{
217214
public void main()
218215
{
219-
220216
var apiInstance = new StoreApi();
221217
var body = new Order(); // Order | order placed for purchasing the pet
222218

samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/docs/UserApi.md

-8
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ namespace Example
3636
{
3737
public void main()
3838
{
39-
4039
var apiInstance = new UserApi();
4140
var body = new User(); // User | Created user object
4241
@@ -95,7 +94,6 @@ namespace Example
9594
{
9695
public void main()
9796
{
98-
9997
var apiInstance = new UserApi();
10098
var body = new List<User>(); // List<User> | List of user object
10199
@@ -154,7 +152,6 @@ namespace Example
154152
{
155153
public void main()
156154
{
157-
158155
var apiInstance = new UserApi();
159156
var body = new List<User>(); // List<User> | List of user object
160157
@@ -215,7 +212,6 @@ namespace Example
215212
{
216213
public void main()
217214
{
218-
219215
var apiInstance = new UserApi();
220216
var username = username_example; // string | The name that needs to be deleted
221217
@@ -274,7 +270,6 @@ namespace Example
274270
{
275271
public void main()
276272
{
277-
278273
var apiInstance = new UserApi();
279274
var username = username_example; // string | The name that needs to be fetched. Use user1 for testing.
280275
@@ -334,7 +329,6 @@ namespace Example
334329
{
335330
public void main()
336331
{
337-
338332
var apiInstance = new UserApi();
339333
var username = username_example; // string | The user name for login
340334
var password = password_example; // string | The password for login in clear text
@@ -396,7 +390,6 @@ namespace Example
396390
{
397391
public void main()
398392
{
399-
400393
var apiInstance = new UserApi();
401394

402395
try
@@ -453,7 +446,6 @@ namespace Example
453446
{
454447
public void main()
455448
{
456-
457449
var apiInstance = new UserApi();
458450
var username = username_example; // string | name that need to be deleted
459451
var body = new User(); // User | Updated user object

samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/src/main/CsharpDotNet2/Org/OpenAPITools/Client/ApiClient.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,9 @@ public void UpdateParamsForAuth(Dictionary<String, String> queryParams, Dictiona
261261
{
262262
case "api_key":
263263
headerParams["api_key"] = GetApiKeyWithPrefix("api_key");
264-
265264
break;
266265
case "petstore_auth":
267-
268-
//TODO support oauth
266+
headerParams["Authorization"] = "Bearer " + Configuration.AccessToken;
269267
break;
270268
default:
271269
//TODO show warning about security definition not found

samples/client/petstore/csharp-dotnet2/OpenAPIClientTest/Lib/OpenAPIClient/src/main/CsharpDotNet2/Org/OpenAPITools/Client/Configuration.cs

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ public class Configuration
3737
/// <value>The password.</value>
3838
public static String Password { get; set; }
3939

40+
/// <summary>
41+
/// Gets or sets the access token (Bearer/OAuth authentication).
42+
/// </summary>
43+
/// <value>The access token.</value>
44+
public static String AccessToken { get; set; }
45+
4046
/// <summary>
4147
/// Gets or sets the API key based on the authentication name.
4248
/// </summary>

0 commit comments

Comments
 (0)