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

DomainObjectReader bypasses value object invariants [DATAREST-1191]

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

Patrik Mihalcin opened DATAREST-1191 and commented

I have Validity value object:

@Value
@Embeddable
public class Validity {
    public static final String VALID_FROM_CANT_BE_NULL = "Valid from can't be null";
    public static final String VALID_TO_CANT_BE_BEFORE_VALID_FROM = "Valid to can't be before valid from";

    @Column(name = "VALID_FROM", nullable = false)
    private final LocalDate validFrom;

    @Column(name = "VALID_TO")
    private final LocalDate validTo;

    public Validity(LocalDate validFrom, LocalDate validTo) {
        checkValidity(validFrom, validTo);

        this.validFrom = validFrom;
        this.validTo = validTo;
    }

    private void checkValidity(LocalDate validFrom, LocalDate validTo) {
        Assert.notNull(validFrom, VALID_FROM_CANT_BE_NULL);
        if (validTo != null) {
            Assert.isTrue(validFrom.isBefore(validTo) || validFrom.isEqual(validTo), VALID_TO_CANT_BE_BEFORE_VALID_FROM);
        }
    }
}

When I want to create new Validity using REST API, I can issue POST request, but I have to tell Jackson to use Validity constructor using following Jackson customization:

abstract static class ValidityMixin {
	@JsonCreator
	public ValidityMixin(@JsonProperty(required = true) LocalDate validFrom,
							LocalDate validTo) {
	}
}

When I want to update existing Validity object and I issue PATCH request, DomainObjectReader used by JsonPatchHandler doesn't go through constructor and it sets fields using reflection and system ends up with Validity object with broken invariant.

Why is that?


No further details from DATAREST-1191

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.