Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Text.Encodings.Web;
using System.Text.Json.Nodes;
using System.Text.Json.Schema;
using System.Text.RegularExpressions;

namespace System.Text.Json.Serialization.Converters
{
Expand Down Expand Up @@ -495,8 +496,30 @@ private bool IsDefinedValueOrCombinationOfValues(ulong key)

if (s_isFlagsEnum)
{
// Do not report enum values in case of flags.
return new() { Type = JsonSchemaType.String };
string? pattern = null;

if (_enumFieldInfo.Length > 0)
{
using ValueStringBuilder sb = new(stackalloc char[JsonConstants.StackallocCharThreshold]);
sb.Append("^(");
sb.Append(Regex.Escape(_enumFieldInfo[0].JsonName));
for (int i = 1; i < _enumFieldInfo.Length; i++)
{
sb.Append('|');
sb.Append(Regex.Escape(_enumFieldInfo[i].JsonName));
}
sb.Append(")(\\w*,\\w*(");
sb.Append(Regex.Escape(_enumFieldInfo[0].JsonName));
for (int i = 1; i < _enumFieldInfo.Length; i++)
{
sb.Append('|');
sb.Append(Regex.Escape(_enumFieldInfo[i].JsonName));
}
sb.Append("))*$");
pattern = sb.ToString();
}

return new() { Type = JsonSchemaType.String, Pattern = pattern };
}

JsonArray enumValues = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ public static IEnumerable<ITestData> GetTestDataCore()
// Enum types
yield return new TestData<IntEnum>(IntEnum.A, ExpectedJsonSchema: """{"type":"integer"}""");
yield return new TestData<StringEnum>(StringEnum.A, ExpectedJsonSchema: """{"enum":["A","B","C"]}""");
yield return new TestData<FlagsStringEnum>(FlagsStringEnum.A, ExpectedJsonSchema: """{"type":"string"}""");
yield return new TestData<FlagsStringEnum>(FlagsStringEnum.A, ExpectedJsonSchema: """{"type":"string","pattern":"^(A|B|C)(\\w*,\\w*(A|B|C))*$"}""");
yield return new TestData<FlagsEnumWithNameAttributes>(
FlagsEnumWithNameAttributes.NormalFlag,
ExpectedJsonSchema: """{"type":"string","pattern":"^(A|\"\\.\\\\)(\\w*,\\w*(A|\"\\.\\\\))*$"}""");
yield return new TestData<EnumWithNameAttributes>(
EnumWithNameAttributes.Value1,
AdditionalValues: [EnumWithNameAttributes.Value2],
Expand Down Expand Up @@ -1297,6 +1300,15 @@ public enum StringEnum { A, B, C };
[Flags, JsonConverter(typeof(JsonStringEnumConverter<FlagsStringEnum>))]
public enum FlagsStringEnum { A = 1, B = 2, C = 4 };

[Flags, JsonConverter(typeof(JsonStringEnumConverter<FlagsEnumWithNameAttributes>))]
public enum FlagsEnumWithNameAttributes
{
[JsonStringEnumMemberName("A")]
NormalFlag = 1,
[JsonStringEnumMemberName("\".\\")]
StrangeFlag = 2,
}

[JsonConverter(typeof(JsonStringEnumConverter<EnumWithNameAttributes>))]
public enum EnumWithNameAttributes
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public sealed partial class JsonSchemaExporterTests_SourceGen()
[JsonSerializable(typeof(IntEnum))]
[JsonSerializable(typeof(StringEnum))]
[JsonSerializable(typeof(FlagsStringEnum))]
[JsonSerializable(typeof(FlagsEnumWithNameAttributes))]
[JsonSerializable(typeof(EnumWithNameAttributes))]
// Nullable<T> types
[JsonSerializable(typeof(bool?))]
Expand Down
Loading