Skip to content

Commit 03e768a

Browse files
committed
fixes #15
automatically enhance StatusCodes
1 parent 5865883 commit 03e768a

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

src/main/java/org/restdoc/server/impl/RestDocGenerator.java

+22-11
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import javax.ws.rs.Path;
3838
import javax.ws.rs.PathParam;
3939
import javax.ws.rs.QueryParam;
40+
import javax.ws.rs.core.Response.Status;
4041

4142
import org.restdoc.annotations.RestDocAccept;
4243
import org.restdoc.annotations.RestDocHeader;
@@ -239,17 +240,19 @@ private void addResourceMethod(final String basepath, final Method method) {
239240
this.ext.newResource(restResource);
240241
}
241242

242-
if (restResource.getMethods().containsKey(methodType)) {
243-
this.logger.warn("Duplicate method detected for resource: " + path + " -> " + methodType);
244-
return;
243+
final MethodDefinition def;
244+
if (!restResource.getMethods().containsKey(methodType)) {
245+
def = new MethodDefinition();
246+
def.setDescription(methodDescription);
247+
def.setResponse(new ResponseDefinition());
248+
} else {
249+
def = restResource.getMethods().get(methodType);
245250
}
246251

247-
final MethodDefinition def = new MethodDefinition();
248-
def.setDescription(methodDescription);
249252
def.getHeaders().putAll(methodRequestHeader);
250253
def.getAccepts().addAll(this.getAccepts(method, parameterTypes, parameterAnnotations));
251254
def.getStatusCodes().putAll(this.getStatusCodes(method));
252-
def.setResponse(this.getMethodResponse(method));
255+
this.addMethodResponse(def.getResponse(), method);
253256

254257
restResource.getMethods().put(methodType, def);
255258

@@ -329,17 +332,19 @@ protected void parseMethodParameter(final List<String> queryParams, final Map<St
329332
}
330333
}
331334

332-
private ResponseDefinition getMethodResponse(final Method method) {
333-
final ResponseDefinition def = new ResponseDefinition();
335+
private void addMethodResponse(final ResponseDefinition def, final Method method) {
336+
boolean typeFound = false;
334337
if (method.isAnnotationPresent(RestDocResponse.class)) {
335338
final RestDocResponse docResponse = method.getAnnotation(RestDocResponse.class);
336339
final RestDocType[] types = docResponse.types();
337340
for (final RestDocType restDocType : types) {
338341
if (!restDocType.schemaClass().equals(Object.class)) {
339342
final String schema = SchemaResolver.getSchemaFromType(restDocType.schemaClass(), this.schemaMap, this.ext);
340343
def.type(restDocType.type(), schema);
344+
typeFound = true;
341345
} else {
342346
def.type(restDocType.type(), restDocType.schema());
347+
typeFound = true;
343348
}
344349
}
345350

@@ -348,7 +353,7 @@ private ResponseDefinition getMethodResponse(final Method method) {
348353
def.header(restDocHeader.name(), restDocHeader.description(), restDocHeader.required());
349354
}
350355
}
351-
if (def.getTypes().isEmpty() && !method.getReturnType().equals(Void.TYPE)) {
356+
if (!typeFound && !method.getReturnType().equals(Void.TYPE)) {
352357
final String schema = SchemaResolver.getSchemaFromTypeOrNull(method.getGenericReturnType(), this.schemaMap, this.ext);
353358
String[] mediaTypes = MediaTypeResolver.getProducesMediaType(method);
354359
if (mediaTypes != null) {
@@ -357,17 +362,23 @@ private ResponseDefinition getMethodResponse(final Method method) {
357362
}
358363
}
359364
}
360-
return def;
361365
}
362366

363367
private Map<String, String> getStatusCodes(final Method method) {
364368
final Map<String, String> codeMap = Maps.newHashMap();
365369
if (method.isAnnotationPresent(RestDocReturnCodes.class)) {
366370
final RestDocReturnCode[] returnCodes = method.getAnnotation(RestDocReturnCodes.class).value();
367371
for (final RestDocReturnCode rdrc : returnCodes) {
368-
codeMap.put(rdrc.code(), rdrc.description());
372+
String description = rdrc.description();
373+
if (description.isEmpty()) {
374+
description = Status.fromStatusCode(Integer.valueOf(rdrc.code())).getReasonPhrase();
375+
}
376+
codeMap.put(rdrc.code(), description);
369377
}
370378
}
379+
if (method.getReturnType().equals(Void.TYPE)) {
380+
codeMap.put(String.valueOf(Status.NO_CONTENT.getStatusCode()), Status.NO_CONTENT.getReasonPhrase());
381+
}
371382
return codeMap;
372383
}
373384

src/test/java/org/restdoc/server/impl/MyRSBean.java

+22-7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import javax.ws.rs.Consumes;
2020
import javax.ws.rs.GET;
2121
import javax.ws.rs.POST;
22+
import javax.ws.rs.PUT;
2223
import javax.ws.rs.Path;
2324
import javax.ws.rs.PathParam;
2425
import javax.ws.rs.Produces;
@@ -96,29 +97,42 @@ public List<Msg> getMessageListJson() {
9697
@RestDoc(id = "message", resourceDescription = "A single message", methodDescription = "Read the message content")
9798
@RestDocReturnCodes({@RestDocReturnCode(code = "200", description = "All went well"), @RestDocReturnCode(code = "403", description = "Access not allowed"), @RestDocReturnCode(code = "404", description = "Message not found")})
9899
@Produces("text/plain")
99-
@Consumes("text/plain")
100100
public String getMessage(@PathParam("id") @RestDocParam(description = "The message id") final String id) {
101101
if (this.messages.containsKey(id)) {
102102
return this.messages.get(id);
103103
}
104104
throw new WebApplicationException(Status.NOT_FOUND);
105105
}
106106

107+
/**
108+
* @param id
109+
* @return the message
110+
*/
111+
@Path("/{id}")
112+
@GET
113+
@RestDoc(id = "message", resourceDescription = "A single message", methodDescription = "Read the message content")
114+
@RestDocReturnCodes({@RestDocReturnCode(code = "200", description = "All went well"), @RestDocReturnCode(code = "403", description = "Access not allowed"), @RestDocReturnCode(code = "404", description = "Message not found")})
115+
@Produces("application/json")
116+
public String getMessageJSON(@PathParam("id") @RestDocParam(description = "The message id") final String id) {
117+
if (this.messages.containsKey(id)) {
118+
return this.messages.get(id);
119+
}
120+
throw new WebApplicationException(Status.NOT_FOUND);
121+
}
122+
107123
/**
108124
* @param id
109125
* @param content
110126
* @return the created message
111127
*/
112128
@Path("/{id}")
113-
@POST
129+
@PUT
114130
@RestDoc(methodDescription = "Update the message content")
115-
@RestDocReturnCodes({@RestDocReturnCode(code = "200", description = "All went well"), @RestDocReturnCode(code = "403", description = "Access not allowed")})
116-
@Produces("text/plain")
131+
@RestDocReturnCodes({@RestDocReturnCode(code = "403", description = "")})
117132
@Consumes("text/plain")
118133
@Scopes("write")
119-
public String setMessage(@PathParam("id") final String id, final String content) {
134+
public void setMessage(@PathParam("id") final String id, final String content) {
120135
this.messages.put(id, content);
121-
return content;
122136
}
123137

124138
/**
@@ -133,7 +147,8 @@ public String setMessage(@PathParam("id") final String id, final String content)
133147
@Consumes("application/json")
134148
@SuppressWarnings("unused")
135149
public String setMessage(final Msg msg, @QueryParam("X-TTL") @RestDocParam(description = "The message lifetime") final Long lifetime) {
136-
return this.setMessage(msg.getId(), msg.getContent());
150+
this.setMessage(msg.getId(), msg.getContent());
151+
return msg.getContent();
137152
}
138153

139154
}

0 commit comments

Comments
 (0)