-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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
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(); | ||
}); |
There was a problem hiding this comment.
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.
JsonSerializerSettings settings = new JsonSerializerSettings() | ||
{ | ||
ContractResolver = new CamelCasePropertyNamesContractResolver() | ||
}; | ||
|
||
var schema = Subject(null, (s) => | ||
{ | ||
s.ContractResolver = new CamelCasePropertyNamesContractResolver(); | ||
}).GenerateSchema(typeof(IDictionary<IntEnum, int>), new SchemaRepository()); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); | ||
} |
There was a problem hiding this comment.
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)
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.