google / google/closure-templates

class com.google.template.soy.data.internal.SoyMapImpl cannot be cast to class com.google.template.soy.data.SoyLegacyObjectMap

Open
#223 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
700
Forks
199
Avg merge
2d 12h
Merged PRs (30d)
26

Description

Let's say you have a use case with a param `x` of type `map`. This map includes other nested maps as value. Now, assume you need to access `x['field1']['field2']` for instance. This does not work out of the box.

Looking at the source code, I tried to mark nested maps as SoyMap with `com.google.template.soy.data.SoyValueConverter.markAsSoyMap`. Unfortunately, I am still getting this error message:

```java
Unexpected error occurred: class com.google.template.soy.data.internal.SoyMapImpl cannot be cast to class com.google.template.soy.data.SoyLegacyObjectMap (com.google.template.soy.data.internal.SoyMapImpl and com.google.template.soy.data.SoyLegacyObjectMap are in unnamed module of loader 'app')
java.lang.ClassCastException: class com.google.template.soy.data.internal.SoyMapImpl cannot be cast to class com.google.template.soy.data.SoyLegacyObjectMap (com.google.template.soy.data.internal.SoyMapImpl and com.google.template.soy.data.SoyLegacyObjectMap are in unnamed module of loader 'app')
at mynamespace.Layout.render(mytemplate.soy:68)
```

Line 68 of the soy template contains `

{$x['field1']['field2']}
`.

Is SoySauce supporting this use case? as a workaround, I am thinking about flattening nested maps or to define a custom function with "variable args" to get access to the nested fields. If indexed access operators are not supposed to be used in a nested context, what is the recommended approach for the best compliance with the future of closure templates?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.