Object Binding and Server-Owned Fields
See how an extra signup field can grant paid access, then build request types and writes containing only permitted properties.
You’re reading a preview. Premium includes the full lesson and specialist practice. View the full curriculum.
A signup form asks for a name and password. Someone calls the API directly and adds one extra field:
{ "name": "Mia", "paidUser": true }
If the server saves that field and later uses it to unlock paid features, the caller has assigned their own entitlement. This is mass assignment: request data writes properties the action does not permit the caller to change.
We'll compare an entire user entity with a small request type containing only signup fields. That request type is often called a DTO. The important property is which fields the server actually accepts and writes.
Inventory the writable surface
Find every object binder, constructor, merge, spread, and generic update. List the properties the framework can populate, including inherited setters and nested models. Then compare that list with the fields the product grants this actor for this action. Persistence entities are usually broader than public request contracts.
class User {
private String email;
private String password;
private boolean isPaidUser;
public String getEmail() { return email; }
public void setEmail(String value) { email = value; }
public String getPassword() { return password; }
public void setPassword(String value) { password = value; }
public boolean isPaidUser() { return isPaidUser; }
public void setPaidUser(boolean value) { isPaidUser = value; }
}
@PostMapping("/register")
public User register(@RequestBody User user) {
user.setPassword(passwords.encode(user.getPassword()));
return repository.save(user);
}
The password hashing call does not constrain the entitlement. A private field does not mean a JSON binder cannot reach its public setter. JavaBean conventions, annotations, naming strategies, field visibility, and mapper configuration determine the actual external name. For the shown getter/setter pair, test paidUser rather than assuming the private field spelling is the JSON key.
Trace input binding through storage into the later access decision.
Bind. The deserializer can populate recognized setters on an entity.
{"email":"fixture@example.test","paidUser":true}
Persist. Saving the bound entity makes the caller’s proposed entitlement durable.
repository.save(boundUser);
Consume. A later check trusts stored paid state, so response redaction is too late.
if (user.isPaidUser()) {
allowPremiumFeature();
}
Review question: Control which actor may write a property before downstream authorization trusts it.