RWS / dxa-web-application-java

SDL Digital Experience Accelerator Java Spring MVC web application
25 stars 37 forks source link

DXA entities cache fails for schemas mapped in multiple entities #113

Closed jajimene closed 5 years ago

jajimene commented 6 years ago

When DXA cache is enabled, "entities" cache is enabled by default.

In case of multiple ViewModels (or DXA Entities) mapping the same schema, DXA is not able to match the proper class when retrieving data from Cache.

In a very simple example:

@SemanticEntity(entityName = "image")
public EntityA { }

@SemanticEntity(entityName = "image")
public EntityB { }
  1. Imagine there's a Page-First that includes a component with id "1234" using EntityA component template. When this request is first made, the entity gets cached.
  2. Later, a request to a different page Page-Second that includes the same component with id "1234" using another component template EntityB comes in.
  3. DXA sees there's a component with id "1234" already cached, and it tries to reuse it for Page-Second page model builder. However, the instance cached is of class EntityA, while Page-Second is expecting an EntityB, throwing then a java.lang.IllegalArgumentException.

Checking the internals, it looks related to the key used to cache the Link models:

For View Model 

cachekey= LocalizationAwareCacheKey(localizationId=<PublicationId>, key=SimpleKey [<Itemid>,<SchemaId>,MVCData])

DXA is able to Map the data from cache

For Link Model 

cachekey= LocalizationAwareCacheKey(localizationId=<PublicationId>, key=SimpleKey [<Itemid>,<SchemaId>,null]) 

Where the key is using only itemId + schemaId.

As we have different Component Link models for same schema, DXA fails to map it if entity comes from cache with a different type of object (for the same component) than what it is expected in the viewmodel.

rpannekoek commented 5 years ago

Internal Issue ID (for tracking purposes): CRQ-12844

rpannekoek commented 5 years ago

Fixed in DXA 2.2