Skip to content

Commit 99b4ba4

Browse files
committed
Improve C# type generation for required attributes
Update C# type generation template to throw ArgumentNullException for missing required int, double, and boolean attributes instead of using default values. Also, corrects the type check from 'float' to 'double' for consistency.
1 parent 89882a2 commit 99b4ba4

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

templates/cli/lib/type-generation/languages/csharp.js.twig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,21 @@ public class <%= toPascalCase(collection.name) %>
120120
if (attribute.array) {
121121
-%> (map["<%- attribute.key %>"] as IEnumerable<object>)<%- !attribute.required ? '?' : '' %>.Select(e => Convert.ToInt32(e)).ToList()<%
122122
} else {
123-
-%> map["<%- attribute.key %>"] != null ? Convert.ToInt32(map["<%- attribute.key %>"]) : (<%- attribute.required ? '0' : 'null' %>)<%
123+
-%> map["<%- attribute.key %>"] != null ? Convert.ToInt32(map["<%- attribute.key %>"]) : <% if (attribute.required) { %>throw new ArgumentNullException("<%- attribute.key %>", "Required attribute '<%- attribute.key %>' was null.")<% } else { %>null<% } %><%
124124
}
125125
// FLOAT
126-
} else if (attribute.type === 'float') {
126+
} else if (attribute.type === 'double') {
127127
if (attribute.array) {
128128
-%> (map["<%- attribute.key %>"] as IEnumerable<object>)<%- !attribute.required ? '?' : '' %>.Select(e => Convert.ToDouble(e)).ToList()<%
129129
} else {
130-
-%> map["<%- attribute.key %>"] != null ? Convert.ToDouble(map["<%- attribute.key %>"]) : (<%- attribute.required ? '0.0' : 'null' %>)<%
130+
-%> map["<%- attribute.key %>"] != null ? Convert.ToDouble(map["<%- attribute.key %>"]) : <% if (attribute.required) { %>throw new ArgumentNullException("<%- attribute.key %>", "Required attribute '<%- attribute.key %>' was null.")<% } else { %>null<% } %><%
131131
}
132132
// BOOLEAN
133133
} else if (attribute.type === 'boolean') {
134134
if (attribute.array) {
135135
-%> (map["<%- attribute.key %>"] as IEnumerable<object>)<%- !attribute.required ? '?' : '' %>.Select(e => Convert.ToBoolean(e)).ToList()<%
136136
} else {
137-
-%> map["<%- attribute.key %>"] != null ? Convert.ToBoolean(map["<%- attribute.key %>"]) : (<%- attribute.required ? 'false' : 'null' %>)<%
137+
-%> map["<%- attribute.key %>"] != null ? Convert.ToBoolean(map["<%- attribute.key %>"]) : <% if (attribute.required) { %>throw new ArgumentNullException("<%- attribute.key %>", "Required attribute '<%- attribute.key %>' was null.")<% } else { %>null<% } %><%
138138
}
139139
// STRING, DATETIME, EMAIL
140140
} else if (attribute.type === 'string' || attribute.type === 'datetime' || attribute.type === 'email') {

0 commit comments

Comments
 (0)