Search in sources :

Example 1 with StatusCode

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);
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) Entity(com.webcohesion.enunciate.api.resources.Entity) DataTypeReference(com.webcohesion.enunciate.api.datatype.DataTypeReference) TemplateModel(freemarker.template.TemplateModel) Method(com.webcohesion.enunciate.api.resources.Method) StatusCode(com.webcohesion.enunciate.api.resources.StatusCode)

Aggregations

DataTypeReference (com.webcohesion.enunciate.api.datatype.DataTypeReference)1 Entity (com.webcohesion.enunciate.api.resources.Entity)1 Method (com.webcohesion.enunciate.api.resources.Method)1 StatusCode (com.webcohesion.enunciate.api.resources.StatusCode)1 TemplateModel (freemarker.template.TemplateModel)1 TemplateModelException (freemarker.template.TemplateModelException)1