Skip to content

Commit 505df5c

Browse files
TurnerjRehanSaeed
authored andcommitted
Additional checks around enum parsing
This includes additional tests for EnumHelper.
1 parent 4a973d5 commit 505df5c

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

Source/Common/EnumHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Schema.NET;
77
/// <summary>
88
/// Helper for parsing strings into Enum values.
99
/// </summary>
10-
internal static class EnumHelper
10+
public static class EnumHelper
1111
{
1212
/// <summary>
1313
/// Converts the string representation of the name or numeric value of one or more
@@ -61,15 +61,15 @@ public static bool TryParseEnumFromSchemaUri(
6161
out object? result)
6262
{
6363
string? enumString;
64-
if (value is not null && value.StartsWith(Constants.HttpSchemaOrgUrl, StringComparison.OrdinalIgnoreCase))
64+
if (value is not null && value.Length > Constants.HttpSchemaOrgUrl.Length && value.StartsWith(Constants.HttpSchemaOrgUrl, StringComparison.OrdinalIgnoreCase))
6565
{
6666
#if NETCOREAPP3_0_OR_GREATER
6767
enumString = value[(Constants.HttpSchemaOrgUrl.Length + 1)..];
6868
#else
6969
enumString = value.Substring(Constants.HttpSchemaOrgUrl.Length + 1);
7070
#endif
7171
}
72-
else if (value is not null && value.StartsWith(Constants.HttpsSchemaOrgUrl, StringComparison.OrdinalIgnoreCase))
72+
else if (value is not null && value.Length > Constants.HttpsSchemaOrgUrl.Length && value.StartsWith(Constants.HttpsSchemaOrgUrl, StringComparison.OrdinalIgnoreCase))
7373
{
7474
#if NETCOREAPP3_0_OR_GREATER
7575
enumString = value[(Constants.HttpsSchemaOrgUrl.Length + 1)..];
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
namespace Schema.NET.Test;
2+
3+
using Xunit;
4+
5+
public class EnumHelperTest
6+
{
7+
private enum TestEnum
8+
{
9+
DefaultValue,
10+
RealValue,
11+
}
12+
13+
[Theory]
14+
[InlineData("RealValue")]
15+
[InlineData("http://schema.org/RealValue")]
16+
[InlineData("https://schema.org/RealValue")]
17+
[InlineData("HTTPS://SCHEMA.ORG/RealValue")]
18+
public void TryParseEnumFromSchemaUri_AllowedValues(string inputValue)
19+
{
20+
var result = EnumHelper.TryParseEnumFromSchemaUri(
21+
typeof(TestEnum),
22+
inputValue,
23+
out var parsedValue);
24+
25+
Assert.True(result);
26+
Assert.Equal(TestEnum.RealValue, parsedValue);
27+
}
28+
29+
[Theory]
30+
[InlineData("REALVALUE")]
31+
[InlineData("http://schema.org/REALVALUE")]
32+
[InlineData("http://schema.org")]
33+
[InlineData("https://schema.org")]
34+
[InlineData("http://schema.org/")]
35+
[InlineData("https://schema.org/")]
36+
[InlineData("NonValue")]
37+
[InlineData("http://example.org/RealValue")]
38+
public void TryParseEnumFromSchemaUri_InvalidValues(string inputValue)
39+
{
40+
var result = EnumHelper.TryParseEnumFromSchemaUri(
41+
typeof(TestEnum),
42+
inputValue,
43+
out _);
44+
45+
Assert.False(result);
46+
}
47+
}

0 commit comments

Comments
 (0)