Skip to content

Commit

Permalink
Fix #945
Browse files Browse the repository at this point in the history
  • Loading branch information
mgubaidullin committed Oct 23, 2023
1 parent 96c395e commit 3583d1e
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 44 deletions.
5 changes: 5 additions & 0 deletions karavan-core/src/core/api/CamelDefinitionApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,11 @@ export class CamelDefinitionApi {
if (element?.errorHandler !== undefined) {
def.errorHandler = CamelDefinitionApi.createErrorHandlerDefinition(element.errorHandler);
}
def.intercept = element && element?.intercept ? element?.intercept.map((x:any) => CamelDefinitionApi.createInterceptDefinition(x)) :[];
def.interceptFrom = element && element?.interceptFrom ? element?.interceptFrom.map((x:any) => CamelDefinitionApi.createInterceptFromDefinition(x)) :[];
def.interceptSendToEndpoint = element && element?.interceptSendToEndpoint ? element?.interceptSendToEndpoint.map((x:any) => CamelDefinitionApi.createInterceptSendToEndpointDefinition(x)) :[];
def.onException = element && element?.onException ? element?.onException.map((x:any) => CamelDefinitionApi.createOnExceptionDefinition(x)) :[];
def.onCompletion = element && element?.onCompletion ? element?.onCompletion.map((x:any) => CamelDefinitionApi.createOnCompletionDefinition(x)) :[];
return def;
}

Expand Down
5 changes: 5 additions & 0 deletions karavan-core/src/core/api/CamelDefinitionYamlStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1936,13 +1936,18 @@ export class CamelDefinitionYamlStep {
static readRouteConfigurationDefinition = (element: any): RouteConfigurationDefinition => {

let def = element ? new RouteConfigurationDefinition({...element}) : new RouteConfigurationDefinition();
def.onCompletion = element && element?.onCompletion ? element?.onCompletion.map((x:any) => CamelDefinitionYamlStep.readOnCompletionDefinition(x.onCompletion)) :[];
def.interceptSendToEndpoint = element && element?.interceptSendToEndpoint ? element?.interceptSendToEndpoint.map((x:any) => CamelDefinitionYamlStep.readInterceptSendToEndpointDefinition(x.interceptSendToEndpoint)) :[];
def.intercept = element && element?.intercept ? element?.intercept.map((x:any) => CamelDefinitionYamlStep.readInterceptDefinition(x.intercept)) :[];
if (element?.errorHandler !== undefined) {
if (Array.isArray(element.errorHandler)) {
def.errorHandler = CamelDefinitionYamlStep.readErrorHandlerDefinition(element.errorHandler[0]);
} else {
def.errorHandler = CamelDefinitionYamlStep.readErrorHandlerDefinition(element.errorHandler);
}
}
def.onException = element && element?.onException ? element?.onException.map((x:any) => CamelDefinitionYamlStep.readOnExceptionDefinition(x.onException)) :[];
def.interceptFrom = element && element?.interceptFrom ? element?.interceptFrom.map((x:any) => CamelDefinitionYamlStep.readInterceptFromDefinition(x.interceptFrom)) :[];

return def;
}
Expand Down
10 changes: 5 additions & 5 deletions karavan-core/src/core/model/CamelMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1937,11 +1937,11 @@ export const CamelModelMetadata: ElementMeta[] = [
]),
new ElementMeta('routeConfiguration', 'RouteConfigurationDefinition', 'Route Configuration', "Reusable configuration for Camel route(s).", 'configuration', [
new PropertyMeta('errorHandler', 'Error Handler', "Sets the error handler to use, for routes that has not already been configured with an error handler.", 'ErrorHandlerDefinition', '', '', false, false, false, true, '', ''),
new PropertyMeta('intercept', 'Intercept', "Adds a route for an interceptor that intercepts every processing step.", 'object', '', '', false, false, true, true, '', ''),
new PropertyMeta('interceptFrom', 'Intercept From', "Adds a route for an interceptor that intercepts incoming messages on the given endpoint.", 'object', '', '', false, false, true, true, '', ''),
new PropertyMeta('interceptSendToEndpoint', 'Intercept Send To Endpoint', "Applies a route for an interceptor if an exchange is send to the given endpoint", 'object', '', '', false, false, true, true, '', ''),
new PropertyMeta('onException', 'On Exception', "Exception clause for catching certain exceptions and handling them.", 'object', '', '', false, false, true, true, '', ''),
new PropertyMeta('onCompletion', 'On Completion', "On completion callback for doing custom routing when the org.apache.camel.Exchange is complete.", 'object', '', '', false, false, true, true, '', ''),
new PropertyMeta('intercept', 'Intercept', "Adds a route for an interceptor that intercepts every processing step.", 'InterceptDefinition', '', '', false, false, true, true, '', ''),
new PropertyMeta('interceptFrom', 'Intercept From', "Adds a route for an interceptor that intercepts incoming messages on the given endpoint.", 'InterceptFromDefinition', '', '', false, false, true, true, '', ''),
new PropertyMeta('interceptSendToEndpoint', 'Intercept Send To Endpoint', "Applies a route for an interceptor if an exchange is send to the given endpoint", 'InterceptSendToEndpointDefinition', '', '', false, false, true, true, '', ''),
new PropertyMeta('onException', 'On Exception', "Exception clause for catching certain exceptions and handling them.", 'OnExceptionDefinition', '', '', false, false, true, true, '', ''),
new PropertyMeta('onCompletion', 'On Completion', "On completion callback for doing custom routing when the org.apache.camel.Exchange is complete.", 'OnCompletionDefinition', '', '', false, false, true, true, '', ''),
new PropertyMeta('precondition', 'Precondition', "The predicate of the precondition in simple language to evaluate in order to determine if this route configuration should be included or not.", 'string', '', '', false, false, false, false, 'advanced', ''),
new PropertyMeta('id', 'Id', "Sets the id of this node", 'string', '', '', false, false, false, false, '', ''),
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
public class AbstractGenerator {

Logger LOGGER = Logger.getLogger(AbstractGenerator.class.getName());
protected static boolean print = false;

protected Vertx vertx = Vertx.vertx();

Expand Down Expand Up @@ -117,9 +118,8 @@ private Map<String, JsonObject> getJsonObjectProperties (JsonObject val) {
return properties;
}

protected Map<String, JsonObject> getClassProperties (JsonObject obj) {
protected Map<String, JsonObject> getClassProperties (String stepName, JsonObject obj) {
Map<String, JsonObject> properties = new LinkedHashMap<>();

obj.getMap().keySet().forEach(key -> {
if (key.equals("oneOf")) {
JsonObject val = obj.getJsonArray("oneOf").getJsonObject(1).getJsonObject("properties");
Expand Down Expand Up @@ -320,8 +320,19 @@ protected String getAttributeClass(JsonObject attribute) {
return classSimple(attribute.getString("$ref"));
}

protected String getAttributeArrayClass(JsonObject attribute) {
return classSimple(attribute.getJsonObject("items").getString("$ref"));
protected String getAttributeArrayClass(String stepName, JsonObject attribute) {
if (attribute.containsKey("items")) {
JsonObject items = attribute.getJsonObject("items");
if (items.containsKey("$ref")) {
return classSimple(items.getString("$ref"));
} else if (items.containsKey("properties")) {
JsonObject properties = items.getJsonObject("properties");
return classSimple(properties.getJsonObject(stepName).getString("$ref"));
} else {
return items.getString("type");
}
}
return "string";
}

protected String classSimple(String classFullName) {
Expand Down Expand Up @@ -400,4 +411,8 @@ protected Map<String, String> getProcessorDefinitionStepNameMap() {
protected JsonObject getDefinition(JsonObject definitions, String className) {
return definitions.getJsonObject(className.replace("#/items/definitions/", ""));
}

protected boolean isAttributeRefArray(JsonObject attribute) {
return attribute.containsKey("type") && attribute.getString("type").equals("array");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ private String generateModelApi(String classFullName, JsonObject obj) {
String className = classSimple(classFullName);
String stepName = getStepNameForClass(className);

Map<String, JsonObject> properties = getClassProperties(obj);
print = (stepName.equalsIgnoreCase("RouteConfiguration"));

Map<String, JsonObject> properties = getClassProperties(stepName, obj);

List<String> attrs = new ArrayList<>();
AtomicBoolean hasId = new AtomicBoolean(false);
Expand All @@ -149,12 +151,14 @@ private String generateModelApi(String classFullName, JsonObject obj) {
if ("id".equals(name)) {
hasId.set(true);
}
if (isAttributeRefArray(aValue) && name.equals("steps") && ! className.equals("ChoiceDefinition") && ! className.equals("SwitchDefinition") && ! className.equals("KameletDefinition")) {
boolean attributeIsArray = isAttributeRefArray(aValue);
String attributeArrayClass = getAttributeArrayClass(name, aValue);
if (attributeIsArray && name.equals("steps") && ! className.equals("ChoiceDefinition") && ! className.equals("SwitchDefinition") && ! className.equals("KameletDefinition")) {
attrs.add(" def.steps = CamelDefinitionApi.createSteps(element?.steps);");
} else if (isAttributeRefArray(aValue) && !name.equals("steps")) {
} else if (attributeIsArray && !name.equals("steps") && !attributeArrayClass.equals("string")) {
String code = String.format(
" def.%1$s = element && element?.%1$s ? element?.%1$s.map((x:any) => CamelDefinitionApi.create%2$s(x)) :[];"
, name, getAttributeArrayClass(aValue));
, name, attributeArrayClass);
attrs.add(code);
} else if (isAttributeRef(aValue)
&& !getAttributeClass(aValue).equals("SagaActionUriDefinition") // SagaActionUriDefinition is exception
Expand Down Expand Up @@ -194,13 +198,4 @@ private String getStringToRequired(JsonObject obj, String className) {
return "";
}
}

private boolean isAttributeRefArray(JsonObject attribute) {
if (attribute.containsKey("type") && attribute.getString("type").equals("array")) {
JsonObject items = attribute.getJsonObject("items");
return items.containsKey("$ref");
} else {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ private void createCamelDefinitions() throws Exception {

private String generateModel(String classFullName, JsonObject obj, JsonObject definitions, Map<String, JsonObject> dslMetadata) {
String className = classSimple(classFullName);
Map<String, JsonObject> properties = getClassProperties(obj);
String stepName = getStepNameForClass(className);
Map<String, JsonObject> properties = getClassProperties(stepName, obj);

List<String> required = obj.containsKey("required") ? obj.getJsonArray("required").getList() : List.of();
List<String> attrs = new ArrayList<>();
String stepName = getStepNameForClass(className);
if (className.endsWith("Definition")) {
attrs.add(" stepName?: string = '" + stepName + "'");
} else if (className.endsWith("Expression")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,24 @@ private String generateModelApi(String classFullName, JsonObject obj) {
String s1 = getStringToRequired(obj, className);
AtomicReference<String> s3 = new AtomicReference<>("");

Map<String, JsonObject> properties = getClassProperties(obj);
Map<String, JsonObject> properties = getClassProperties(stepName, obj);

Map<String, String> attrs = new HashMap<>();
properties.keySet().stream().sorted(getComparator(stepName)).forEach(aName -> {
if (aName.equals("uri")) {
s3.set("\n def = ComponentApi.parseElementUri(def);");
}
JsonObject aValue = properties.get(aName);
if (isAttributeRefArray(aValue) && aName.equals("steps") && ! className.equals("ChoiceDefinition") && ! className.equals("SwitchDefinition") && ! className.equals("KameletDefinition")) {
boolean attributeIsArray = isAttributeRefArray(aValue);
String attributeArrayClass = getAttributeArrayClass(aName, aValue);
if (attributeIsArray && aName.equals("steps") && ! className.equals("ChoiceDefinition") && ! className.equals("SwitchDefinition") && ! className.equals("KameletDefinition")) {
attrs.put(aName, " def.steps = CamelDefinitionYamlStep.readSteps(element?.steps);\n");
} else if (isAttributeRefArray(aValue) && !aName.equals("steps")) {
} else if (attributeIsArray && !aName.equals("steps") && !attributeArrayClass.equals("string")) {
String format = Arrays.asList("intercept", "interceptFrom", "interceptSendToEndpoint", "onCompletion", "onException").contains(aName)
? " def.%1$s = element && element?.%1$s ? element?.%1$s.map((x:any) => CamelDefinitionYamlStep.read%2$s(x.%1$s)) :[]; \n"
: " def.%1$s = element && element?.%1$s ? element?.%1$s.map((x:any) => CamelDefinitionYamlStep.read%2$s(x)) :[]; \n";

String code = String.format(format, aName, getAttributeArrayClass(aValue));
String code = String.format(format, aName, attributeArrayClass);
attrs.put(aName, code);
} else if (isAttributeRef(aValue) && getAttributeClass(aValue).equals("ExpressionDefinition")) { // Expressions implicits
String code = String.format(
Expand Down Expand Up @@ -162,13 +164,4 @@ private String getStringToRequired(JsonObject obj, String className) {
return "";
}
}

private boolean isAttributeRefArray(JsonObject attribute) {
if (attribute.containsKey("type") && attribute.getString("type").equals("array")) {
JsonObject items = attribute.getJsonObject("items");
return items.containsKey("$ref");
} else {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,10 @@ private void createCamelDefinitions() throws Exception {
definitions.getMap().forEach((s, o) -> {
if (s.startsWith("org.apache.camel.model.") && s.endsWith("Definition")) {
String name = classSimple(s);
String stepName = getStepNameForClass(name);
JsonObject obj = getDefinition(definitions, s);
// JsonObject props = obj.containsKey("oneOf") ? obj.getJsonArray("oneOf").getJsonObject(1).getJsonObject("properties") : obj.getJsonObject("properties");
Map<String, JsonObject> properties = getClassProperties(obj);
Map<String, JsonObject> properties = getClassProperties(stepName, obj);
classProps.put(name, JsonObject.mapFrom(properties));
}
});
Expand Down Expand Up @@ -180,7 +181,7 @@ private String getMetadataCode(String className, Map<String, JsonObject> classPr
if ("inheritErrorHandler".equals(pname) && p == null) {
} else {

PropertyMeta pm = getAttributeType(new JsonObject((Map) v));
PropertyMeta pm = getAttributeType(pname, new JsonObject((Map) v));
String displayName = p != null && p.containsKey("displayName") ? p.getString("displayName") : pname;
String desc = p != null && p.containsKey("description") ? p.getString("description") : pname;
String en = p != null && p.containsKey("enum") ? p.getString("enum").replace("[", "").replace("]", "") : "";
Expand Down Expand Up @@ -219,7 +220,7 @@ private String getJavaType(JsonObject p) {
return "";
}

private PropertyMeta getAttributeType(JsonObject attribute) {
private PropertyMeta getAttributeType(String pname, JsonObject attribute) {
if (attribute.containsKey("$ref")) {
String classFullName = attribute.getString("$ref");
String className = classSimple(classFullName);
Expand All @@ -232,11 +233,22 @@ private PropertyMeta getAttributeType(JsonObject attribute) {
return new PropertyMeta(className, false, true);
} else if (attribute.containsKey("type") && attribute.getString("type").equals("array")) {
JsonObject items = attribute.getJsonObject("items");
if (items.containsKey("$ref") && items.getString("$ref").equals("#/items/definitions/org.apache.camel.model.ProcessorDefinition")) {
return new PropertyMeta("CamelElement", true, true);
if (items.containsKey("properties") && items.getJsonObject("properties").containsKey(pname)) {
String t = items.getJsonObject("properties").getJsonObject(pname).getString("$ref");
if (t.equals("#/items/definitions/org.apache.camel.model.ProcessorDefinition")) {
return new PropertyMeta("CamelElement", true, true);
} else {
String className = classSimple(t);
return new PropertyMeta(className, true, true);
}
} else if (items.containsKey("$ref")) {
String className = classSimple(items.getString("$ref"));
return new PropertyMeta(className, true, true);
String t = items.getString("$ref");
if (t.equals("#/items/definitions/org.apache.camel.model.ProcessorDefinition")) {
return new PropertyMeta("CamelElement", true, true);
} else {
String className = classSimple(t);
return new PropertyMeta(className, true, true);
}
} else {
return new PropertyMeta(items.getString("type"), true, false);
}
Expand Down

0 comments on commit 3583d1e

Please sign in to comment.