Skip to content

Commit c9f3c7b

Browse files
committed
Match void / no content response and swagger
1 parent ef59854 commit c9f3c7b

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

src/main/java/io/dinject/javalin/generator/MethodReader.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class MethodReader {
5959
this.bean = bean;
6060
this.beanPath = bean.getPath();
6161
this.element = element;
62-
this.isVoid = element.getReturnType().toString().equals("void");
62+
this.isVoid = element.getReturnType().getKind() == TypeKind.VOID;
6363
this.methodRoles = Util.findRoles(element);
6464
this.javadoc = Javadoc.parse(ctx.getDocComment(element));
6565
this.produces = produces(bean);
@@ -142,11 +142,11 @@ void addMeta(ProcessingContext ctx) {
142142
response.setDescription(javadoc.getReturnDescription());
143143

144144
TypeMirror returnType = element.getReturnType();
145-
if (isNoResponse(returnType)) {
145+
if (isVoid) {
146146
if (isEmpty(response.getDescription())) {
147147
response.setDescription("No content");
148148
}
149-
responses.addApiResponse("204", response);
149+
responses.addApiResponse(Integer.toString(httpStatusCode()), response);
150150
} else {
151151
responses.addApiResponse(ApiResponses.DEFAULT, response);
152152
String contentMediaType = (produces == null) ? MediaType.APPLICATION_JSON : produces;
@@ -159,10 +159,6 @@ private boolean isEmpty(String value) {
159159
return value == null || value.isEmpty();
160160
}
161161

162-
private boolean isNoResponse(TypeMirror returnType) {
163-
return returnType.getKind() == TypeKind.VOID;
164-
}
165-
166162
/**
167163
* Return true if the method is included in documentation.
168164
*/
@@ -241,7 +237,7 @@ private List<String> roles() {
241237
}
242238

243239
private int httpStatusCode() {
244-
return webMethod.statusCode();
240+
return webMethod.statusCode(isVoid);
245241
}
246242

247243
private boolean isReturnContent() {

src/main/java/io/dinject/javalin/generator/WebMethod.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,25 @@
22

33
enum WebMethod {
44
GET(200),
5-
PUT(200),
65
POST(201),
7-
PATCH(200),
8-
DELETE(200);
6+
PUT(200, 204),
7+
PATCH(200, 204),
8+
DELETE(200, 204);
99

1010
private int statusCode;
11+
private int voidStatusCode;
12+
13+
WebMethod(int statusCode, int voidStatusCode) {
14+
this.statusCode = statusCode;
15+
this.voidStatusCode = voidStatusCode;
16+
}
1117

1218
WebMethod(int statusCode) {
1319
this.statusCode = statusCode;
20+
this.voidStatusCode = statusCode;
1421
}
1522

16-
int statusCode() {
17-
return statusCode;
23+
int statusCode(boolean isVoid) {
24+
return isVoid ? voidStatusCode : statusCode;
1825
}
1926
}

0 commit comments

Comments
 (0)