diff --git a/test-suite/src/test/java/io/micronaut/docs/validation/disable/Application.java b/test-suite/src/test/java/io/micronaut/docs/validation/disable/Application.java new file mode 100644 index 00000000..96277924 --- /dev/null +++ b/test-suite/src/test/java/io/micronaut/docs/validation/disable/Application.java @@ -0,0 +1,46 @@ +package io.micronaut.docs.validation.disable; + +import io.micronaut.context.annotation.Property; +import io.micronaut.core.annotation.Introspected; +import io.micronaut.http.annotation.Controller; +import io.micronaut.http.annotation.Get; +import io.micronaut.runtime.Micronaut; +import jakarta.validation.constraints.Max; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Positive; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +public class Application { + + public static void main(String[] args) { + Micronaut.run(Application.class, args); + } + + @Property(name = "spec.name", value = "ValidationDisableTest") + @Controller + static class MyController { + @Get + MyBean bean(@ValidInternalId int internalId) { + return new MyBean(internalId); + } + } + + @Introspected + public record MyBean(@ValidInternalId int internalId) { + } + + @Documented + @Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.TYPE_USE}) + @Retention(RetentionPolicy.RUNTIME) + @NotNull + @Positive + @Max(Integer.MAX_VALUE) + public @interface ValidInternalId { + } + +} diff --git a/test-suite/src/test/java/io/micronaut/docs/validation/disable/ValidationDisableTest.java b/test-suite/src/test/java/io/micronaut/docs/validation/disable/ValidationDisableTest.java new file mode 100644 index 00000000..a62f14b6 --- /dev/null +++ b/test-suite/src/test/java/io/micronaut/docs/validation/disable/ValidationDisableTest.java @@ -0,0 +1,37 @@ +package io.micronaut.docs.validation.disable; + +import io.micronaut.context.annotation.Property; +import io.micronaut.http.HttpRequest; +import io.micronaut.http.client.HttpClient; +import io.micronaut.http.client.annotation.Client; +import io.micronaut.test.extensions.junit5.annotation.MicronautTest; +import jakarta.inject.Inject; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@Property(name = "spec.name", value = "ValidationDisableTest") +@MicronautTest(rebuildContext = true) +public class ValidationDisableTest { + @Inject + @Client("/") + HttpClient client; + + @Test + @Property(name = "micronaut.validator.enabled", value = "false") + void getBean_withValidationDisabled() { + testIt(); + } + + @Test + @Property(name = "micronaut.validator.enabled", value = "true") + void getBean_withValidationEnabled() { + testIt(); + } + + void testIt() { + var bean = client.toBlocking().retrieve(HttpRequest.GET("?internalId=42"), Application.MyBean.class); + + assertEquals(42, bean.internalId()); + } +} diff --git a/validation/src/main/java/io/micronaut/validation/exceptions/package-info.java b/validation/src/main/java/io/micronaut/validation/exceptions/package-info.java index e4730be8..f04922b3 100644 --- a/validation/src/main/java/io/micronaut/validation/exceptions/package-info.java +++ b/validation/src/main/java/io/micronaut/validation/exceptions/package-info.java @@ -19,5 +19,9 @@ * @author graemerocher * @since 1.0 */ +@Requires(property = ValidatorConfiguration.ENABLED, value = StringUtils.TRUE, defaultValue = StringUtils.TRUE) package io.micronaut.validation.exceptions; +import io.micronaut.context.annotation.Requires; +import io.micronaut.core.util.StringUtils; +import io.micronaut.validation.validator.ValidatorConfiguration; \ No newline at end of file diff --git a/validation/src/main/java/io/micronaut/validation/package-info.java b/validation/src/main/java/io/micronaut/validation/package-info.java index 752faf92..f2016583 100644 --- a/validation/src/main/java/io/micronaut/validation/package-info.java +++ b/validation/src/main/java/io/micronaut/validation/package-info.java @@ -14,10 +14,13 @@ * limitations under the License. */ /** - * Validation advice and interceptors. + * Package annotations. * - * @author graemerocher - * @since 1.0 + * @since 4.9.0 */ +@Requires(property = ValidatorConfiguration.ENABLED, value = StringUtils.TRUE, defaultValue = StringUtils.TRUE) package io.micronaut.validation; +import io.micronaut.context.annotation.Requires; +import io.micronaut.core.util.StringUtils; +import io.micronaut.validation.validator.ValidatorConfiguration; diff --git a/validation/src/main/java/io/micronaut/validation/validator/DefaultValidator.java b/validation/src/main/java/io/micronaut/validation/validator/DefaultValidator.java index 8141e639..f083461b 100644 --- a/validation/src/main/java/io/micronaut/validation/validator/DefaultValidator.java +++ b/validation/src/main/java/io/micronaut/validation/validator/DefaultValidator.java @@ -21,7 +21,6 @@ import io.micronaut.context.annotation.ConfigurationReader; import io.micronaut.context.annotation.Primary; import io.micronaut.context.annotation.Property; -import io.micronaut.context.annotation.Requires; import io.micronaut.context.exceptions.BeanInstantiationException; import io.micronaut.core.annotation.AnnotatedElement; import io.micronaut.core.annotation.AnnotationMetadata; @@ -101,7 +100,6 @@ */ @Singleton @Primary -@Requires(property = ValidatorConfiguration.ENABLED, value = StringUtils.TRUE, defaultValue = StringUtils.TRUE) public class DefaultValidator implements Validator, ExecutableMethodValidator, ReactiveValidator, AnnotatedElementValidator, BeanDefinitionValidator { diff --git a/validation/src/main/java/io/micronaut/validation/validator/constraints/package-info.java b/validation/src/main/java/io/micronaut/validation/validator/constraints/package-info.java new file mode 100644 index 00000000..71cbe0b1 --- /dev/null +++ b/validation/src/main/java/io/micronaut/validation/validator/constraints/package-info.java @@ -0,0 +1,26 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * Package annotations. + * + * @since 4.9.0 + */ +@Requires(property = ValidatorConfiguration.ENABLED, value = StringUtils.TRUE, defaultValue = StringUtils.TRUE) +package io.micronaut.validation.validator.constraints; + +import io.micronaut.context.annotation.Requires; +import io.micronaut.core.util.StringUtils; +import io.micronaut.validation.validator.ValidatorConfiguration; diff --git a/validation/src/main/java/io/micronaut/validation/validator/extractors/package-info.java b/validation/src/main/java/io/micronaut/validation/validator/extractors/package-info.java new file mode 100644 index 00000000..738abfe9 --- /dev/null +++ b/validation/src/main/java/io/micronaut/validation/validator/extractors/package-info.java @@ -0,0 +1,26 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * Package annotations. + * + * @since 4.9.0 + */ +@Requires(property = ValidatorConfiguration.ENABLED, value = StringUtils.TRUE, defaultValue = StringUtils.TRUE) +package io.micronaut.validation.validator.extractors; + +import io.micronaut.context.annotation.Requires; +import io.micronaut.core.util.StringUtils; +import io.micronaut.validation.validator.ValidatorConfiguration; diff --git a/validation/src/main/java/io/micronaut/validation/validator/messages/package-info.java b/validation/src/main/java/io/micronaut/validation/validator/messages/package-info.java new file mode 100644 index 00000000..75726cdd --- /dev/null +++ b/validation/src/main/java/io/micronaut/validation/validator/messages/package-info.java @@ -0,0 +1,26 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * Package annotations. + * + * @since 4.9.0 + */ +@Requires(property = ValidatorConfiguration.ENABLED, value = StringUtils.TRUE, defaultValue = StringUtils.TRUE) +package io.micronaut.validation.validator.messages; + +import io.micronaut.context.annotation.Requires; +import io.micronaut.core.util.StringUtils; +import io.micronaut.validation.validator.ValidatorConfiguration; diff --git a/validation/src/main/java/io/micronaut/validation/validator/package-info.java b/validation/src/main/java/io/micronaut/validation/validator/package-info.java new file mode 100644 index 00000000..431fc076 --- /dev/null +++ b/validation/src/main/java/io/micronaut/validation/validator/package-info.java @@ -0,0 +1,25 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * Package annotations. + * + * @since 4.9.0 + */ +@Requires(property = ValidatorConfiguration.ENABLED, value = StringUtils.TRUE, defaultValue = StringUtils.TRUE) +package io.micronaut.validation.validator; + +import io.micronaut.context.annotation.Requires; +import io.micronaut.core.util.StringUtils; diff --git a/validation/src/main/java/io/micronaut/validation/validator/resolver/package-info.java b/validation/src/main/java/io/micronaut/validation/validator/resolver/package-info.java new file mode 100644 index 00000000..6dbd26b3 --- /dev/null +++ b/validation/src/main/java/io/micronaut/validation/validator/resolver/package-info.java @@ -0,0 +1,26 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * Package annotations. + * + * @since 4.9.0 + */ +@Requires(property = ValidatorConfiguration.ENABLED, value = StringUtils.TRUE, defaultValue = StringUtils.TRUE) +package io.micronaut.validation.validator.resolver; + +import io.micronaut.context.annotation.Requires; +import io.micronaut.core.util.StringUtils; +import io.micronaut.validation.validator.ValidatorConfiguration; diff --git a/validation/src/test/groovy/io/micronaut/validation/DisableValidationSpec.groovy b/validation/src/test/groovy/io/micronaut/validation/DisableValidationSpec.groovy new file mode 100644 index 00000000..5cf0b781 --- /dev/null +++ b/validation/src/test/groovy/io/micronaut/validation/DisableValidationSpec.groovy @@ -0,0 +1,33 @@ +package io.micronaut.validation + +import io.micronaut.context.annotation.Property +import io.micronaut.http.HttpRequest +import io.micronaut.http.client.HttpClient +import io.micronaut.http.client.annotation.Client +import io.micronaut.test.extensions.spock.annotation.MicronautTest +import jakarta.inject.Inject +import spock.lang.Specification + +@MicronautTest(rebuildContext = true) +class DisableValidationSpec extends Specification { + @Inject + @Client("/") + HttpClient client; + + @Property(name = "micronaut.validator.enabled", value = "false") + void "getBean with validation disabled"() { + testIt(); + } + + @Property(name = "micronaut.validator.enabled", value = "true") + void "getBean with validation enabled"() { + testIt(); + } + + void testIt() { + var bean = client.toBlocking().retrieve(HttpRequest.GET("?internalId=42"), Application.MyBean.class); + + assertThat(bean.internalId()).isEqualTo(42); + } +} +