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

Only generated response status code when statusCode > 0 #266

Merged
merged 1 commit into from
Aug 8, 2023
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 @@ -379,13 +379,13 @@ public TypeMirror returnType() {
}

public int statusCode() {
if (statusCode > 0) {
if (statusCode != 0) {
// using explicit status code
return statusCode;
}
return producesAnnotation
.map(ProducesPrism::statusCode)
.filter(s -> s > 0)
.filter(s -> s != 0)
.orElseGet(() -> webMethod.statusCode(isVoid));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,9 @@ private void writeContextReturn() {
final var produces = producesOp.map(MediaType::parse).orElse(MediaType.APPLICATION_JSON);
final var contentTypeString = " res.headers().contentType(HttpMediaType.";
switch (produces) {
case APPLICATION_JSON -> writer.append(contentTypeString + "APPLICATION_JSON);").eol();
case TEXT_HTML -> writer.append(contentTypeString + "TEXT_HTML);").eol();
case TEXT_PLAIN -> writer.append(contentTypeString + "TEXT_PLAIN);").eol();
case APPLICATION_JSON -> writer.append(contentTypeString).append("APPLICATION_JSON);").eol();
case TEXT_HTML -> writer.append(contentTypeString).append("TEXT_HTML);").eol();
case TEXT_PLAIN -> writer.append(contentTypeString).append("TEXT_PLAIN);").eol();
case UNKNOWN -> writer.append(contentTypeString + "create(\"%s\"));", producesOp.orElse("UNKNOWN")).eol();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ private void writeMethod(final String fullPath) {
writer.append(" app.%s(\"%s\", ctx -> {", webMethod.name().toLowerCase(), fullPath).eol();
}
if (!customMethod) {
writer.append(" ctx.status(%d);", method.statusCode()).eol();
int statusCode = method.statusCode();
if (statusCode > 0) {
writer.append(" ctx.status(%d);", statusCode).eol();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ void write(boolean requestScoped) {
final String fullPath = segments.fullPath();

writer.append(" routing.%s(\"%s\", ctx -> {", webMethod.name().toLowerCase(), fullPath).eol();
writer.append(" ctx.status(%d);", method.statusCode()).eol();
int statusCode = method.statusCode();
if (statusCode > 0) {
writer.append(" ctx.status(%d);", statusCode).eol();
}

List<PathSegments.Segment> matrixSegments = segments.matrixSegments();
for (PathSegments.Segment matrixSegment : matrixSegments) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,11 @@ String getWithMatrixParam(int year, String author, String country, String other,
String slashAccepting(String name, String nam0, String nam1) {
return "got name:" + name + " splat0:" + nam0 + " splat1:" + nam1;
}

@Produces(value = "text/plain")
@Get("controlStatusCode")
String controlStatusCode(Context ctx) {
ctx.status(201);
return "controlStatusCode";
}
}
23 changes: 22 additions & 1 deletion tests/test-javalin-jsonb/src/main/resources/public/openapi.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"openapi" : "3.0.1",
"info" : {
"title" : "Example service showing off the Path extension method of controller",
"title" : "Example service",
"description" : "Example Javalin controllers with Java and Maven",
"version" : ""
},
Expand Down Expand Up @@ -348,6 +348,27 @@
}
}
},
"/hello/controlStatusCode" : {
"get" : {
"tags" : [

],
"summary" : "",
"description" : "",
"responses" : {
"200" : {
"description" : "",
"content" : {
"text/plain" : {
"schema" : {
"type" : "string"
}
}
}
}
}
}
},
"/hello/findbyname/{name}" : {
"get" : {
"tags" : [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,4 +321,16 @@ void get_slashAcceptingPath_expect200() {
assertThat(hres.statusCode()).isEqualTo(200);
assertEquals("got name:one splat0:a/b splat1:x/y/z", hres.body());
}

@Test
void get_controlStatusCode_expect201() {
final HttpResponse<String> hres = client.request()
.path("hello/controlStatusCode")
.GET()
.asString();

assertThat(hres.statusCode()).isEqualTo(201);
assertEquals("controlStatusCode", hres.body());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ Map<String, Object> strBody2() {
return map;
}

@Produces(statusCode = -1) // Can use -1 to programmatically set the ServerResponse statusCode
@Post("/strBody3")
Map<String, Object> strBody3(ServerResponse res) {
res.status(200);
return Map.of("hi", "strBody3");
}

@ExceptionHandler
String exception(IllegalArgumentException ex) {
return "Err: " + ex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ void strBody() {
assertThat(res.headers().firstValue("Content-Length")).isPresent();
}

@Test
void strBody3() {
HttpResponse<String> res = client.request()
.path("test/strBody3")
.body("{\"key\":42}")
.POST()
.asString();

assertThat(res.statusCode()).isEqualTo(200);
assertThat(res.body()).isEqualTo("{\"hi\":\"strBody3\"}");
assertThat(res.headers().firstValue("Content-Type")).isPresent().get().isEqualTo("application/json");
assertThat(res.headers().firstValue("Content-Length")).isPresent();
}

@Test
void blah() {
HttpResponse<String> res = client.request()
Expand Down