Search in sources :

Example 6 with DataTypeReference

use of com.webcohesion.enunciate.api.datatype.DataTypeReference 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)

Example 7 with DataTypeReference

use of com.webcohesion.enunciate.api.datatype.DataTypeReference in project enunciate by stoicflame.

the class SimpleNameForMethod method simpleNameFor.

@Override
public String simpleNameFor(Object unwrapped, boolean noParams) throws TemplateModelException {
    if (unwrapped instanceof Entity) {
        List<? extends MediaTypeDescriptor> mediaTypes = ((Entity) unwrapped).getMediaTypes();
        for (MediaTypeDescriptor mediaType : mediaTypes) {
            if (this.jsonContext.getLabel().equals(mediaType.getSyntax())) {
                DataTypeReference dataType = mediaType.getDataType();
                unwrapped = this.jsonContext.findType(dataType);
                if (unwrapped == null) {
                    return "Object";
                }
            }
        }
    }
    if (unwrapped instanceof Entity) {
        return "Object";
    }
    return super.simpleNameFor(unwrapped, noParams);
}
Also used : MediaTypeDescriptor(com.webcohesion.enunciate.api.resources.MediaTypeDescriptor) Entity(com.webcohesion.enunciate.api.resources.Entity) DataTypeReference(com.webcohesion.enunciate.api.datatype.DataTypeReference)

Example 8 with DataTypeReference

use of com.webcohesion.enunciate.api.datatype.DataTypeReference in project enunciate by stoicflame.

the class ReferencedDatatypeNameForMethod method exec.

@SuppressWarnings("rawtypes")
public Object exec(List list) throws TemplateModelException {
    if (list.size() < 1) {
        throw new TemplateModelException("The datatypeNameFor method must have a parameter.");
    }
    TemplateModel from = (TemplateModel) list.get(0);
    Object unwrapped = FreemarkerUtil.unwrap(from);
    if (!DataTypeReference.class.isAssignableFrom(unwrapped.getClass())) {
        throw new TemplateModelException("No referenced data type name for: " + unwrapped);
    }
    DataTypeReference reference = DataTypeReference.class.cast(unwrapped);
    BaseType baseType = reference.getBaseType();
    String format = reference.getBaseTypeFormat();
    String defaultType = "file";
    if (list.size() > 1) {
        defaultType = FreemarkerUtil.unwrap((TemplateModel) list.get(1)).toString();
    }
    switch(baseType) {
        case bool:
            return "boolean";
        case number:
            if ("int32".equals(format) || "int64".equals(format)) {
                return "integer";
            } else {
                return "number";
            }
        case string:
            return "string";
        default:
            return defaultType;
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) DataTypeReference(com.webcohesion.enunciate.api.datatype.DataTypeReference) BaseType(com.webcohesion.enunciate.api.datatype.BaseType) TemplateModel(freemarker.template.TemplateModel)

Aggregations

DataTypeReference (com.webcohesion.enunciate.api.datatype.DataTypeReference)8 TemplateModel (freemarker.template.TemplateModel)5 TemplateModelException (freemarker.template.TemplateModelException)5 MediaTypeDescriptor (com.webcohesion.enunciate.api.resources.MediaTypeDescriptor)4 Entity (com.webcohesion.enunciate.api.resources.Entity)3 DataType (com.webcohesion.enunciate.api.datatype.DataType)2 BaseType (com.webcohesion.enunciate.api.datatype.BaseType)1 Method (com.webcohesion.enunciate.api.resources.Method)1 StatusCode (com.webcohesion.enunciate.api.resources.StatusCode)1 DataTypeReferenceImpl (com.webcohesion.enunciate.modules.jaxb.api.impl.DataTypeReferenceImpl)1 TypeDefinition (com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)1 XmlType (com.webcohesion.enunciate.modules.jaxb.model.types.XmlType)1