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