-
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?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
{ | ||
|
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.