use of com.webcohesion.enunciate.api.resources.StatusCode in project enunciate by stoicflame.
the class ResponsesOfMethod method exec.
public Object exec(List list) throws TemplateModelException {
if (list.size() < 1) {
throw new TemplateModelException("The responsesOf method must have a parameter.");
}
TemplateModel from = (TemplateModel) list.get(0);
Object unwrapped = FreemarkerUtil.unwrap(from);
if (unwrapped instanceof Method) {
Method method = (Method) unwrapped;
TreeSet<SwaggerResponse> responses = new TreeSet<>(Comparator.comparingInt(SwaggerResponse::getCode));
List<? extends Parameter> successHeaders = method.getResponseHeaders();
Entity responseEntity = method.getResponseEntity();
DataTypeReference successDataType = FindBestDataTypeMethod.findBestDataType(responseEntity);
boolean successResponseFound = false;
if (method.getResponseCodes() != null) {
for (StatusCode code : method.getResponseCodes()) {
boolean successResponse = code.getCode() >= 200 && code.getCode() < 300;
DataTypeReference dataType = FindBestDataTypeMethod.findBestDataType(code.getMediaTypes());
dataType = dataType == null && successResponse ? successDataType : dataType;
List<? extends Parameter> headers = successResponse ? successHeaders : Collections.<Parameter>emptyList();
responses.add(new SwaggerResponse(code.getCode(), dataType, headers, code.getCondition()));
successResponseFound |= successResponse;
}
}
if (!successResponseFound) {
int code = DEFAULT_201_METHODS.contains(method.getHttpMethod().toUpperCase()) ? 201 : DEFAULT_204_METHODS.contains(method.getHttpMethod().toUpperCase()) ? 204 : 200;
String description = responseEntity != null ? responseEntity.getDescription() : "Success";
responses.add(new SwaggerResponse(code, successDataType, successHeaders, description));
}
return responses;
}
throw new TemplateModelException("No responses for: " + unwrapped);
}
Aggregations