forked from micronaut-projects/micronaut-validation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed micronaut-projects#372
- Loading branch information
Showing
11 changed files
with
255 additions
and
5 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
test-suite/src/test/java/io/micronaut/docs/validation/disable/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
test-suite/src/test/java/io/micronaut/docs/validation/disable/ValidationDisableTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
validation/src/main/java/io/micronaut/validation/validator/constraints/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
26 changes: 26 additions & 0 deletions
26
validation/src/main/java/io/micronaut/validation/validator/extractors/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
26 changes: 26 additions & 0 deletions
26
validation/src/main/java/io/micronaut/validation/validator/messages/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
25 changes: 25 additions & 0 deletions
25
validation/src/main/java/io/micronaut/validation/validator/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
26 changes: 26 additions & 0 deletions
26
validation/src/main/java/io/micronaut/validation/validator/resolver/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
33 changes: 33 additions & 0 deletions
33
validation/src/test/groovy/io/micronaut/validation/DisableValidationSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
|