-
Notifications
You must be signed in to change notification settings - Fork 12
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
ConstraintViolationException HTTP status should be configurable #313
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.tietoevry.quarkus.resteasy.problem; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.quarkus.test.junit.QuarkusTestProfile; | ||
import io.quarkus.test.junit.TestProfile; | ||
import io.restassured.RestAssured; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Map; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.Matchers.hasSize; | ||
|
||
@QuarkusTest | ||
@TestProfile(ConstraintViolationMapperConfigIT.CustomHttpStatus.class) | ||
class ConstraintViolationMapperConfigIT { | ||
|
||
static { | ||
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(); | ||
} | ||
|
||
@Test | ||
void constraintViolationShouldProvideErrorDetails() { | ||
given() | ||
.body("{\"phraseName\": 10 }") | ||
.contentType(APPLICATION_JSON) | ||
.post("/throw/validation/constraint-violation-exception") | ||
.then() | ||
.statusCode(422) | ||
.body("title", equalTo("Constraint violation")) | ||
.body("status", equalTo(422)) | ||
.body("violations", hasSize(1)); | ||
} | ||
|
||
public static class CustomHttpStatus implements QuarkusTestProfile { | ||
|
||
@Override | ||
public Map<String, String> getConfigOverrides() { | ||
return Map.of( | ||
"quarkus.resteasy.problem.constraint-violation.status", "422", | ||
"quarkus.resteasy.problem.constraint-violation.title", "Constraint violation" | ||
); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.tietoevry.quarkus.resteasy.problem; | ||
|
||
import io.quarkus.test.junit.QuarkusIntegrationTest; | ||
|
||
@QuarkusIntegrationTest | ||
class ConstraintViolationMapperConfigNativeIT extends ConstraintViolationMapperConfigIT { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.tietoevry.quarkus.resteasy.problem; | ||
|
||
import io.quarkus.runtime.annotations.ConfigPhase; | ||
import io.quarkus.runtime.annotations.ConfigRoot; | ||
import io.smallrye.config.ConfigMapping; | ||
import io.smallrye.config.WithDefault; | ||
import io.smallrye.config.WithName; | ||
|
||
@ConfigMapping(prefix = "quarkus.resteasy.problem") | ||
@ConfigRoot(phase = ConfigPhase.RUN_TIME) | ||
public interface ProblemRuntimeConfig { | ||
|
||
/** | ||
* Config for ConstraintViolationException mapper | ||
*/ | ||
@WithName("constraint-violation") | ||
ConstraintViolationMapperConfig constraintViolation(); | ||
|
||
interface ConstraintViolationMapperConfig { | ||
static ConstraintViolationMapperConfig defaults() { | ||
return new ConstraintViolationMapperConfig() { | ||
@Override | ||
public int status() { | ||
return 400; | ||
} | ||
|
||
@Override | ||
public String title() { | ||
return "Bad Request"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it's worth to define a constant to keep the default values aligned. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I didn't like this defaults() method that much from the very beginning, will try to simplify this. |
||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Response status code when ConstraintViolationException is thrown. | ||
*/ | ||
@WithDefault("400") | ||
int status(); | ||
|
||
/** | ||
* Response title when ConstraintViolationException is thrown. | ||
*/ | ||
@WithDefault("Bad Request") | ||
String title(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's worth to mention that this extension uses
org.slf4j
instead ofjboss logging
. Thus a user has to pick the appropriated MDC ...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. On the other hand, quick tests show that setting properties via
org.jboss.logging.MDC
just works, jboss probably propagates everything to slf4j (or is a service provider etc). I'll double check that in real app though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah cool, wasn't aware of that. Thanks for pointing this out.