Skip to content
Merged
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 @@ -57,10 +57,24 @@ public DataContract GetDataContractForType(Type type)

if (IsSupportedDictionary(type, out Type keyType, out Type valueType))
{
IEnumerable<string> keys = null;

if (keyType.IsEnum)
{
// This is a special case where we know the possible key values
var enumValuesAsJson = keyType.GetEnumValues()
.Cast<object>()
.Select(value => JsonConverterFunc(value));

keys = enumValuesAsJson.Any(json => json.StartsWith("\""))
? enumValuesAsJson.Select(json => json.Replace("\"", string.Empty))
: keyType.GetEnumNames();
}

return DataContract.ForDictionary(
underlyingType: type,
valueType: valueType,
keys: null, // STJ doesn't currently support dictionaries with enum key types
keys: keys,
jsonConverter: JsonConverterFunc);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,17 @@ public void GenerateSchema_HonorsSerializerOption_StringEnumConverter(
Assert.Equal(expectedDefaultAsJson, propertySchema.Default.ToJson());
}

[Fact]
public void GenerateSchema_HonorsEnumDictionaryKeys_StringEnumConverter()
{
var subject = Subject();
var schemaRepository = new SchemaRepository();

var referenceSchema = subject.GenerateSchema(typeof(Dictionary<IntEnum, string>), schemaRepository);

Assert.Equal(typeof(IntEnum).GetEnumNames(), referenceSchema.Properties.Keys);
}

[Fact]
public void GenerateSchema_HonorsSerializerAttribute_StringEnumConverter()
{
Expand Down