When designing objects in C# you use pascal casing for your properties, but in other languages you don’t, and example (other than java) is TypeScript here’s an article from Microsoft about it.
And that’s cool, a lot of the languages have different standards and depending on which one you are in, you write a little different.
The problem is when you try to work on a standard that defines cross platform communication that is case sensitive, an example being Swagger using REST and JSON.
So the issue we had today was a WebAPI project was generating objects like this:
{ "ObjectId": 203 "ObjectName" : "My Object Name" }
When swaggerated the object comes out correctly with the correct pascal casing, however when using swagger codegen the object is converted to camel case (TypeScript Below)
export interface MyObject { objectId: number; objectName: string; }
The final output is a generated client library that can’t read any objects from the API because JavaScript is case sensitive.
After some investigation we found that when the swagger outputs camel casing the C# client generators (Autorest and Swagger codegen) will output C# code that is in camel casing but with properties to do the translating from camel to pascal, like the below example
/// <summary> /// Gets or Sets TimeZoneName /// </summary> [JsonProperty(PropertyName = "timeZoneName")] public string TimeZoneName { get; set; }
So to avoid pushing shit up hill we decided to roll down it. I found this excellent article on creating a filter for WebAPI to convert all your pascal cased objects to camel case on the fly
So we found that the best practice is:
- Write Web API C# in Pascal casing
- Covert using an action filter from pascal to camel case Json objects
- Creating the client with TyepScript (or other camel language) default option will then work
- Creating the C# client will add the JsonProperty to translate from camel to pascal and resulting C# client will be pascal cased
I raised a GitHub Issue here with a link into the source code that I found in swagger codegen, however later realized that changing the way we do things will mitigate long term pain.
Url http://fizzylogic.nl/2014/07/30/changing-the-casing-of-json-properties-in-asp-dot-net-web-api/
is not working
LikeLike