Skip to content

disallow mutation of record types by clients#944

Merged
gavinking merged 2 commits intojakartaee:mainfrom
gavinking:record-clone
Feb 20, 2026
Merged

disallow mutation of record types by clients#944
gavinking merged 2 commits intojakartaee:mainfrom
gavinking:record-clone

Conversation

@gavinking
Copy link
Copy Markdown
Member

We have some record types with array members. We should prevent mutation of the arrays by returning clones to the client.

@njr-11 @otaviojava do we need to do something similar in Jakarta Data?

@gavinking gavinking marked this pull request as ready for review February 5, 2026 10:01
@njr-11
Copy link
Copy Markdown
Member

njr-11 commented Feb 5, 2026

@njr-11 @otaviojava do we need to do something similar in Jakarta Data?

I checked and Jakarta Data mostly covers this, but I did identify 3 places that we can fix. I will open a PR there.

Copy link
Copy Markdown
Member

@njr-11 njr-11 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. But also note there is another way for clients to mutate it. They could do,

mapping = new CompoundMapping(elements);
...
elements[0] = ...

Although someone doing that is probably trying to cause trouble.

@gavinking
Copy link
Copy Markdown
Member Author

But also note there is another way for clients to mutate it

Yeah true so maybe the constructor should also clone.

But I was more worried about them trying to mutate the ones returned by EMF.getResultSetMappings(), where it's the provider creating the ResultSetMappings.

Anyway, good catch, I will fix it anyway.

@gavinking
Copy link
Copy Markdown
Member Author

I will fix it anyway.

I'm now also cloning things in record constructors.

Keep finding the whole record construct to be far less useful than I had hoped. By the time you actually add all the extra boilerplate code to make them deeply immutable, they may as well just have been a class :-(

@njr-11 njr-11 assigned gavinking and unassigned njr-11 Feb 5, 2026
@njr-11
Copy link
Copy Markdown
Member

njr-11 commented Feb 5, 2026

Switched the assignee back to Gavin who authored this PR. It is likely I was mistakenly added as assignee when attempting to set the reviewer.

@beikov
Copy link
Copy Markdown
Contributor

beikov commented Feb 5, 2026

How about changing the record components to be lists instead of arrays to avoid the copying on read access? If we use List.copyOf() all the way, the copies are even avoided.

@gavinking
Copy link
Copy Markdown
Member Author

How about changing the record components to be lists instead of arrays to avoid the copying on read access? If we use List.copyOf() all the way, the copies are even avoided.

Aaaahmm, sure, we could do that. Looks like we already used that pattern in at least one place in the Data APIs.

Having a tough time deciding if it's on balance better. It's slightly better for performance, slightly worse for type safety, so I dunno 🤷‍♂️

@gavinking gavinking merged commit 1090bd0 into jakartaee:main Feb 20, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants