-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from configcat/fix-global-json-settings
Ensure internal serializer doesn't use the global json serializer settings
- Loading branch information
Showing
7 changed files
with
104 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Converters; | ||
using System; | ||
using System.IO; | ||
|
||
namespace ConfigCat.Client.Tests | ||
{ | ||
[TestClass] | ||
public class DeserializerTests | ||
{ | ||
[TestMethod] | ||
public void Ensure_Global_Settings_Doesnt_Interfere() | ||
{ | ||
JsonConvert.DefaultSettings = () => | ||
{ | ||
var settings = new JsonSerializerSettings(); | ||
settings.Converters.Add(new StringEnumConverter { AllowIntegerValues = false }); | ||
return settings; | ||
}; | ||
|
||
var deserializer = new ConfigDeserializer(new LoggerWrapper(new ConsoleLogger(LogLevel.Debug)), JsonSerializer.Create()); | ||
|
||
deserializer.TryDeserialize(new ProjectConfig("{\"p\": {\"u\": \"http://example.com\", \"r\": 0}}", DateTime.Now, ""), out var configs); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,41 @@ | ||
using ConfigCat.Client.Evaluate; | ||
using Newtonsoft.Json; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
|
||
namespace ConfigCat.Client | ||
{ | ||
internal class ConfigDeserializer : IConfigDeserializer | ||
{ | ||
private readonly ILogger logger; | ||
private readonly JsonSerializer serializer; | ||
|
||
public ConfigDeserializer(ILogger logger) | ||
public ConfigDeserializer(ILogger logger, JsonSerializer serializer) | ||
{ | ||
this.logger = logger; | ||
this.serializer = serializer; | ||
} | ||
|
||
public bool TryDeserialize(ProjectConfig projectConfig, out IDictionary<string, Setting> settings) | ||
{ | ||
if (projectConfig.JsonString == null) | ||
{ | ||
this.logger.Warning("ConfigJson is not present."); | ||
|
||
settings = null; | ||
|
||
return false; | ||
} | ||
|
||
var settingsWithPreferences = JsonConvert.DeserializeObject<SettingsWithPreferences>(projectConfig.JsonString); | ||
using (var stringReader = new StringReader(projectConfig.JsonString)) | ||
using (var reader = new JsonTextReader(stringReader)) | ||
{ | ||
var settingsWithPreferences = this.serializer.Deserialize<SettingsWithPreferences>(reader); | ||
|
||
settings = settingsWithPreferences.Settings; | ||
settings = settingsWithPreferences.Settings; | ||
|
||
return true; | ||
return true; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using System; | ||
using System.IO; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
|
@@ -17,14 +18,14 @@ internal sealed class HttpConfigFetcher : IConfigFetcher, IDisposable | |
private readonly ILogger log; | ||
|
||
private readonly HttpClientHandler httpClientHandler; | ||
|
||
private readonly JsonSerializer serializer; | ||
private readonly bool isCustomUri; | ||
|
||
private HttpClient httpClient; | ||
|
||
private Uri requestUri; | ||
|
||
public HttpConfigFetcher(Uri requestUri, string productVersion, ILogger logger, HttpClientHandler httpClientHandler, bool isCustomUri) | ||
public HttpConfigFetcher(Uri requestUri, string productVersion, ILogger logger, HttpClientHandler httpClientHandler, JsonSerializer serializer, bool isCustomUri) | ||
{ | ||
this.requestUri = requestUri; | ||
|
||
|
@@ -33,7 +34,7 @@ public HttpConfigFetcher(Uri requestUri, string productVersion, ILogger logger, | |
this.log = logger; | ||
|
||
this.httpClientHandler = httpClientHandler; | ||
|
||
this.serializer = serializer; | ||
this.isCustomUri = isCustomUri; | ||
|
||
ReInitializeHttpClient(); | ||
|
@@ -98,53 +99,57 @@ private async Task<Tuple<HttpResponseMessage, string>> FetchRequest(ProjectConfi | |
{ | ||
var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false); | ||
|
||
var body = JsonConvert.DeserializeObject<SettingsWithPreferences>(responseBody); | ||
|
||
if (body?.Preferences != null) | ||
using (var stringReader = new StringReader(responseBody)) | ||
using (var reader = new JsonTextReader(stringReader)) | ||
{ | ||
var newBaseUrl = body.Preferences.Url; | ||
|
||
if (newBaseUrl == null || requestUri.Host == new Uri(newBaseUrl).Host) | ||
{ | ||
return Tuple.Create(response, responseBody); | ||
} | ||
|
||
Evaluate.RedirectMode redirect = body.Preferences.RedirectMode; | ||
|
||
if (isCustomUri && redirect != RedirectMode.Force) | ||
{ | ||
return Tuple.Create(response, responseBody); | ||
} | ||
|
||
UpdateRequestUri(new Uri(newBaseUrl)); | ||
var body = this.serializer.Deserialize<SettingsWithPreferences>(reader); | ||
|
||
if (redirect == RedirectMode.No) | ||
if (body?.Preferences != null) | ||
{ | ||
return Tuple.Create(response, responseBody); | ||
var newBaseUrl = body.Preferences.Url; | ||
|
||
if (newBaseUrl == null || requestUri.Host == new Uri(newBaseUrl).Host) | ||
{ | ||
return Tuple.Create(response, responseBody); | ||
} | ||
|
||
Evaluate.RedirectMode redirect = body.Preferences.RedirectMode; | ||
|
||
if (isCustomUri && redirect != RedirectMode.Force) | ||
{ | ||
return Tuple.Create(response, responseBody); | ||
} | ||
|
||
UpdateRequestUri(new Uri(newBaseUrl)); | ||
|
||
if (redirect == RedirectMode.No) | ||
{ | ||
return Tuple.Create(response, responseBody); | ||
} | ||
|
||
if (redirect == RedirectMode.Should) | ||
{ | ||
this.log.Warning("Your dataGovernance parameter at ConfigCatClient initialization is not in sync " + | ||
"with your preferences on the ConfigCat Dashboard: " + | ||
"https://app.configcat.com/organization/data-governance. " + | ||
"Only Organization Admins can access this preference."); | ||
} | ||
|
||
if (maxExecutionCount <= 1) | ||
{ | ||
log.Error("Redirect loop during config.json fetch. Please contact [email protected]."); | ||
return Tuple.Create(response, responseBody); | ||
} | ||
|
||
return await this.FetchRequest( | ||
lastConfig, | ||
ReplaceUri(request.RequestUri, new Uri(newBaseUrl)), | ||
--maxExecutionCount); | ||
} | ||
|
||
if (redirect == RedirectMode.Should) | ||
else | ||
{ | ||
this.log.Warning("Your dataGovernance parameter at ConfigCatClient initialization is not in sync " + | ||
"with your preferences on the ConfigCat Dashboard: " + | ||
"https://app.configcat.com/organization/data-governance. " + | ||
"Only Organization Admins can access this preference."); | ||
} | ||
|
||
if (maxExecutionCount <= 1) | ||
{ | ||
log.Error("Redirect loop during config.json fetch. Please contact [email protected]."); | ||
return Tuple.Create(response, responseBody); | ||
} | ||
|
||
return await this.FetchRequest( | ||
lastConfig, | ||
ReplaceUri(request.RequestUri, new Uri(newBaseUrl)), | ||
--maxExecutionCount); | ||
} | ||
else | ||
{ | ||
return Tuple.Create(response, responseBody); | ||
} | ||
} | ||
|
||
|