VoidPointer
VoidPointer
CC#
Created by VoidPointer on 11/8/2024 in #help
✅ How to specify camel case for all response properties with Newtonsoft
This is the type of JSON I receive from an external API:
"phoneValidation": {
"success": true,
"data": {
"number": "27101238765",
"valid": true,
"account_details": {
"google": {
"registered": false,
"full_name": null
},
"twitter": {
"registered": true
},
"skype": {
"registered": false,
"age": null,
"city": null,
},
"whatsapp": {
"registered": true,
"last_active": null
},
"telegram": {
"registered": null,
"photo": null,
},
"phoneValidation": {
"success": true,
"data": {
"number": "27101238765",
"valid": true,
"account_details": {
"google": {
"registered": false,
"full_name": null
},
"twitter": {
"registered": true
},
"skype": {
"registered": false,
"age": null,
"city": null,
},
"whatsapp": {
"registered": true,
"last_active": null
},
"telegram": {
"registered": null,
"photo": null,
},
This I deserialize into this DTO:
public class ValidatePhoneNumberReceived
{
public bool Success { get; set; }
public ValidatePhoneNumberData Data { get; set; }

public class ValidatePhoneNumberData
{
public string Number { get; set; }
public Dictionary<string, object> AccountDetails { get; set; }
}
}
public class ValidatePhoneNumberReceived
{
public bool Success { get; set; }
public ValidatePhoneNumberData Data { get; set; }

public class ValidatePhoneNumberData
{
public string Number { get; set; }
public Dictionary<string, object> AccountDetails { get; set; }
}
}
Then I do some stuff, and return the same model from my own API. AccountDetails contains object, because for each key, the external API returns a different set of properties, as we see in the json account_details.google, and account_details.whatsapp etc. They appear in my response with the same snake case I received them with. I have even tried adding an action filter to add a NewtonsoftJsonOutputFormatter set to camel case. How can I force the dynamic properties to be camel case?
11 replies
CC#
Created by VoidPointer on 7/13/2024 in #help
Stuck between using a MetadataLoadContext for reflection and using an assembly reference for testing
Following some advice here, and because it seems sensible, I switched to using a MetadataLoadContext to reflect types from a 3rd party compiled assembly, but now when it comes to testing a method that returns a Type, the most reliable way I have found so far of getting a reliable *expected * Type is to reference the assembly in my test project and use typeof. This means my expected Type comes from an assembly located in the build output, while my actual Type comes from the original location of the assembly of interest. So an assert.Equal on the two types fails. Right now I'm getting by with an assert.Equal on the type names, but that's flimsy.
1 replies