Skip to content

Commit 752ed38

Browse files
committed
fix: fix something
1 parent 46e278e commit 752ed38

16 files changed

+326
-336
lines changed

src/Ocelot/Configuration/Repository/DiskFileConfigurationRepository.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ public Task<Response<FileConfiguration>> Get()
5050
jsonConfiguration = FileSys.ReadAllText(_environmentFile.FullName);
5151
}
5252

53-
var fileConfiguration = JsonSerializer.Deserialize<FileConfiguration>(jsonConfiguration, JsonSerializerOptionsFactory.Web);
53+
var fileConfiguration = JsonSerializer.Deserialize<FileConfiguration>(jsonConfiguration, JsonSerializerOptionsFactory.Web);
5454

5555
return Task.FromResult<Response<FileConfiguration>>(new OkResponse<FileConfiguration>(fileConfiguration));
5656
}
5757

58-
public Task<Response> Set(FileConfiguration fileConfiguration)
59-
{
60-
var jsonConfiguration = JsonSerializer.Serialize(fileConfiguration, JsonSerializerOptionsFactory.WebWriteIndented);
58+
public Task<Response> Set(FileConfiguration fileConfiguration)
59+
{
60+
var jsonConfiguration = JsonSerializer.Serialize(fileConfiguration, JsonSerializerOptionsFactory.WebWriteIndented);
6161

6262
lock (_lock)
6363
{

src/Ocelot/Configuration/Repository/FileConfigurationPoller.cs

+10-10
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private async Task Poll()
6969

7070
if (fileConfig.IsError)
7171
{
72-
_logger.LogWarning(() =>$"error geting file config, errors are {string.Join(',', fileConfig.Errors.Select(x => x.Message))}");
72+
_logger.LogWarning(() => $"error geting file config, errors are {string.Join(',', fileConfig.Errors.Select(x => x.Message))}");
7373
return;
7474
}
7575

@@ -90,15 +90,15 @@ private async Task Poll()
9090
_logger.LogInformation("Finished polling");
9191
}
9292

93-
/// <summary>
94-
/// We could do object comparison here but performance isnt really a problem. This might be an issue one day!.
95-
/// </summary>
96-
/// <returns>hash of the config.</returns>
97-
private static string ToJson(FileConfiguration config)
98-
{
99-
var currentHash = JsonSerializer.Serialize(config, JsonSerializerOptionsFactory.Web);
100-
return currentHash;
101-
}
93+
/// <summary>
94+
/// We could do object comparison here but performance isnt really a problem. This might be an issue one day!.
95+
/// </summary>
96+
/// <returns>hash of the config.</returns>
97+
private static string ToJson(FileConfiguration config)
98+
{
99+
var currentHash = JsonSerializer.Serialize(config, JsonSerializerOptionsFactory.Web);
100+
return currentHash;
101+
}
102102

103103
public void Dispose()
104104
{

src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs

+29-30
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using Microsoft.Extensions.Configuration.Memory;
44
using Ocelot.Configuration.File;
55
using Ocelot.Infrastructure;
6-
using System.Text;
76
using System.Text.Json;
87

98
namespace Ocelot.DependencyInjection;
@@ -92,8 +91,8 @@ public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder
9291
private static IConfigurationBuilder ApplyMergeOcelotJsonOption(IConfigurationBuilder builder, MergeOcelotJson mergeTo, string json,
9392
string primaryConfigFile, bool? optional, bool? reloadOnChange)
9493
{
95-
return mergeTo == MergeOcelotJson.ToMemory ?
96-
builder.AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(json))) :
94+
return mergeTo == MergeOcelotJson.ToMemory ?
95+
builder.AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(json))) :
9796
AddOcelotJsonFile(builder, json, primaryConfigFile, optional, reloadOnChange);
9897
}
9998

@@ -133,38 +132,38 @@ private static string GetMergedOcelotJson(string folder, IWebHostEnvironment env
133132
continue;
134133
}
135134

136-
var lines = File.ReadAllText(file.FullName);
137-
var config = JsonSerializer.Deserialize<FileConfiguration>(lines, JsonSerializerOptionsFactory.Web);
138-
if (file.Name.Equals(globalFileInfo.Name, StringComparison.OrdinalIgnoreCase) &&
139-
file.FullName.Equals(globalFileInfo.FullName, StringComparison.OrdinalIgnoreCase))
140-
{
141-
fileConfiguration.GlobalConfiguration = config.GlobalConfiguration;
142-
}
135+
var lines = File.ReadAllText(file.FullName);
136+
var config = JsonSerializer.Deserialize<FileConfiguration>(lines, JsonSerializerOptionsFactory.Web);
137+
if (file.Name.Equals(globalFileInfo.Name, StringComparison.OrdinalIgnoreCase) &&
138+
file.FullName.Equals(globalFileInfo.FullName, StringComparison.OrdinalIgnoreCase))
139+
{
140+
fileConfiguration.GlobalConfiguration = config.GlobalConfiguration;
141+
}
143142

144143
fileConfiguration.Aggregates.AddRange(config.Aggregates);
145144
fileConfiguration.Routes.AddRange(config.Routes);
146145
}
147146

148-
return JsonSerializer.Serialize(fileConfiguration, JsonSerializerOptionsFactory.WebWriteIndented);
149-
}
150-
151-
/// <summary>
152-
/// Adds Ocelot configuration by ready configuration object and writes JSON to the primary configuration file.<br/>
153-
/// Finally, adds JSON file as configuration provider.
154-
/// </summary>
155-
/// <remarks>Use optional arguments for injections and overridings.</remarks>
156-
/// <param name="builder">Configuration builder to extend.</param>
157-
/// <param name="fileConfiguration">File configuration to add as JSON provider.</param>
158-
/// <param name="primaryConfigFile">Primary config file.</param>
159-
/// <param name="optional">The 2nd argument of the AddJsonFile.</param>
160-
/// <param name="reloadOnChange">The 3rd argument of the AddJsonFile.</param>
161-
/// <returns>An <see cref="IConfigurationBuilder"/> object.</returns>
162-
public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder, FileConfiguration fileConfiguration,
163-
string primaryConfigFile = null, bool? optional = null, bool? reloadOnChange = null) // optional injections
164-
{
165-
var json = JsonSerializer.Serialize(fileConfiguration, JsonSerializerOptionsFactory.WebWriteIndented);
166-
return AddOcelotJsonFile(builder, json, primaryConfigFile, optional, reloadOnChange);
167-
}
147+
return JsonSerializer.Serialize(fileConfiguration, JsonSerializerOptionsFactory.WebWriteIndented);
148+
}
149+
150+
/// <summary>
151+
/// Adds Ocelot configuration by ready configuration object and writes JSON to the primary configuration file.<br/>
152+
/// Finally, adds JSON file as configuration provider.
153+
/// </summary>
154+
/// <remarks>Use optional arguments for injections and overridings.</remarks>
155+
/// <param name="builder">Configuration builder to extend.</param>
156+
/// <param name="fileConfiguration">File configuration to add as JSON provider.</param>
157+
/// <param name="primaryConfigFile">Primary config file.</param>
158+
/// <param name="optional">The 2nd argument of the AddJsonFile.</param>
159+
/// <param name="reloadOnChange">The 3rd argument of the AddJsonFile.</param>
160+
/// <returns>An <see cref="IConfigurationBuilder"/> object.</returns>
161+
public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder, FileConfiguration fileConfiguration,
162+
string primaryConfigFile = null, bool? optional = null, bool? reloadOnChange = null) // optional injections
163+
{
164+
var json = JsonSerializer.Serialize(fileConfiguration, JsonSerializerOptionsFactory.WebWriteIndented);
165+
return AddOcelotJsonFile(builder, json, primaryConfigFile, optional, reloadOnChange);
166+
}
168167

169168
/// <summary>
170169
/// Adds Ocelot configuration by ready configuration object, environment and merge option, reading the required files from the current default folder.

src/Ocelot/DependencyInjection/OcelotBuilder.cs

+43-43
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public OcelotBuilder(IServiceCollection services, IConfiguration configurationRo
111111
Services.TryAddSingleton<IHttpHandlerOptionsCreator, HttpHandlerOptionsCreator>();
112112
Services.TryAddSingleton<IDownstreamAddressesCreator, DownstreamAddressesCreator>();
113113
Services.TryAddSingleton<IDelegatingHandlerHandlerFactory, DelegatingHandlerHandlerFactory>();
114-
114+
115115
Services.TryAddSingleton<IOcelotConfigurationChangeTokenSource, OcelotConfigurationChangeTokenSource>();
116116
Services.TryAddSingleton<IOptionsMonitor<IInternalConfiguration>, OcelotConfigurationMonitor>();
117117

@@ -152,48 +152,48 @@ public OcelotBuilder(IServiceCollection services, IConfiguration configurationRo
152152
.Invoke(Services.AddMvcCore(), assembly);
153153
}
154154

155-
/// <summary>
156-
/// Adds default ASP.NET services which are the minimal part of the gateway core.
157-
/// <para>
158-
/// To remove these services, use custom builder in the <see cref="ServiceCollectionExtensions.AddOcelotUsingBuilder(IServiceCollection, Func{IMvcCoreBuilder, Assembly, IMvcCoreBuilder})"/> extension-method.
159-
/// </para>
160-
/// </summary>
161-
/// <remarks>
162-
/// Note that the following <see cref="IServiceCollection"/> extensions being called:<br/>
163-
/// - <see cref="MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection)"/>, impossible to remove.<br/>
164-
/// - <see cref="LoggingServiceCollectionExtensions.AddLogging(IServiceCollection)"/><br/>
165-
/// - <see cref="AnalysisServiceCollectionExtensions.AddMiddlewareAnalysis(IServiceCollection)"/><br/>
166-
/// - <see cref="EncoderServiceCollectionExtensions.AddWebEncoders(IServiceCollection)"/>.
167-
/// <para>
168-
/// Warning! The following <see cref="IMvcCoreBuilder"/> extensions being called<br/>
169-
/// - <see cref="MvcCoreMvcCoreBuilderExtensions.AddApplicationPart(IMvcCoreBuilder, Assembly)"/><br/>
170-
/// - <see cref="MvcCoreMvcCoreBuilderExtensions.AddControllersAsServices(IMvcCoreBuilder)"/><br/>
171-
/// - <see cref="MvcCoreMvcCoreBuilderExtensions.AddAuthorization(IMvcCoreBuilder)"/>.
172-
/// </para>
173-
/// </remarks>
174-
/// <param name="builder">The default builder being returned by <see cref="MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection)"/> extension-method.</param>
175-
/// <param name="assembly">The web app assembly.</param>
176-
/// <returns>An <see cref="IMvcCoreBuilder"/> object.</returns>
177-
protected IMvcCoreBuilder AddDefaultAspNetServices(IMvcCoreBuilder builder, Assembly assembly)
178-
{
179-
Services
180-
.AddLogging()
181-
.AddMiddlewareAnalysis()
182-
.AddWebEncoders();
183-
184-
return builder
185-
.AddApplicationPart(assembly)
186-
.AddControllersAsServices()
187-
.AddAuthorization()
188-
.AddJsonOptions(op =>
189-
{
190-
op.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
191-
op.JsonSerializerOptions.NumberHandling = JsonNumberHandling.AllowReadingFromString;
192-
op.JsonSerializerOptions.WriteIndented = false;
193-
op.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
194-
op.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
195-
});
196-
}
155+
/// <summary>
156+
/// Adds default ASP.NET services which are the minimal part of the gateway core.
157+
/// <para>
158+
/// To remove these services, use custom builder in the <see cref="ServiceCollectionExtensions.AddOcelotUsingBuilder(IServiceCollection, Func{IMvcCoreBuilder, Assembly, IMvcCoreBuilder})"/> extension-method.
159+
/// </para>
160+
/// </summary>
161+
/// <remarks>
162+
/// Note that the following <see cref="IServiceCollection"/> extensions being called:<br/>
163+
/// - <see cref="MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection)"/>, impossible to remove.<br/>
164+
/// - <see cref="LoggingServiceCollectionExtensions.AddLogging(IServiceCollection)"/><br/>
165+
/// - <see cref="AnalysisServiceCollectionExtensions.AddMiddlewareAnalysis(IServiceCollection)"/><br/>
166+
/// - <see cref="EncoderServiceCollectionExtensions.AddWebEncoders(IServiceCollection)"/>.
167+
/// <para>
168+
/// Warning! The following <see cref="IMvcCoreBuilder"/> extensions being called<br/>
169+
/// - <see cref="MvcCoreMvcCoreBuilderExtensions.AddApplicationPart(IMvcCoreBuilder, Assembly)"/><br/>
170+
/// - <see cref="MvcCoreMvcCoreBuilderExtensions.AddControllersAsServices(IMvcCoreBuilder)"/><br/>
171+
/// - <see cref="MvcCoreMvcCoreBuilderExtensions.AddAuthorization(IMvcCoreBuilder)"/>.
172+
/// </para>
173+
/// </remarks>
174+
/// <param name="builder">The default builder being returned by <see cref="MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection)"/> extension-method.</param>
175+
/// <param name="assembly">The web app assembly.</param>
176+
/// <returns>An <see cref="IMvcCoreBuilder"/> object.</returns>
177+
protected IMvcCoreBuilder AddDefaultAspNetServices(IMvcCoreBuilder builder, Assembly assembly)
178+
{
179+
Services
180+
.AddLogging()
181+
.AddMiddlewareAnalysis()
182+
.AddWebEncoders();
183+
184+
return builder
185+
.AddApplicationPart(assembly)
186+
.AddControllersAsServices()
187+
.AddAuthorization()
188+
.AddJsonOptions(op =>
189+
{
190+
op.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
191+
op.JsonSerializerOptions.NumberHandling = JsonNumberHandling.AllowReadingFromString;
192+
op.JsonSerializerOptions.WriteIndented = false;
193+
op.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
194+
op.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
195+
});
196+
}
197197

198198
public IOcelotBuilder AddSingletonDefinedAggregator<T>()
199199
where T : class, IDefinedAggregator

src/Ocelot/Ocelot.csproj

+5-4
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,17 @@
3737
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
3838
<PrivateAssets>all</PrivateAssets>
3939
</PackageReference>
40-
<PackageReference Include="System.Text.Json" Version="8.0.5" />
41-
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="8.0.0" />
40+
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="8.0.0" />
4241
</ItemGroup>
4342
<!-- Conditionally obtain references for the net 8.0 target -->
4443
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
45-
<PackageReference Include="Microsoft.AspNetCore.MiddlewareAnalysis" Version="8.0.11" />
44+
<PackageReference Include="Microsoft.AspNetCore.MiddlewareAnalysis" Version="8.0.11" />
45+
<PackageReference Include="System.Text.Json" Version="8.0.5" />
4646
</ItemGroup>
4747
<!-- Conditionally obtain references for the net 9.0 target -->
4848
<ItemGroup Condition=" '$(TargetFramework)' == 'net9.0' ">
49-
<PackageReference Include="Microsoft.AspNetCore.MiddlewareAnalysis" Version="9.0.0" />
49+
<PackageReference Include="Microsoft.AspNetCore.MiddlewareAnalysis" Version="9.0.0" />
50+
<PackageReference Include="System.Text.Json" Version="9.0.0" />
5051
</ItemGroup>
5152
<ItemGroup>
5253
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
+11-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
using System.Text.Json.Serialization;
22

3-
namespace Ocelot.AcceptanceTests
3+
namespace Ocelot.AcceptanceTests;
4+
5+
internal class BearerToken
46
{
5-
internal class BearerToken
6-
{
7-
[JsonPropertyName("access_token")]
8-
public string AccessToken { get; set; }
7+
[JsonPropertyName("access_token")]
8+
public string AccessToken { get; set; }
99

10-
[JsonPropertyName("expires_in")]
11-
public int ExpiresIn { get; set; }
10+
[JsonPropertyName("expires_in")]
11+
public int ExpiresIn { get; set; }
1212

13-
[JsonPropertyName("token_type")]
14-
public string TokenType { get; set; }
15-
}
16-
}
13+
[JsonPropertyName("token_type")]
14+
public string TokenType { get; set; }
15+
}
16+

test/Ocelot.AcceptanceTests/Caching/CachingTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ private void ThenTheCounterValueShouldBe(int expected)
299299
LastName = "Test",
300300
};
301301

302-
var testBody1String = JsonSerializer.Serialize(testBody1, JsonSerializerOptionsFactory.Web);
302+
var testBody1String = JsonSerializer.Serialize(testBody1, JsonSerializerOptionsFactory.Web);
303303

304304
var testBody2 = new TestBody
305305
{
@@ -309,7 +309,7 @@ private void ThenTheCounterValueShouldBe(int expected)
309309
LastName = "Test",
310310
};
311311

312-
var testBody2String = JsonSerializer.Serialize(testBody2, JsonSerializerOptionsFactory.Web);
312+
var testBody2String = JsonSerializer.Serialize(testBody2, JsonSerializerOptionsFactory.Web);
313313

314314
return (testBody1String, testBody2String);
315315
}

test/Ocelot.AcceptanceTests/ServiceDiscovery/EurekaServiceDiscoveryTests.cs

+29-29
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ private void GivenThereIsAFakeEurekaServiceDiscoveryProvider(string url, string
144144
},
145145
};
146146

147-
var json = JsonSerializer.Serialize(applications, JsonSerializerOptionsFactory.Web);
148-
context.Response.Headers.Append("Content-Type", "application/json");
149-
await context.Response.WriteAsync(json);
150-
}
151-
});
152-
}
147+
var json = JsonSerializer.Serialize(applications, JsonSerializerOptionsFactory.Web);
148+
context.Response.Headers.Append("Content-Type", "application/json");
149+
await context.Response.WriteAsync(json);
150+
}
151+
});
152+
}
153153

154154
private void GivenEurekaProductServiceOneIsRunning(string url)
155155
{
@@ -194,28 +194,28 @@ public FakeEurekaService(string serviceId, string host, int port, bool isSecure,
194194
public IDictionary<string, string> Metadata { get; }
195195
}
196196

197-
public class Port
198-
{
199-
[JsonPropertyName("$")]
200-
public int value { get; set; }
197+
public class Port
198+
{
199+
[JsonPropertyName("$")]
200+
public int value { get; set; }
201201

202-
[JsonPropertyName("@enabled")]
203-
public string enabled { get; set; }
204-
}
202+
[JsonPropertyName("@enabled")]
203+
public string enabled { get; set; }
204+
}
205205

206-
public class SecurePort
207-
{
208-
[JsonPropertyName("$")]
209-
public int value { get; set; }
206+
public class SecurePort
207+
{
208+
[JsonPropertyName("$")]
209+
public int value { get; set; }
210210

211-
[JsonPropertyName("@enabled")]
212-
public string enabled { get; set; }
213-
}
211+
[JsonPropertyName("@enabled")]
212+
public string enabled { get; set; }
213+
}
214214

215-
public class DataCenterInfo
216-
{
217-
[JsonPropertyName("@class")]
218-
public string value { get; set; }
215+
public class DataCenterInfo
216+
{
217+
[JsonPropertyName("@class")]
218+
public string value { get; set; }
219219

220220
public string name { get; set; }
221221
}
@@ -235,11 +235,11 @@ public class LeaseInfo
235235
public long serviceUpTimestamp { get; set; }
236236
}
237237

238-
public class ValueMetadata
239-
{
240-
[JsonPropertyName("@class")]
241-
public string value { get; set; }
242-
}
238+
public class ValueMetadata
239+
{
240+
[JsonPropertyName("@class")]
241+
public string value { get; set; }
242+
}
243243

244244
public class Instance
245245
{

test/Ocelot.Benchmarks/AllTheThingsBenchmarks.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public static void GivenThereIsAConfiguration(FileConfiguration fileConfiguratio
113113
{
114114
var configurationPath = Path.Combine(AppContext.BaseDirectory, "ocelot.json");
115115

116-
var jsonConfiguration = JsonSerializer.Serialize(fileConfiguration, JsonSerializerOptionsFactory.Web);
116+
var jsonConfiguration = JsonSerializer.Serialize(fileConfiguration, JsonSerializerOptionsFactory.Web);
117117

118118
if (File.Exists(configurationPath))
119119
{

0 commit comments

Comments
 (0)