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

Validating Annotations at Compile Time does not seem to work as described in doc #380

Open
roar-skinderviken opened this issue Jun 18, 2024 · 2 comments

Comments

@roar-skinderviken
Copy link

Expected Behavior

When following the docs here:
https://micronaut-projects.github.io/micronaut-validation/snapshot/guide/index.html#compileTimeValidation
I expect the below annotation to be validated

import jakarta.validation.Constraint

@Retention(AnnotationRetention.RUNTIME)
@Constraint(validatedBy = [])
annotation class TimeOff(
    @DurationPattern val maxDuration: String
)

Actual Behaviour

Given the following service

import jakarta.inject.Singleton
import jakarta.validation.constraints.NotBlank
import java.time.Duration

@Singleton
open class HolidayService {

    open fun startHoliday(
        @NotBlank person: String,
        @TimeOff(maxDuration = "rubbish") duration: String // should give compile time error
    ): String = "Person $person is off on holiday for ${Duration.parse(duration).toDays()} days"
}

@TimeOff(maxDuration = "rubbish") does not fail compilation.

Annotation DurationPattern is defined like this

import jakarta.validation.Constraint
import kotlin.annotation.AnnotationRetention.RUNTIME

@Retention(RUNTIME)
@Constraint(validatedBy = [])
annotation class DurationPattern(
    val message: String = "invalid duration ({validatedValue})" // (2)
)

DurationPatternValidator is defines like this

import io.micronaut.core.annotation.AnnotationValue
import io.micronaut.validation.validator.constraints.ConstraintValidator
import io.micronaut.validation.validator.constraints.ConstraintValidatorContext

class DurationPatternValidator : ConstraintValidator<DurationPattern, CharSequence> {
    override fun isValid(
        value: CharSequence?,
        annotationMetadata: AnnotationValue<DurationPattern>,
        context: ConstraintValidatorContext): Boolean {
        return value == null || value.toString().matches("^P?\\d+D$".toRegex())
    }
}

/src/main/resources/META-INF/services/io.micronaut.validation.validator.constraints.ConstraintValidator

no.javatec.DurationPatternValidator

Steps To Reproduce

Clone the example application and run
./gradlew clean build

Please observe that the tests are executed.

Environment Information

Ubuntu 22.04
JDK 21 (Temurin)

Example Application

https://github.com/roar-skinderviken/compile-time-validation-issue

Version

4.5.0

@graemerocher
Copy link
Contributor

the validator needs to be your annotation processor classpath and cannot be in the same project/source tree

@roar-skinderviken
Copy link
Author

Thanks for looking into this @graemerocher

I've updated the example app
https://github.com/roar-skinderviken/compile-time-validation-issue
to a multi-module project with the following modules:

  • custom-annotations (Kotlin)
  • demo-app (Kotlin)
  • demo-app-java

Build fails as expected for demo-app-java when applying @TimeOff(duration = "rubbish") to HolidayService#startHoliday.

The Kotlin demo-app still builds without errors with the same annotation. Any advice on how to fix this will be appreciated.

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

No branches or pull requests

2 participants