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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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 @@ -90,6 +90,15 @@ public DataContract GetDataContractForType(Type type)
keys = enumValuesAsJson.Any(json => json.StartsWith("\""))
? enumValuesAsJson.Select(json => json.Replace("\"", string.Empty))
: keyType.GetEnumNames();

keys = keyType.GetEnumValues().Cast<object>().Select(v =>
{
var dic = new Dictionary<object, int>() { { v, 0 } };
var serialized = JsonConvert.SerializeObject(dic, _serializerSettings);
var deserialized =
JsonConvert.DeserializeObject<Dictionary<string, int>>(serialized, _serializerSettings);
return deserialized.Keys.Single();
});
Comment on lines +86 to +93
Copy link
Author

@j2ghz j2ghz Aug 23, 2024

Choose a reason for hiding this comment

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

This completely replaces the old code, and (at least locally) passes all tests. I'm not a huge fan of actually doing a serialization roundtrip, but it's the only way I came up with to reliably follow all rules and transformations of Newtonsoft.Json. Ideas welcome.

}

return DataContract.ForDictionary(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,33 @@ public void GenerateSchema_GeneratesObjectSchema_IfDictionaryTypeHasEnumKey()
Assert.Equal(new[] { "Value2", "Value4", "Value8" }, schema.Properties.Keys);
}

[Fact]
public void GenerateSchema_GeneratesObjectSchema_IfDictionaryTypeHasEnumKey_CamelCasePropertyNamesContractResolver()
{
JsonSerializerSettings settings = new JsonSerializerSettings()
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};

var schema = Subject(null, (s) =>
{
s.ContractResolver = new CamelCasePropertyNamesContractResolver();
}).GenerateSchema(typeof(IDictionary<IntEnum, int>), new SchemaRepository());
Comment on lines +148 to +156
Copy link
Author

Choose a reason for hiding this comment

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

Added CamelCasePropertyNamesContractResolver to a copy of an existing test (GenerateSchema_GeneratesObjectSchema_IfDictionaryTypeHasEnumKey)


Assert.Equal("object", schema.Type);
Assert.Equal(new[] { "value2", "value4", "value8" }, schema.Properties.Keys);
Copy link
Author

Choose a reason for hiding this comment

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

Which makes these lowercase


var a = new NewtonsoftDataContractResolver(settings);
var b = a.GetDataContractForType(typeof(IDictionary<IntEnum, int>)).JsonConverter(new Dictionary<IntEnum, int>()
{
{ IntEnum.Value2, 2 }, { IntEnum.Value4, 4 }, { IntEnum.Value8, 8 },
});
foreach (var key in schema.Properties.Keys)
{
Assert.Contains(key, b);
}
Comment on lines +161 to +169
Copy link
Author

@j2ghz j2ghz Aug 23, 2024

Choose a reason for hiding this comment

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

This feels out of place here, so I'd welcome pointers on where such a test should live. I was hoping to find a place in IntegrationTests, but I didn't see an obvious one. It also only checks that the key is present somewhere in the serialized string, but maybe that's good enough. (also will fix variable naming)

}

[Fact]
public void GenerateSchema_GeneratesReferencedDictionarySchema_IfDictionaryTypeIsSelfReferencing()
{
Expand Down