node-red-node-swagger
node-red-node-swagger copied to clipboard
"List Operations" in sidebar does not include flows with embedded link out/in pairs
I tracked down why my new http endpoint was not showing in the Swagger sidebar... the code searches from the http in
node until it finds a connected http response
node -- and if it doesn't find one, it won't show in the Operation List.
This check is good -- however, in my case, the http response
node IS connected, but through a link out/in pair, which is causing my api to be excluded from the Operations list. The function that does this validation is:
function checkWiresForHttpResponse (node) {
var wires = node.wires[0];
for(var i in wires){
var newNode = RED.nodes.getNode(wires[i]);
if(newNode.type == "http response"){
return true;
} else if(checkWiresForHttpResponse(newNode)){
return true;
}
}
return false;
}
I tried to "hop over" the link nodes, by checking if node.type === "link out"
, then use the node.links
array (instead of wires[0]
). Although node.links
exists in the flows.json file, once it is loaded into the runtime, that attribute is no longer available... (?) hmmm
So it probably requires an intermediate node lookup, something like this (untested)?
var newNode = RED.nodes.getNode(wires[i]);
if (newNode.type == "link out") {
newNode = RED.nodes.getNode(node.links[0]);
}
However, this assumes there is only one link in
connected to each link out
-- so some iteration will probably be needed. And will this work for wires connected to subflows? Probably not, which makes me wonder if this validation is even necessary.
Today, any http in
node wired to an http response
node will appear in the list of Swagger Operations. Instead, I suggest we eliminate the checkWires validation, and only include those http in
nodes that have a swagger-doc
node defined. I'd be happy to submit a PR with these and any other necessary changes, if you agree with my assessment.
Another broken condition related to this issue -- the node-link traversal seems to only follow wires from the 1st output port of a switch
node (and probably other nodes with multiple output ports?).
While developing most of my http "endpoint" flows, I use inject nodes as an alternate way to test the logic of the flow. So at the end of the flow is a switch node that checks if msg.req is not null, and sends the response to an http response
node (or a debug
node, if not initiated from an http in
node). If I test for null first (connected to the debug node) then my endpoint does not show up in the swagger sidebar -- even though there is an http response node at the end of the flow.