Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default method parameters on Micronaut Data interface are not validated #388

Closed
ilopmar opened this issue Feb 4, 2020 · 2 comments
Closed

Comments

@ilopmar
Copy link
Contributor

ilopmar commented Feb 4, 2020

Parameters in a default method in a Micronaut Data repository are not validated. This doesn't happen with other methods defined in the interface that Micronaut implements.

Steps to Reproduce

Entity and DTO:

@Entity
@Table(name = "SENSOR")
public class SensorEntity {

    @Id
    @GeneratedValue
    private Long id;
    // more fields, constructor, getters/setters
   ...
}

@Introspected
public class Sensor {

    private Long id;
    // more fields, constructor, getters/setters
   ...
}

Micronaut Data interface:

@Validated
@JdbcRepository(dialect = Dialect.MYSQL)
public interface SensorRepository extends CrudRepository<SensorEntity, Long> {

    default Optional<Sensor> findSensorById(@Nonnull @NotNull Long id) {
        return Optional.empty();
    }
}

Spock Test:

    @Unroll
    void 'test "findSensorById(#id)" triggers ConstraintViolationException'() {
        when:
        sensorRepository.findSensorById(id)

        then:
        def e = thrown(ConstraintViolationException)
        e.constraintViolations.collect { it.propertyPath.toString() }.any { it.contains(field) }
        e.constraintViolations.collect { it.message }.any { it.contains(errorMessage) }

        where:
        id   | field               | errorMessage
        null | 'findSensorById.id' | 'must not be null'
    }

Expected Behaviour

The test should pass.

Actual Behaviour

The test fails with:

Expected exception of type 'javax.validation.ConstraintViolationException', but no exception was thrown
Expected exception of type 'javax.validation.ConstraintViolationException', but no exception was thrown
	at org.spockframework.lang.SpecInternals.checkExceptionThrown(SpecInternals.java:80)
	at org.spockframework.lang.SpecInternals.thrownImpl(SpecInternals.java:67)
	at mn.data.defmethod.SensorRepositoryConstraintsSpec.test "findSensorById(#id)" triggers ConstraintViolationException(SensorRepositoryConstraintsSpec.groovy:22)

Please note that if from the default method I call another method like findById then the test fails with the following error referencing findById instead of the original method.

e.constraintViolations.collect { it.propertyPath.toString() }.any { it.contains(field) }
| |                    |                                      |
| |                    [findById.id]                          false
| [io.micronaut.validation.validator.DefaultValidator$DefaultConstraintViolation@202479c0]
javax.validation.ConstraintViolationException: findById.id: must not be null

Environment Information

  • Operating System: Linux Mint
  • Micronaut Version: 1.3.0.RC1
  • JDK Version: OpenJDK 11

Example Application

https://github.com/ilopmar/mn-data-default-method-validation

@stale
Copy link

stale bot commented Apr 4, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@graemerocher
Copy link
Contributor

Fixed in core

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants