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);
}
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);
}
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;
}
}
Aggregations