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

Cannot PATCH an entity, that has an enum set, to increase the number of enums, if 'enum traslation' is enabled [DATAREST-1139]

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

@odrotbohm is already working on this.

Since Dec 31, 2020.

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

Description

Sergei Poznanski opened DATAREST-1139 and commented

If an entity has an enum set property, and 'enum traslation' is enabled (spring.data.rest.enable-enum-translation=true), the trying to PATCH this entity, to increase a number of enums in the set, leads to an error:

Could not read payload!; nested exception is java.lang.IllegalStateException: Can only translate enum with property information!

If I update the enum set with the same number of enums (or less) then the entity is updated correctly.

PUT updates the enum set correctly in anyway.

If I turn off the 'enum traslation' then PATCH works as expected.

Example:

@Entity
public class User {

    private String name;

    @Enumerated(EnumType.STRING)
    @CollectionTable(name = "roles", joinColumns = @JoinColumn(name = "user_id"))
    @ElementCollection
    private Set<Role> roles;

    // other stuff 
}

public enum Role {
    ADMIN, USER, POWER_USER
}

Enum translation:

io.github.cepr0.enumtranslatingissue.Role.ADMIN=Admin
io.github.cepr0.enumtranslatingissue.Role.USER=User
io.github.cepr0.enumtranslatingissue.Role.POWER_USER=Power user

Error:

GET http://localhost:8080/api/users/1
{
    "name": "user1",
    "roles": [
        "Admin",
        "User"
    ],
    "_links": {
        "self": {
            "href": "http://localhost:8080/api/users/1"
        },
        "user": {
            "href": "http://localhost:8080/api/users/1"
        }
    }
}

PATCH http://localhost:8080/api/users/1
{
  "roles": [
          "Admin",
          "User",
          "Power user"
      ]
}

{
    "cause": {
        "cause": null,
        "message": "Can only translate enum with property information!"
    },
    "message": "Could not read payload!; nested exception is java.lang.IllegalStateException: Can only translate enum with property information!"
}

Demo project


Affects: 2.6.7 (Ingalls SR7)

Reference URL: https://stackoverflow.com/q/46103203

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.