use of org.apache.camel.model.rest.RestOperationResponseMsgDefinition in project camel by apache.
the class RestSwaggerReader method parse.
private void parse(Swagger swagger, RestDefinition rest, String camelContextId, ClassResolver classResolver) {
List<VerbDefinition> verbs = new ArrayList<>(rest.getVerbs());
// must sort the verbs by uri so we group them together when an uri has multiple operations
Collections.sort(verbs, new VerbOrdering());
// we need to group the operations within the same tag, so use the path as default if not configured
String pathAsTag = rest.getTag() != null ? rest.getTag() : FileUtil.stripLeadingSeparator(rest.getPath());
String summary = rest.getDescriptionText();
if (ObjectHelper.isNotEmpty(pathAsTag)) {
// add rest as tag
Tag tag = new Tag();
tag.description(summary);
tag.name(pathAsTag);
swagger.addTag(tag);
}
// gather all types in use
Set<String> types = new LinkedHashSet<>();
for (VerbDefinition verb : verbs) {
// check if the Verb Definition must be excluded from documentation
Boolean apiDocs;
if (verb.getApiDocs() != null) {
apiDocs = verb.getApiDocs();
} else {
// fallback to option on rest
apiDocs = rest.getApiDocs();
}
if (apiDocs != null && !apiDocs) {
continue;
}
String type = verb.getType();
if (ObjectHelper.isNotEmpty(type)) {
if (type.endsWith("[]")) {
type = type.substring(0, type.length() - 2);
}
types.add(type);
}
type = verb.getOutType();
if (ObjectHelper.isNotEmpty(type)) {
if (type.endsWith("[]")) {
type = type.substring(0, type.length() - 2);
}
types.add(type);
}
// there can also be types in response messages
if (verb.getResponseMsgs() != null) {
for (RestOperationResponseMsgDefinition def : verb.getResponseMsgs()) {
type = def.getResponseModel();
if (ObjectHelper.isNotEmpty(type)) {
if (type.endsWith("[]")) {
type = type.substring(0, type.length() - 2);
}
types.add(type);
}
}
}
}
// use annotation scanner to find models (annotated classes)
for (String type : types) {
Class<?> clazz = classResolver.resolveClass(type);
appendModels(clazz, swagger);
}
doParseVerbs(swagger, rest, camelContextId, verbs, pathAsTag);
}
use of org.apache.camel.model.rest.RestOperationResponseMsgDefinition in project camel by apache.
the class RestSwaggerReader method doParseResponseMessages.
private void doParseResponseMessages(Swagger swagger, VerbDefinition verb, Operation op) {
for (RestOperationResponseMsgDefinition msg : verb.getResponseMsgs()) {
Response response = null;
if (op.getResponses() != null) {
response = op.getResponses().get(msg.getCode());
}
if (response == null) {
response = new Response();
}
if (ObjectHelper.isNotEmpty(msg.getResponseModel())) {
Property prop = modelTypeAsProperty(msg.getResponseModel(), swagger);
response.setSchema(prop);
}
response.setDescription(msg.getMessage());
// add headers
if (msg.getHeaders() != null) {
for (RestOperationResponseHeaderDefinition header : msg.getHeaders()) {
String name = header.getName();
String type = header.getDataType();
if ("string".equals(type)) {
StringProperty sp = new StringProperty();
sp.setName(name);
sp.setDescription(header.getDescription());
if (header.getAllowableValues() != null) {
sp.setEnum(header.getAllowableValues());
}
response.addHeader(name, sp);
} else if ("int".equals(type) || "integer".equals(type)) {
IntegerProperty ip = new IntegerProperty();
ip.setName(name);
ip.setDescription(header.getDescription());
List<Integer> values;
if (!header.getAllowableValues().isEmpty()) {
values = new ArrayList<Integer>();
for (String text : header.getAllowableValues()) {
values.add(Integer.valueOf(text));
}
ip.setEnum(values);
}
response.addHeader(name, ip);
} else if ("long".equals(type)) {
LongProperty lp = new LongProperty();
lp.setName(name);
lp.setDescription(header.getDescription());
List<Long> values;
if (!header.getAllowableValues().isEmpty()) {
values = new ArrayList<Long>();
for (String text : header.getAllowableValues()) {
values.add(Long.valueOf(text));
}
lp.setEnum(values);
}
response.addHeader(name, lp);
} else if ("float".equals(type)) {
FloatProperty lp = new FloatProperty();
lp.setName(name);
lp.setDescription(header.getDescription());
List<Float> values;
if (!header.getAllowableValues().isEmpty()) {
values = new ArrayList<Float>();
for (String text : header.getAllowableValues()) {
values.add(Float.valueOf(text));
}
lp.setEnum(values);
}
response.addHeader(name, lp);
} else if ("double".equals(type)) {
DoubleProperty dp = new DoubleProperty();
dp.setName(name);
dp.setDescription(header.getDescription());
List<Double> values;
if (!header.getAllowableValues().isEmpty()) {
values = new ArrayList<Double>();
for (String text : header.getAllowableValues()) {
values.add(Double.valueOf(text));
}
dp.setEnum(values);
}
response.addHeader(name, dp);
} else if ("boolean".equals(type)) {
BooleanProperty bp = new BooleanProperty();
bp.setName(name);
bp.setDescription(header.getDescription());
response.addHeader(name, bp);
} else if ("array".equals(type)) {
ArrayProperty ap = new ArrayProperty();
ap.setName(name);
ap.setDescription(header.getDescription());
if (header.getArrayType() != null) {
if (header.getArrayType().equalsIgnoreCase("string")) {
ap.setItems(new StringProperty());
}
if (header.getArrayType().equalsIgnoreCase("int") || header.getArrayType().equalsIgnoreCase("integer")) {
ap.setItems(new IntegerProperty());
}
if (header.getArrayType().equalsIgnoreCase("long")) {
ap.setItems(new LongProperty());
}
if (header.getArrayType().equalsIgnoreCase("float")) {
ap.setItems(new FloatProperty());
}
if (header.getArrayType().equalsIgnoreCase("double")) {
ap.setItems(new DoubleProperty());
}
if (header.getArrayType().equalsIgnoreCase("boolean")) {
ap.setItems(new BooleanProperty());
}
}
response.addHeader(name, ap);
}
}
}
op.addResponse(msg.getCode(), response);
}
}
Aggregations