Fix: fix type conversion of strings for all models in .NET SDK#670
Fix: fix type conversion of strings for all models in .NET SDK#670lohanidamodar merged 4 commits intomasterfrom
Conversation
| public static {{ definition.name | caseUcfirst | overrideIdentifier}} From(Dictionary<string, object> map) => new {{ definition.name | caseUcfirst | overrideIdentifier}}( | ||
| {%~ for property in definition.properties %} | ||
| {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}: {% if property.sub_schema %}{% if property.type == 'array' %}((JArray)map["{{ property.name }}"]).ToObject<List<Dictionary<string, object>>>().Select(it => {{property.sub_schema | caseUcfirst | overrideIdentifier}}.From(map: it)).ToList(){% else %}{{property.sub_schema | caseUcfirst | overrideIdentifier}}.From(map: ((JObject)map["{{ property.name }}"]).ToObject<Dictionary<string, object>>()!){% endif %}{% else %}{% if property.type == 'array' %}((JArray)map["{{ property.name }}"]).ToObject<{{ property | typeName }}>(){% else %}{% if property.type == "integer" or property.type == "number" %}{% if not property.required %}map["{{ property.name }}"] == null ? null : {% endif %}Convert.To{% if property.type == "integer" %}Int64{% else %}Double{% endif %}(map["{{ property.name }}"]){% else %}({{ property | typeName }}{% if not property.required %}?{% endif %})map["{{ property.name }}"]{% endif %}{% endif %}{% endif %}{% if not loop.last or (loop.last and definition.additionalProperties) %},{% endif %} | ||
| {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}: {% if property.sub_schema %}{% if property.type == 'array' %}((JArray)map["{{ property.name }}"]).ToObject<List<Dictionary<string, object>>>().Select(it => {{property.sub_schema | caseUcfirst | overrideIdentifier}}.From(map: it)).ToList(){% else %}{{property.sub_schema | caseUcfirst | overrideIdentifier}}.From(map: ((JObject)map["{{ property.name }}"]).ToObject<Dictionary<string, object>>()!){% endif %}{% else %}{% if property.type == 'array' %}((JArray)map["{{ property.name }}"]).ToObject<{{ property | typeName }}>(){% else %}{% if property.type == "integer" or property.type == "number" %}{% if not property.required %}map["{{ property.name }}"] == null ? null : {% endif %}Convert.To{% if property.type == "integer" %}Int64{% else %}Double{% endif %}(map["{{ property.name }}"]){% else %}{% if property.type == "bool" %}({{ property | typeName }}{% if not property.required %}?{% endif %})map["{{ property.name }}"]{% endif %}map["{{ property.name }}"].ToString(){% endif %}{% endif %}{% endif %}{% if not loop.last or (loop.last and definition.additionalProperties) %},{% endif %} |
There was a problem hiding this comment.
map["{{ property.name }}"].ToString()
Let's add a check for if the property is required and use a null-conditional to call ToString()
There was a problem hiding this comment.
We can make the code a bit more compact if the output is map["{{ property.name }}"]?.ToString() instead of map["{{ property.name }}"] == null ? null : map["{{ property.name }}"].ToString() since they are technically equivalent
There was a problem hiding this comment.
Makes sense
I was trying to retain the same styling as earlier (for numbers)
But that's a fair point 💯
There was a problem hiding this comment.
Let's make it so the ? is only present if the parameter is not required. If the parameter is required, we should be able to safely call ToString() without as it will always be non-null (if it is null, it indicates an API issue). So we should have:
map["{{ property.name }}"].ToString()for required parametersmap["{{ property.name }}"]?.ToString()for optional parameters
What does this PR do?
Corrects type conversion of
stringtype by shifting from(string)type cast to using.ToString()method in theFrom()function in all the ModelsExample (from the
Documentmodel):Test Plan
composerphp example.phpin the repoFrom()function in all Models in the generated .NET SDKRelated PRs and Issues
#320
Have you read the Contributing Guidelines on issues?
Yes