Skip to content

Commit 440be08

Browse files
committed
update
1 parent 3b58266 commit 440be08

8 files changed

Lines changed: 449 additions & 8 deletions

File tree

modules/openapi-generator/src/test/resources/3_0/csharp/petstore-with-fake-endpoints-models-for-testing-with-http-signature-oneof-primitive-types-localhost.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2783,7 +2783,8 @@ components:
27832783
# JsonReader will give back C# System.Double regardless of format.
27842784
- type: number
27852785
format: float32
2786-
- $ref: '#/components/schemas/MixedSubId'
2786+
- type: object
2787+
$ref: '#/components/schemas/MixedSubId'
27872788
description: Mixed oneOf types for testing
27882789
MixedAnyOf:
27892790
properties:
@@ -2797,7 +2798,8 @@ components:
27972798
# JsonReader will give back C# System.Double regardless of format.
27982799
- type: number
27992800
format: float32
2800-
- $ref: '#/components/schemas/MixedSubId'
2801+
- type: object
2802+
$ref: '#/components/schemas/MixedSubId'
28012803

28022804
description: Mixed anyOf types for testing
28032805
MixedSubId:

modules/openapi-generator/src/test/resources/3_0/csharp/petstore-with-fake-endpoints-models-for-testing-with-http-signature-oneof-primitive-types.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2781,7 +2781,8 @@ components:
27812781
# JsonReader will give back C# System.Double regardless of format.
27822782
- type: number
27832783
format: float32
2784-
- $ref: '#/components/schemas/MixedSubId'
2784+
- type: object
2785+
$ref: '#/components/schemas/MixedSubId'
27852786
description: Mixed oneOf types for testing
27862787
MixedAnyOf:
27872788
properties:
@@ -2795,10 +2796,10 @@ components:
27952796
# JsonReader will give back C# System.Double regardless of format.
27962797
- type: number
27972798
format: float32
2798-
- $ref: '#/components/schemas/MixedSubId'
2799-
2799+
- type: object
2800+
$ref: '#/components/schemas/MixedSubId'
28002801
description: Mixed anyOf types for testing
28012802
MixedSubId:
28022803
properties:
28032804
id:
2804-
type: string
2805+
type: string

modules/openapi-generator/src/test/resources/3_0/csharp/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2839,7 +2839,8 @@ components:
28392839
# JsonReader will give back C# System.Double regardless of format.
28402840
- type: number
28412841
format: float32
2842-
- $ref: '#/components/schemas/MixedSubId'
2842+
- type: object
2843+
$ref: '#/components/schemas/MixedSubId'
28432844
description: Mixed oneOf types for testing
28442845
MixedAnyOf:
28452846
properties:
@@ -2853,7 +2854,8 @@ components:
28532854
# JsonReader will give back C# System.Double regardless of format.
28542855
- type: number
28552856
format: float32
2856-
- $ref: '#/components/schemas/MixedSubId'
2857+
- type: object
2858+
$ref: '#/components/schemas/MixedSubId'
28572859
description: Mixed anyOf types for testing
28582860
MixedSubId:
28592861
properties:
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
/*
2+
* OpenAPI Petstore
3+
*
4+
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
5+
*
6+
* The version of the OpenAPI document: 1.0.0
7+
* Generated by: https://github.com/openapitools/openapi-generator.git
8+
*/
9+
10+
using System;
11+
using Microsoft.Extensions.Hosting;
12+
using Microsoft.Extensions.DependencyInjection;
13+
using System.Collections.Generic;
14+
using System.Security.Cryptography;
15+
using Org.OpenAPITools.Client;
16+
using Org.OpenAPITools.Api;
17+
using Org.OpenAPITools.Extensions;
18+
using Xunit;
19+
20+
namespace Org.OpenAPITools.Test.Api
21+
{
22+
/// <summary>
23+
/// Tests the dependency injection.
24+
/// </summary>
25+
public class DependencyInjectionTest
26+
{
27+
private readonly IHost _hostUsingConfigureWithoutAClient =
28+
Host.CreateDefaultBuilder([]).ConfigureApi((context, services, options) =>
29+
{
30+
ApiKeyToken apiKeyToken1 = new("<token>", ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
31+
options.AddTokens(apiKeyToken1);
32+
33+
ApiKeyToken apiKeyToken2 = new("<token>", ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1));
34+
options.AddTokens(apiKeyToken2);
35+
36+
BearerToken bearerToken1 = new("<token>", timeout: TimeSpan.FromSeconds(1));
37+
options.AddTokens(bearerToken1);
38+
39+
BasicToken basicToken1 = new("<username>", "<password>", timeout: TimeSpan.FromSeconds(1));
40+
options.AddTokens(basicToken1);
41+
42+
HttpSigningConfiguration config1 = new("<keyId>", "<keyFilePath>", null, [], HashAlgorithmName.SHA256, "<signingAlgorithm>", 0);
43+
HttpSignatureToken httpSignatureToken1 = new(config1, timeout: TimeSpan.FromSeconds(1));
44+
options.AddTokens(httpSignatureToken1);
45+
46+
OAuthToken oauthToken1 = new("token", timeout: TimeSpan.FromSeconds(1));
47+
options.AddTokens(oauthToken1);
48+
})
49+
.Build();
50+
51+
private readonly IHost _hostUsingConfigureWithAClient =
52+
Host.CreateDefaultBuilder([]).ConfigureApi((context, services, options) =>
53+
{
54+
ApiKeyToken apiKeyToken1 = new("<token>", ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
55+
options.AddTokens(apiKeyToken1);
56+
57+
ApiKeyToken apiKeyToken2 = new("<token>", ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1));
58+
options.AddTokens(apiKeyToken2);
59+
60+
BearerToken bearerToken1 = new("<token>", timeout: TimeSpan.FromSeconds(1));
61+
options.AddTokens(bearerToken1);
62+
63+
BasicToken basicToken1 = new("<username>", "<password>", timeout: TimeSpan.FromSeconds(1));
64+
options.AddTokens(basicToken1);
65+
66+
HttpSigningConfiguration config1 = new("<keyId>", "<keyFilePath>", null, [], HashAlgorithmName.SHA256, "<signingAlgorithm>", 0);
67+
HttpSignatureToken httpSignatureToken1 = new(config1, timeout: TimeSpan.FromSeconds(1));
68+
options.AddTokens(httpSignatureToken1);
69+
70+
OAuthToken oauthToken = new("token", timeout: TimeSpan.FromSeconds(1));
71+
options.AddTokens(oauthToken);
72+
options.AddApiHttpClients(client => client.BaseAddress = new Uri(ClientUtils.BASE_ADDRESS));
73+
})
74+
.Build();
75+
76+
private readonly IHost _hostUsingAddWithoutAClient =
77+
Host.CreateDefaultBuilder([]).ConfigureServices((host, services) =>
78+
{
79+
services.AddApi(options =>
80+
{
81+
ApiKeyToken apiKeyToken1 = new("<token>", ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
82+
options.AddTokens(apiKeyToken1);
83+
84+
ApiKeyToken apiKeyToken2 = new("<token>", ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1));
85+
options.AddTokens(apiKeyToken2);
86+
87+
BearerToken bearerToken1 = new("<token>", timeout: TimeSpan.FromSeconds(1));
88+
options.AddTokens(bearerToken1);
89+
90+
BasicToken basicToken1 = new("<username>", "<password>", timeout: TimeSpan.FromSeconds(1));
91+
options.AddTokens(basicToken1);
92+
93+
HttpSigningConfiguration config1 = new("<keyId>", "<keyFilePath>", null, [], HashAlgorithmName.SHA256, "<signingAlgorithm>", 0);
94+
HttpSignatureToken httpSignatureToken1 = new(config1, timeout: TimeSpan.FromSeconds(1));
95+
options.AddTokens(httpSignatureToken1);
96+
97+
OAuthToken oauthToken1 = new("token", timeout: TimeSpan.FromSeconds(1));
98+
options.AddTokens(oauthToken1);
99+
});
100+
})
101+
.Build();
102+
103+
private readonly IHost _hostUsingAddWithAClient =
104+
Host.CreateDefaultBuilder([]).ConfigureServices((host, services) =>
105+
{
106+
services.AddApi(options =>
107+
{
108+
ApiKeyToken apiKeyToken1 = new("<token>", ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
109+
options.AddTokens(apiKeyToken1);
110+
111+
ApiKeyToken apiKeyToken2 = new("<token>", ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1));
112+
options.AddTokens(apiKeyToken2);
113+
114+
BearerToken bearerToken1 = new("<token>", timeout: TimeSpan.FromSeconds(1));
115+
options.AddTokens(bearerToken1);
116+
117+
BasicToken basicToken1 = new("<username>", "<password>", timeout: TimeSpan.FromSeconds(1));
118+
options.AddTokens(basicToken1);
119+
120+
HttpSigningConfiguration config1 = new("<keyId>", "<keyFilePath>", null, [], HashAlgorithmName.SHA256, "<signingAlgorithm>", 0);
121+
HttpSignatureToken httpSignatureToken1 = new(config1, timeout: TimeSpan.FromSeconds(1));
122+
options.AddTokens(httpSignatureToken1);
123+
124+
OAuthToken oauthToken1 = new("token", timeout: TimeSpan.FromSeconds(1));
125+
options.AddTokens(oauthToken1);
126+
options.AddApiHttpClients(client => client.BaseAddress = new Uri(ClientUtils.BASE_ADDRESS));
127+
});
128+
})
129+
.Build();
130+
131+
/// <summary>
132+
/// Test dependency injection when using the configure method
133+
/// </summary>
134+
[Fact]
135+
public void ConfigureApiWithAClientTest()
136+
{
137+
var anotherFakeApi = _hostUsingConfigureWithAClient.Services.GetRequiredService<IAnotherFakeApi>();
138+
Assert.True(anotherFakeApi.HttpClient.BaseAddress != null);
139+
140+
var defaultApi = _hostUsingConfigureWithAClient.Services.GetRequiredService<IDefaultApi>();
141+
Assert.True(defaultApi.HttpClient.BaseAddress != null);
142+
143+
var fakeApi = _hostUsingConfigureWithAClient.Services.GetRequiredService<IFakeApi>();
144+
Assert.True(fakeApi.HttpClient.BaseAddress != null);
145+
146+
var fakeClassnameTags123Api = _hostUsingConfigureWithAClient.Services.GetRequiredService<IFakeClassnameTags123Api>();
147+
Assert.True(fakeClassnameTags123Api.HttpClient.BaseAddress != null);
148+
149+
var petApi = _hostUsingConfigureWithAClient.Services.GetRequiredService<IPetApi>();
150+
Assert.True(petApi.HttpClient.BaseAddress != null);
151+
152+
var storeApi = _hostUsingConfigureWithAClient.Services.GetRequiredService<IStoreApi>();
153+
Assert.True(storeApi.HttpClient.BaseAddress != null);
154+
155+
var userApi = _hostUsingConfigureWithAClient.Services.GetRequiredService<IUserApi>();
156+
Assert.True(userApi.HttpClient.BaseAddress != null);
157+
}
158+
159+
/// <summary>
160+
/// Test dependency injection when using the configure method
161+
/// </summary>
162+
[Fact]
163+
public void ConfigureApiWithoutAClientTest()
164+
{
165+
var anotherFakeApi = _hostUsingConfigureWithoutAClient.Services.GetRequiredService<IAnotherFakeApi>();
166+
Assert.True(anotherFakeApi.HttpClient.BaseAddress != null);
167+
168+
var defaultApi = _hostUsingConfigureWithoutAClient.Services.GetRequiredService<IDefaultApi>();
169+
Assert.True(defaultApi.HttpClient.BaseAddress != null);
170+
171+
var fakeApi = _hostUsingConfigureWithoutAClient.Services.GetRequiredService<IFakeApi>();
172+
Assert.True(fakeApi.HttpClient.BaseAddress != null);
173+
174+
var fakeClassnameTags123Api = _hostUsingConfigureWithoutAClient.Services.GetRequiredService<IFakeClassnameTags123Api>();
175+
Assert.True(fakeClassnameTags123Api.HttpClient.BaseAddress != null);
176+
177+
var petApi = _hostUsingConfigureWithoutAClient.Services.GetRequiredService<IPetApi>();
178+
Assert.True(petApi.HttpClient.BaseAddress != null);
179+
180+
var storeApi = _hostUsingConfigureWithoutAClient.Services.GetRequiredService<IStoreApi>();
181+
Assert.True(storeApi.HttpClient.BaseAddress != null);
182+
183+
var userApi = _hostUsingConfigureWithoutAClient.Services.GetRequiredService<IUserApi>();
184+
Assert.True(userApi.HttpClient.BaseAddress != null);
185+
}
186+
187+
/// <summary>
188+
/// Test dependency injection when using the add method
189+
/// </summary>
190+
[Fact]
191+
public void AddApiWithAClientTest()
192+
{
193+
var anotherFakeApi = _hostUsingAddWithAClient.Services.GetRequiredService<IAnotherFakeApi>();
194+
Assert.True(anotherFakeApi.HttpClient.BaseAddress != null);
195+
196+
var defaultApi = _hostUsingAddWithAClient.Services.GetRequiredService<IDefaultApi>();
197+
Assert.True(defaultApi.HttpClient.BaseAddress != null);
198+
199+
var fakeApi = _hostUsingAddWithAClient.Services.GetRequiredService<IFakeApi>();
200+
Assert.True(fakeApi.HttpClient.BaseAddress != null);
201+
202+
var fakeClassnameTags123Api = _hostUsingAddWithAClient.Services.GetRequiredService<IFakeClassnameTags123Api>();
203+
Assert.True(fakeClassnameTags123Api.HttpClient.BaseAddress != null);
204+
205+
var petApi = _hostUsingAddWithAClient.Services.GetRequiredService<IPetApi>();
206+
Assert.True(petApi.HttpClient.BaseAddress != null);
207+
208+
var storeApi = _hostUsingAddWithAClient.Services.GetRequiredService<IStoreApi>();
209+
Assert.True(storeApi.HttpClient.BaseAddress != null);
210+
211+
var userApi = _hostUsingAddWithAClient.Services.GetRequiredService<IUserApi>();
212+
Assert.True(userApi.HttpClient.BaseAddress != null);
213+
}
214+
215+
/// <summary>
216+
/// Test dependency injection when using the add method
217+
/// </summary>
218+
[Fact]
219+
public void AddApiWithoutAClientTest()
220+
{
221+
var anotherFakeApi = _hostUsingAddWithoutAClient.Services.GetRequiredService<IAnotherFakeApi>();
222+
Assert.True(anotherFakeApi.HttpClient.BaseAddress != null);
223+
224+
var defaultApi = _hostUsingAddWithoutAClient.Services.GetRequiredService<IDefaultApi>();
225+
Assert.True(defaultApi.HttpClient.BaseAddress != null);
226+
227+
var fakeApi = _hostUsingAddWithoutAClient.Services.GetRequiredService<IFakeApi>();
228+
Assert.True(fakeApi.HttpClient.BaseAddress != null);
229+
230+
var fakeClassnameTags123Api = _hostUsingAddWithoutAClient.Services.GetRequiredService<IFakeClassnameTags123Api>();
231+
Assert.True(fakeClassnameTags123Api.HttpClient.BaseAddress != null);
232+
233+
var petApi = _hostUsingAddWithoutAClient.Services.GetRequiredService<IPetApi>();
234+
Assert.True(petApi.HttpClient.BaseAddress != null);
235+
236+
var storeApi = _hostUsingAddWithoutAClient.Services.GetRequiredService<IStoreApi>();
237+
Assert.True(storeApi.HttpClient.BaseAddress != null);
238+
239+
var userApi = _hostUsingAddWithoutAClient.Services.GetRequiredService<IUserApi>();
240+
Assert.True(userApi.HttpClient.BaseAddress != null);
241+
}
242+
}
243+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// <auto-generated>
2+
/*
3+
* OpenAPI Petstore
4+
*
5+
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
6+
*
7+
* The version of the OpenAPI document: 1.0.0
8+
* Generated by: https://github.com/openapitools/openapi-generator.git
9+
*/
10+
11+
using System;
12+
using System.Collections.Generic;
13+
using System.Linq;
14+
15+
namespace Org.OpenAPITools.Client
16+
{
17+
/// <summary>
18+
/// Provides a token to the api clients. Tokens will be rate limited based on the provided TimeSpan.
19+
/// </summary>
20+
/// <typeparam name="TTokenBase"></typeparam>
21+
public class RateLimitProvider<TTokenBase> : TokenProvider<TTokenBase> where TTokenBase : TokenBase
22+
{
23+
internal Dictionary<string, global::System.Threading.Channels.Channel<TTokenBase>> AvailableTokens { get; } = new();
24+
25+
/// <summary>
26+
/// Instantiates a ThrottledTokenProvider. Your tokens will be rate limited based on the token's timeout.
27+
/// </summary>
28+
/// <param name="container"></param>
29+
public RateLimitProvider(TokenContainer<TTokenBase> container) : base(container.Tokens)
30+
{
31+
foreach(TTokenBase token in _tokens)
32+
token.StartTimer(token.Timeout ?? TimeSpan.FromMilliseconds(40));
33+
34+
if (container is TokenContainer<ApiKeyToken> apiKeyTokenContainer)
35+
{
36+
string[] headers = apiKeyTokenContainer.Tokens.Select(t => ClientUtils.ApiKeyHeaderToString(t.Header)).Distinct().ToArray();
37+
38+
foreach (string header in headers)
39+
{
40+
global::System.Threading.Channels.BoundedChannelOptions options = new global::System.Threading.Channels.BoundedChannelOptions(apiKeyTokenContainer.Tokens.Count(t => ClientUtils.ApiKeyHeaderToString(t.Header).Equals(header)))
41+
{
42+
FullMode = global::System.Threading.Channels.BoundedChannelFullMode.DropWrite
43+
};
44+
45+
AvailableTokens.Add(header, global::System.Threading.Channels.Channel.CreateBounded<TTokenBase>(options));
46+
}
47+
}
48+
else
49+
{
50+
global::System.Threading.Channels.BoundedChannelOptions options = new global::System.Threading.Channels.BoundedChannelOptions(_tokens.Length)
51+
{
52+
FullMode = global::System.Threading.Channels.BoundedChannelFullMode.DropWrite
53+
};
54+
55+
AvailableTokens.Add(string.Empty, global::System.Threading.Channels.Channel.CreateBounded<TTokenBase>(options));
56+
}
57+
58+
foreach (var availableToken in AvailableTokens)
59+
foreach(TTokenBase token in _tokens)
60+
{
61+
if (token is ApiKeyToken apiKeyToken)
62+
{
63+
if (ClientUtils.ApiKeyHeaderToString(apiKeyToken.Header) == availableToken.Key)
64+
{
65+
token.TokenBecameAvailable += ((sender) => availableToken.Value.Writer.TryWrite((TTokenBase)sender));
66+
}
67+
} else
68+
{
69+
token.TokenBecameAvailable += ((sender) => availableToken.Value.Writer.TryWrite((TTokenBase)sender));
70+
}
71+
}
72+
}
73+
74+
internal override async System.Threading.Tasks.ValueTask<TTokenBase> GetAsync(string header = "", System.Threading.CancellationToken cancellation = default)
75+
{
76+
if (!AvailableTokens.TryGetValue(header, out global::System.Threading.Channels.Channel<TTokenBase> tokens))
77+
throw new KeyNotFoundException($"Could not locate a token for header '{header}'.");
78+
79+
return await tokens.Reader.ReadAsync(cancellation).ConfigureAwait(false);
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)