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

fix Bean node unnamed (#298) #300

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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":"[email protected]","name":"[email protected]"}')
.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
Loading