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

Conversation

j2ghz
Copy link

@j2ghz j2ghz commented Aug 23, 2024

Pull Request

The issue or feature being addressed

Instead of opening an issue I made a PR with a failing test and a possible fix

When using CamelCasePropertyNamesContractResolver, the serializer will use enum keys in camelCase, but the schema will have PascalCase.

Details on the issue fix or feature implementation

Added a test to check that the keys the schema generator produces appear in the output produced by the same serializer. Added a fix that runs the keys through the actual serializer instead of assuming they will be serialized as-is.

Copy link
Author

@j2ghz j2ghz left a comment

Choose a reason for hiding this comment

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

Implementation notes and TODOs

Comment on lines +94 to +101
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();
});
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.

Comment on lines +148 to +156
JsonSerializerSettings settings = new JsonSerializerSettings()
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};

var schema = Subject(null, (s) =>
{
s.ContractResolver = new CamelCasePropertyNamesContractResolver();
}).GenerateSchema(typeof(IDictionary<IntEnum, int>), new SchemaRepository());
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)

}).GenerateSchema(typeof(IDictionary<IntEnum, int>), new SchemaRepository());

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

Comment on lines +161 to +169
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);
}
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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant