generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to select first value extractor type argument by default (#424)
* Allow to select first value extractor type argument by default * fix(deps): update dependency io.micronaut.kotlin:micronaut-kotlin-bom to v4.4.0 * fix(deps): update dependency io.micronaut.reactor:micronaut-reactor-bom to v3.5.0 * fix(deps): update dependency io.micronaut.rxjava2:micronaut-rxjava2-bom to v2.5.0 gradle/libs.versions.toml --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
- Loading branch information
1 parent
78a6e49
commit ebd8a00
Showing
8 changed files
with
110 additions
and
23 deletions.
There are no files selected for viewing
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
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
4 changes: 4 additions & 0 deletions
4
...n/src/test/groovy/io/micronaut/validation/validator/constraints/unwrapped/MyOptional.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,4 @@ | ||
package io.micronaut.validation.validator.constraints.unwrapped; | ||
|
||
public record MyOptional<V>(V value) { | ||
} |
16 changes: 16 additions & 0 deletions
16
...t/groovy/io/micronaut/validation/validator/constraints/unwrapped/MyOptionalExtractor.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,16 @@ | ||
package io.micronaut.validation.validator.constraints.unwrapped; | ||
|
||
import jakarta.inject.Singleton; | ||
import jakarta.validation.valueextraction.UnwrapByDefault; | ||
import jakarta.validation.valueextraction.ValueExtractor; | ||
|
||
@UnwrapByDefault | ||
@Singleton | ||
public class MyOptionalExtractor implements ValueExtractor<MyOptional<?>> { | ||
|
||
@Override | ||
public void extractValues(MyOptional<?> originalValue, ValueReceiver receiver) { | ||
receiver.value("value", originalValue.value()); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
...groovy/io/micronaut/validation/validator/constraints/unwrapped/ValueExtractorsSpec.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,51 @@ | ||
package io.micronaut.validation.validator.constraints.unwrapped | ||
|
||
|
||
import io.micronaut.annotation.processing.test.AbstractTypeElementSpec | ||
import io.micronaut.context.ApplicationContext | ||
import io.micronaut.validation.validator.Validator | ||
import spock.lang.AutoCleanup | ||
import spock.lang.Shared | ||
|
||
class ValueExtractorsSpec extends AbstractTypeElementSpec { | ||
|
||
@Shared | ||
@AutoCleanup | ||
ApplicationContext context = ApplicationContext.run() | ||
|
||
@Shared | ||
Validator validator = context.getBean(Validator) | ||
|
||
void "test @NotNull should be applied on the optional value"() { | ||
given: | ||
def introspection = buildBeanIntrospection('test.Test', """ | ||
package test; | ||
import jakarta.validation.Payload; | ||
import jakarta.validation.constraints.NotNull; | ||
import io.micronaut.validation.validator.constraints.unwrapped.MyOptional; | ||
@io.micronaut.core.annotation.Introspected | ||
class Test { | ||
@NotNull | ||
private MyOptional<String> field; | ||
public MyOptional<String> getField() { | ||
return field; | ||
} | ||
public void setField(MyOptional<String> f) { | ||
this.field = f; | ||
} | ||
} | ||
""") | ||
def instance = introspection.instantiate() | ||
def prop = introspection.getProperty("field").get() | ||
prop.set(instance, new MyOptional(null)) | ||
def constraintViolations = validator.validate(introspection, instance) | ||
|
||
expect: | ||
constraintViolations.size() == 1 | ||
constraintViolations.iterator().next().message == "must not be null" | ||
} | ||
} |