Skip to content

Commit

Permalink
cast dictionary to any during assignment (#1357)
Browse files Browse the repository at this point in the history
* cast dictionary to any during assignment

* update dictionary tests
  • Loading branch information
jjoekoullas committed Apr 12, 2021
1 parent 288eb13 commit 8853956
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public async Task When_property_is_dto_dictionary_then_assignment_may_create_new
var code = codeGenerator.GenerateFile("Test");

//// Assert
Assert.Contains("this.resource[key] = _data[\"resource\"][key] ? MyItem.fromJS(_data[\"resource\"][key]) : new MyItem();", code);
Assert.Contains("(<any>this.resource)[key] = _data[\"resource\"][key] ? MyItem.fromJS(_data[\"resource\"][key]) : new MyItem();", code);
}

[Fact]
Expand Down Expand Up @@ -195,7 +195,7 @@ public async Task When_property_is_string_dictionary_then_assignment_is_correct(
var code = codeGenerator.GenerateFile("Test");

//// Assert
Assert.Contains("this.resource[key] = _data[\"resource\"][key];", code);
Assert.Contains("(<any>this.resource)[key] = _data[\"resource\"][key];", code);
}

public class DictionaryContainer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ if ({{ Value }}) {
for (let key in {{ Value }}) {
if ({{ Value }}.hasOwnProperty(key))
{% if IsDictionaryValueNewableObject -%}
{{ Variable }}{% if RequiresStrictPropertyInitialization %}!{% endif %}[key] = {{ Value }}[key] ? {{ DictionaryValueType }}.fromJS({{ Value }}[key]{% if HandleReferences %}, _mappings{% endif %}) : {% if HasDictionaryValueDefaultValue %}{{ DictionaryValueDefaultValue }}{% else %}<any>{{ NullValue }}{% endif %};
(<any>{{ Variable }}){% if RequiresStrictPropertyInitialization %}!{% endif %}[key] = {{ Value }}[key] ? {{ DictionaryValueType }}.fromJS({{ Value }}[key]{% if HandleReferences %}, _mappings{% endif %}) : {% if HasDictionaryValueDefaultValue %}{{ DictionaryValueDefaultValue }}{% else %}<any>{{ NullValue }}{% endif %};
{% elseif IsDictionaryValueNewableArray -%}
{{ Variable }}{% if RequiresStrictPropertyInitialization %}!{% endif %}[key] = {{ Value }}[key] ? {{ Value }}[key].map((i: any) => {{ DictionaryValueArrayItemType }}.fromJS(i{% if HandleReferences %}, _mappings{% endif %})) : {% if HasDictionaryValueDefaultValue %}{{ DictionaryValueDefaultValue }}{% else %}<any>{{ NullValue }}{% endif %};
(<any>{{ Variable }}){% if RequiresStrictPropertyInitialization %}!{% endif %}[key] = {{ Value }}[key] ? {{ Value }}[key].map((i: any) => {{ DictionaryValueArrayItemType }}.fromJS(i{% if HandleReferences %}, _mappings{% endif %})) : {% if HasDictionaryValueDefaultValue %}{{ DictionaryValueDefaultValue }}{% else %}<any>{{ NullValue }}{% endif %};
{% elseif IsDictionaryValueDate or IsDictionaryValueDateTime -%}
{{ Variable }}{% if RequiresStrictPropertyInitialization %}!{% endif %}[key] = {{ Value }}[key] ? {{ StringToDateCode }}({{ Value }}[key].toString()) : {% if HasDictionaryValueDefaultValue %}{{ DictionaryValueDefaultValue }}{% else %}<any>{{ NullValue }}{% endif %};
(<any>{{ Variable }}){% if RequiresStrictPropertyInitialization %}!{% endif %}[key] = {{ Value }}[key] ? {{ StringToDateCode }}({{ Value }}[key].toString()) : {% if HasDictionaryValueDefaultValue %}{{ DictionaryValueDefaultValue }}{% else %}<any>{{ NullValue }}{% endif %};
{% else -%}
{% if(HasDictionaryValueDefaultValue || NullValue != "undefined"){ -%}
{{ Variable }}{% if RequiresStrictPropertyInitialization %}!{% endif %}[key] = {{ Value }}[key] !== undefined ? {{ Value }}[key] : {% if HasDictionaryValueDefaultValue %}{{ DictionaryValueDefaultValue }}{% else %}<any>{{ NullValue }}{% endif %};
(<any>{{ Variable }}){% if RequiresStrictPropertyInitialization %}!{% endif %}[key] = {{ Value }}[key] !== undefined ? {{ Value }}[key] : {% if HasDictionaryValueDefaultValue %}{{ DictionaryValueDefaultValue }}{% else %}<any>{{ NullValue }}{% endif %};
{% else -%}
{{ Variable }}{% if RequiresStrictPropertyInitialization %}!{% endif %}[key] = {{ Value }}[key];
(<any>{{ Variable }}){% if RequiresStrictPropertyInitialization %}!{% endif %}[key] = {{ Value }}[key];
{% endif -%}
{% endif -%}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ if ({{ Value }}) {
for (let key in {{ Value }}) {
if ({{ Value }}.hasOwnProperty(key))
{% if IsDictionaryValueNewableObject -%}
{{ Variable }}[key] = {{ Value }}[key] ? {{ Value }}[key].toJSON() : <any>{{ NullValue }};
(<any>{{ Variable }})[key] = {{ Value }}[key] ? {{ Value }}[key].toJSON() : <any>{{ NullValue }};
{% elseif IsDictionaryValueDate -%}
{{ Variable }}[key] = {{ Value }}[key] ? {% if UseJsDate %}formatDate({{ Value }}[key]){% else %}{{ Value }}[key].{{ DateToStringCode }}{% endif %} : <any>{{ NullValue }};
(<any>{{ Variable }})[key] = {{ Value }}[key] ? {% if UseJsDate %}formatDate({{ Value }}[key]){% else %}{{ Value }}[key].{{ DateToStringCode }}{% endif %} : <any>{{ NullValue }};
{% elseif IsDictionaryValueDateTime -%}
{{ Variable }}[key] = {{ Value }}[key] ? {{ Value }}[key].{{ DateTimeToStringCode }} : <any>{{ NullValue }};
(<any>{{ Variable }})[key] = {{ Value }}[key] ? {{ Value }}[key].{{ DateTimeToStringCode }} : <any>{{ NullValue }};
{% else -%}
{% if NullValue != "undefined" -%}
{{ Variable }}[key] = {{ Value }}[key] !== undefined ? {{ Value }}[key] : <any>{{ NullValue }};
(<any>{{ Variable }})[key] = {{ Value }}[key] !== undefined ? {{ Value }}[key] : <any>{{ NullValue }};
{% else -%}
{{ Variable }}[key] = {{ Value }}[key];
(<any>{{ Variable }})[key] = {{ Value }}[key];
{% endif -%}
{% endif -%}
}
Expand Down

0 comments on commit 8853956

Please sign in to comment.