Skip to content

Commit

Permalink
fix: avoid adding unnamed Bean node in constraint validation message …
Browse files Browse the repository at this point in the history
…path (#298) (#300)

Co-authored-by: Le Gall, Benoit <benoit.legall@rakuten.com>
  • Loading branch information
2 people authored and sdelamo committed Jan 30, 2024
1 parent b6a182f commit 9a033e1
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ protected String buildMessage(ConstraintViolation<?> violation) {
}
message.append(']');
}
if (node.getKind() != ElementKind.CONTAINER_ELEMENT) {
if (node.getKind() != ElementKind.CONTAINER_ELEMENT && node.getName() != null) {
if (!firstNode) {
message.append('.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;

@ValidPojo
@Introspected
public class Pojo {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.micronaut.validation;

import java.util.Objects;
import jakarta.inject.Singleton;

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

@Singleton
@Introspected
public class PojoValidator implements ConstraintValidator<ValidPojo, Pojo> {

@Override
public boolean isValid(Pojo pojo, AnnotationValue<ValidPojo> annotationMetadata,
ConstraintValidatorContext context) {
if (Objects.equals(pojo.getEmail(), pojo.getName())) {
context.messageTemplate("Email and Name can not be identical");
return false;
}

return true;
}
}
19 changes: 19 additions & 0 deletions validation/src/test/groovy/io/micronaut/validation/ValidPojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.micronaut.validation;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import jakarta.validation.Constraint;

/**
* Simple annotation to validate behavior when adding validation annotation at class level
*/
@Documented
@Target(TYPE)
@Retention(RUNTIME)
@Constraint(validatedBy = {PojoValidator.class})
public @interface ValidPojo {
}
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,36 @@ class ValidatedSpec extends Specification {
server.close()
}

def "test validated controller validates @Valid classes with validation annotation set on class"() {
given:
ApplicationContext context = ApplicationContext.run([
'spec.name': getClass().simpleName
])
EmbeddedServer embeddedServer = context.getBean(EmbeddedServer).start()
HttpClient client = context.createBean(HttpClient, embeddedServer.getURL())
EmbeddedServer server = ApplicationContext.run(EmbeddedServer)

when:
HttpResponse<String> response = client.toBlocking().exchange(
HttpRequest.POST("/validated/pojo", '{"email":"test@example.com","name":"test@example.com"}')
.contentType(MediaType.APPLICATION_JSON_TYPE),
String
)

then:
def e = thrown(HttpClientResponseException)
e.response.code() == HttpStatus.BAD_REQUEST.code

when:
def result = new JsonSlurper().parseText((String) e.response.getBody().get())

then:
result._embedded.errors[0].message == 'pojo: Email and Name can not be identical'

cleanup:
server.close()
}

def "test validated controller validates @Valid classes with standard embedded errors"() {
given:
ApplicationContext context = ApplicationContext.run([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class PojoValidatorSpec extends Specification {

then:
ex = thrown(ConstraintViolationException)
ex.constraintViolations.size() == 1
ex.constraintViolations.size() == 2
}

void "test don't cascade to iterable without @Valid"() {
Expand Down

0 comments on commit 9a033e1

Please sign in to comment.