Skip to content

Commit

Permalink
Be less strict when parsing testData.json (#17)
Browse files Browse the repository at this point in the history
No resason for us to choke on BOM, comments or trailing comma.
  • Loading branch information
ivarne authored Feb 27, 2023
1 parent 8a95fe3 commit 64cb7b7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/Helpers/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,17 @@ public static string AsFileName(this string input, bool throwExceptionOnInvalidC

return illegalFileNameCharacters.Aggregate(input, (current, c) => current.Replace(c, '-'));
}

private static readonly byte[] _utf8bom = new byte[] { 0xEF, 0xBB, 0xBF };
internal static ReadOnlySpan<byte> RemoveBom(this byte[] bytes)
{
// Remove UTF8 BOM (if present)
if (bytes.AsSpan().StartsWith(_utf8bom))
{
return bytes.AsSpan().Slice(_utf8bom.Length);
}

return bytes;
}
}
}
9 changes: 7 additions & 2 deletions src/Services/LocalApp/Implementation/LocalAppHttp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@
using LocalTest.Configuration;
using LocalTest.Services.LocalApp.Interface;
using LocalTest.Services.TestData;
using LocalTest.Helpers;

namespace LocalTest.Services.LocalApp.Implementation
{
public class LocalAppHttp : ILocalApp
{
public static readonly JsonSerializerOptions JSON_OPTIONS = new JsonSerializerOptions(JsonSerializerDefaults.Web)
{
Converters = { new System.Text.Json.Serialization.JsonStringEnumConverter() }
Converters = { new System.Text.Json.Serialization.JsonStringEnumConverter() },
AllowTrailingCommas = true,
ReadCommentHandling = JsonCommentHandling.Skip,
PropertyNameCaseInsensitive = false,
};
public const string XACML_CACHE_KEY = "/api/v1/meta/authorizationpolicy/";
public const string APPLICATION_METADATA_CACHE_KEY = "/api/v1/applicationmetadata?checkOrgApp=false";
Expand Down Expand Up @@ -139,7 +143,8 @@ public async Task<Dictionary<string, Application>> GetApplications()
}

response.EnsureSuccessStatusCode();
return JsonSerializer.Deserialize<AppTestDataModel>(await response.Content.ReadAsByteArrayAsync(), JSON_OPTIONS);
var data = await response.Content.ReadAsByteArrayAsync();
return JsonSerializer.Deserialize<AppTestDataModel>(data.RemoveBom(), JSON_OPTIONS);
}
catch (HttpRequestException e)
{
Expand Down

0 comments on commit 64cb7b7

Please sign in to comment.