spring-projects / spring-projects/spring-data-rest

Unwanted conversion of Generic Data Type to LinkedHashMap [DATAREST-973]

Open
#1,295 0 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

type: bug
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

Roy Krisnadi opened DATAREST-973 and commented

I tried to create SpringBoot Rest

Here's my controller:

@PostMapping
public Response add( Category category) {
    // try to insert category to db and return Response
    Response<Category> response = categoryService.insert(category);
    return response;
}

Response:

// This contains the result, which in this case will be Category
public class Response<T> {
// other fields and getter setter is not included
	private T result;

        public T getResult() {
		return result;
	}

	public void setResult(T result) {
		this.result = result;
	}

Category Service insert:

public Response<T> insert(@NotNull final T bean) {
        Response<T> response = new Response<>();
        bean.setCreated(context.now());
        // The repository is an interface that extends MongoRepository<Category, String>
        response.setResult(repository.save(bean)); 
        response.setSuccess(true);
        return response;
    }

Problem:
When I tried to test the controller as the picture attached
I am expecting to get Category back, instead I get LinkedListMap that I is converted from Category.

I believe the problem is because when I use the generic type, spring will think that it's just a regular Object then convert it to Map.

Expectation:
Spring should return response.getResult() as Category

!SpringTest.JPG|thumbnail!

Sorry I'm not really sure which version of Spring Data Rest this Spring Boot 1.4.3 uses. Maybe I even posting it on the wrong project. But I do my best to create this JIRA hoping that this will be investigated. Thanks


Attachments:

Contributor guide

Open the contributing guide

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.