platform icon indicating copy to clipboard operation
platform copied to clipboard

Immutable generated data structures

Open gilberto-torrezan opened this issue 4 years ago • 1 comments
trafficstars

It's recommended and considered a best practice to avoid mutating data received from the endpoints. This is a breaking change that will introduce the readonly flag and ReadonlyArray to the attributes of generated data structures.

With this change, applications written in Fusion will be safer by design.

Refer to this ticket with more technical details.

Use cases

As a developer, when consuming data from a Fusion endpoint and cascading the data over the application, you are safe to know that the received data is immutable, no matter the logic in your application. It needs to be a conscious decision from the developer to create a mutable clone of the data if needed.

Acceptance criteria

  • [ ] When receiving data from endpoints, the received data structure should not be modifiable
  • [ ] Arrays received by endpoints should not allow insertions, removals, or changes in the order of the items inside the array
  • [ ] The documentation is updated mentioning this breaking change in the upgrade guide

Documentation:

https://vaadin.com/docs/latest/fusion/application/accessing-backend/#read-only-access

How to test?

  • Create endpoints with all possible serializable types from Java, and collections of those. See
  • Consume the endpoints from Typescript
  • Try to mutate any of the attributes received from the object
  • No mutation should be possible

gilberto-torrezan avatar Sep 01 '21 13:09 gilberto-torrezan

Initially this change was meant to be the new default in V22 projects, but after some rounds of internal testing, the feedback was that it gets more in the way than it helps, especially when using MobX for state management. It would also create unnecessary friction for developers migrating from V21.

So we won't be pushing this for V22, but consider it as an option (not the default) for V23.

gilberto-torrezan avatar Oct 20 '21 07:10 gilberto-torrezan