spring-projects / spring-projects/spring-hateoas

How to apply representation model processors recursively?

Open
#1,318 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.1k
Forks
476
PR merge metrics
No merged PRs in 30d

Description

I have a question concerning the representation model processors of Spring HATEOAS. We are experimenting to process models before serializing them to the client. Our use case is to enrich the imageUrl field of UserModel objects at runtime, as we have to build the URL based on values from a config bean (AWS S3 bucket URL differs for DEV / PROD setup).

@Data
public class UserModel {
    // ...
    private String imageUrl;
}

Therefore, we create a UserProcessor to implement this:

public class UserProcessor implements RepresentationModelProcessor<EntityModel<UserModel>> {

    private final ConfigAccessor configAccessor;

    public UserProcessor(ConfigAccessor configAccessor) {
        this.configAccessor = configAccessor;
    }

    @Override
    public EntityModel<UserModel> process(EntityModel<UserModel> model) {
        if (model.getContent() != null)
            // do the enrichment and set "imageUrl" field
        }
        return model;
    }
}

This works perfectly if we have a controller method like this:

@ResponseBody
@GetMapping("/me")
public EntityModel<UserModel> getCurrentUser(@AuthenticationPrincipal Principal principal) {
    UserModel user = ... // get user model
    return EntityModel.of(user);
}

However, we are struggling now with the enrichment whenever a UserModel is referenced in another model class, e.g., the BookModel:

@Data
public class BookModel {
    private String isbn;
    // ...
    private EntityModel<UserModel> user;
}

A controller method returning type EntityModel<BookModel> only applies the processor for its type, but not for types that are referenced. It seems the processors are not applied recursively.

Is this intentional or are we doing something wrong?

Thanks for any input and help,
Michael

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how RepresentationModelProcessor is applied when a controller returns EntityModel, then compare that path with the UserProcessor handling EntityModel. Determine whether referenced UserModel instances are expected to be processed recursively; completion should establish the intended behavior and identify the appropriate test or documentation change.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.