Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix casing of enum keys in a dictionary with CamelCasePropertyNamesContractResolver #3039

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ public DataContract GetDataContractForType(Type type)
if (keyType.IsEnum)
{
// This is a special case where we know the possible key values
var enumValuesAsJson = keyType.GetEnumValues()
.Cast<object>()
.Select(JsonConverterFunc);

keys = enumValuesAsJson.Any(json => json.StartsWith("\""))
? enumValuesAsJson.Select(json => json.Replace("\"", string.Empty))
: keyType.GetEnumNames();
keys = keyType.GetEnumValues().Cast<object>().Select(v =>
{
var dictionary = new Dictionary<object, byte>(1) { [v] = 0 };
var serialized = JsonConverterFunc(dictionary);
var deserialized = JsonConvert.DeserializeObject<Dictionary<string, byte>>(serialized, _serializerSettings);
return deserialized.Keys.First();
});
}

return DataContract.ForDictionary(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ public DataContract GetDataContractForType(Type type)
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, keyType));

keys = enumValuesAsJson.Any(json => json.StartsWith("\""))
? enumValuesAsJson.Select(json => json.Replace("\"", string.Empty))
: keyType.GetEnumNames();
keys = keyType.GetEnumValues().Cast<object>().Select(v =>
{
var dictionary = new Dictionary<object, byte>(1) { [v] = 0 };
var serialized = JsonConverterFunc(dictionary, dictionary.GetType());
var deserialized = JsonSerializer.Deserialize<Dictionary<string, byte>>(serialized, _serializerOptions);
return deserialized.Keys.Single();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return deserialized.Keys.Single();
return deserialized.Keys.First();

});
}

return DataContract.ForDictionary(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.NewtonsoftJson;
using Microsoft.OpenApi.Models;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -142,6 +142,18 @@ public void GenerateSchema_GeneratesObjectSchema_IfDictionaryTypeHasEnumKey()
Assert.Equal(new[] { "Value2", "Value4", "Value8" }, schema.Properties.Keys);
}

[Fact]
public void GenerateSchema_GeneratesObjectSchema_IfDictionaryTypeHasEnumKey_CamelCasePropertyNamesContractResolver()
{
var schema = Subject(null, (s) =>
{
s.ContractResolver = new CamelCasePropertyNamesContractResolver();
}).GenerateSchema(typeof(IDictionary<IntEnum, int>), new SchemaRepository());

Assert.Equal("object", schema.Type);
Assert.Equal(new[] { "value2", "value4", "value8" }, schema.Properties.Keys);
martincostello marked this conversation as resolved.
Show resolved Hide resolved
}

[Fact]
public void GenerateSchema_GeneratesReferencedDictionarySchema_IfDictionaryTypeIsSelfReferencing()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,17 @@ public void GenerateSchema_HonorsEnumDictionaryKeys_StringEnumConverter()
Assert.Equal(typeof(IntEnum).GetEnumNames(), referenceSchema.Properties.Keys);
}

[Fact]
public void GenerateSchema_HonorsEnumDictionaryKeys_StringEnumConverter_CamelCase()
{
var subject = Subject(null, o => o.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase);
var schemaRepository = new SchemaRepository();

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

Assert.Equal(typeof(IntEnum).GetEnumNames().Select(n => n.ToCamelCase()), referenceSchema.Properties.Keys);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer the expected values here are hard-coded, rather than using logic to compute them.

}

[Fact]
public void GenerateSchema_HonorsSerializerAttribute_StringEnumConverter()
{
Expand Down