Search in sources :

Example 1 with Entity

use of com.webcohesion.enunciate.api.resources.Entity 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 "JavaScriptObject";
                }
            }
        }
    }
    if (unwrapped instanceof Entity) {
        return "JavaScriptObject";
    }
    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 2 with Entity

use of com.webcohesion.enunciate.api.resources.Entity in project enunciate by stoicflame.

the class JsonExamplesForMethod method exec.

public Object exec(List list) throws TemplateModelException {
    if (list.size() < 1) {
        throw new TemplateModelException("The uniqueMediaTypesFor method must have a parameter.");
    }
    TemplateModel from = (TemplateModel) list.get(0);
    Object unwrapped = FreemarkerUtil.unwrap(from);
    HashMap<String, String> uniqueMediaTypes = new HashMap<String, String>();
    if (unwrapped instanceof Entity) {
        Entity entity = (Entity) unwrapped;
        List<? extends MediaTypeDescriptor> mts = entity.getMediaTypes();
        if (mts != null) {
            for (MediaTypeDescriptor mt : mts) {
                if (mt.getMediaType().endsWith("json")) {
                    Example example = mt.getExample();
                    if (example != null) {
                        String body = example.getBody();
                        if (body != null) {
                            uniqueMediaTypes.put(mt.getMediaType(), body);
                        }
                    }
                }
            }
        }
    }
    return uniqueMediaTypes;
}
Also used : MediaTypeDescriptor(com.webcohesion.enunciate.api.resources.MediaTypeDescriptor) TemplateModelException(freemarker.template.TemplateModelException) Entity(com.webcohesion.enunciate.api.resources.Entity) HashMap(java.util.HashMap) Example(com.webcohesion.enunciate.api.datatype.Example) TemplateModel(freemarker.template.TemplateModel)

Example 3 with Entity

use of com.webcohesion.enunciate.api.resources.Entity 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 4 with Entity

use of com.webcohesion.enunciate.api.resources.Entity in project enunciate by stoicflame.

the class MediaTypeForMethod method exec.

/**
 * Returns the qname of the element that has the first parameter as the namespace, the second as the element.
 *
 * @param list The arguments.
 * @return The qname.
 */
public Object exec(List list) throws TemplateModelException {
    if (list.size() < 1) {
        throw new TemplateModelException("The MediaTypeForMethod must have a entity as a parameter.");
    }
    TemplateModel from = (TemplateModel) list.get(0);
    Object unwrapped = DeepUnwrap.unwrap(from);
    if (unwrapped instanceof Entity) {
        List<? extends MediaTypeDescriptor> mediaTypes = ((Entity) unwrapped).getMediaTypes();
        if (mediaTypes != null && !mediaTypes.isEmpty()) {
            for (MediaTypeDescriptor mediaType : mediaTypes) {
                if (mediaType.getMediaType().contains("json")) {
                    return mediaType.getMediaType();
                }
            }
        }
    }
    return "application/json";
}
Also used : MediaTypeDescriptor(com.webcohesion.enunciate.api.resources.MediaTypeDescriptor) TemplateModelException(freemarker.template.TemplateModelException) Entity(com.webcohesion.enunciate.api.resources.Entity) TemplateModel(freemarker.template.TemplateModel)

Example 5 with Entity

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

Aggregations

Entity (com.webcohesion.enunciate.api.resources.Entity)8 MediaTypeDescriptor (com.webcohesion.enunciate.api.resources.MediaTypeDescriptor)6 TemplateModel (freemarker.template.TemplateModel)6 TemplateModelException (freemarker.template.TemplateModelException)6 DataTypeReference (com.webcohesion.enunciate.api.datatype.DataTypeReference)3 HashMap (java.util.HashMap)2 Example (com.webcohesion.enunciate.api.datatype.Example)1 Method (com.webcohesion.enunciate.api.resources.Method)1 StatusCode (com.webcohesion.enunciate.api.resources.StatusCode)1 ArrayList (java.util.ArrayList)1