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

Handling of polymorphic class members without a separate repository doesn't work [DATAREST-1186]

Open
#1,551 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

Fabian Günter opened DATAREST-1186 and commented

Hi,
I have a parent class with a polymorphic child member. Adding entities via a Spring-Data-REST repository works fine, but editing (via PATCH) doesn't change the type of the child member, only its attributes.
The parent code is as follows (without getters/setters and some irrelevant JPA annotations):

@Entity
public class Parent {
    @Id
    private Long id;

    @OneToOne(optional = false, cascade = {CascadeType.PERSIST, CascadeType.MERGE}, orphanRemoval = true)
    @JoinColumn(name = "CHILD")
    private Child child;
}

The code of the child is as follows (again, irrelevant fields omitted):

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "@type")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Child {
    // Some common fields, including an ID

    @JsonProperty("@type")
    public String getType() {
        return getClass().getName();
    }
}

The subclasses don't add anything but some simple fields with no references to other entities.
Upon submitting valid JSON to the ParentRepository (extending CrudRepository<Parent, Long>), all fields get merged but the @type property of the child is ignored. This is because of this code in DomainObjectReader:

if (!mappedProperties.hasPersistentPropertyForField(fieldName)) {
    continue;
}

The @type property is obviously not a persistent property, so it gets ignored.
Is there a fix for this or am I missing anything?


Affects: 2.6.9 (Ingalls SR9)

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.