Skip to content

Commit

Permalink
Fixes issue with "undefined" proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
gislikonrad committed Dec 8, 2023
1 parent 098da35 commit f6dc514
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/package/Bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,11 @@ function buildResources(bundle) {
}

Bundle.prototype.getElement = function () {
// read the contents of the file and return it raw
if (!this.element) {
const filePath = this.filePath;
// Read the contents of the file if it exists and return it raw.
// There are cases where the filePath ends with /undefined, so
// we don't want to attempt read a file that doesn't exist.
const filePath = this.filePath;
if (!this.element && fs.existsSync(filePath)) {
debug(`getElement: filePath:${filePath}`);
this.element = new Dom().parseFromString(
fs.readFileSync(filePath).toString()
Expand Down Expand Up @@ -345,10 +347,16 @@ Bundle.prototype.addMessage = function (msg) {
msg.source = msg.entity.getSource();
}
if (!msg.line && msg.entity && typeof msg.entity.getElement == "function") {
msg.line = msg.entity.getElement().lineNumber;
let element = msg.entity.getElement();
if(element) {
msg.line = element.lineNumber;
}
}
if (!msg.column && msg.entity && typeof msg.entity.getElement == "function") {
msg.column = msg.entity.getElement().columnNumber;
let element = msg.entity.getElement();
if(element) {
msg.column = element.columnNumber;
}
}
delete msg.entity;

Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/resources/newBundle/apiproxy/proxies/default.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<ProxyEndpoint name="default">
<HTTPProxyConnection>
<BasePath>/</BasePath>
<Properties/>
<VirtualHost>secure</VirtualHost>
</HTTPProxyConnection>

<PreFlow name="PreFlow">
<Request />
<Response />
</PreFlow>
<PostFlow name="PostFlow">
<Request />
<Response />
</PostFlow>
<RouteRule name="no-route">
<!-- this is ok -->
</RouteRule>

</ProxyEndpoint>
42 changes: 42 additions & 0 deletions test/specs/testBundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const assert = require("assert"),
path = require("path"),
Bundle = require("../../lib/package/Bundle.js");

describe("addMessage", function() {
it("Should add a message for 'undefined' proxies", function() {
const proxyPath = path.resolve(__dirname, '../fixtures/resources/newBundle/apiproxy');
const configuration = {
debug: true,
source: {
type: "filesystem",
path: proxyPath,
bundleType: "apiproxy"
},
profile: 'apigee',
excluded: {},
setExitCode: false,
output: () => {} // suppress output
};

const message = "This is a test";
const plugin = {
ruleId: "TR001",
severity: 1, //warning
nodeType: "Bundle"
};

let bundle = new Bundle(configuration);
bundle.addMessage({
plugin,
message: message
});

bundle.getReport(report => {
let bundleResult = report.find(element => element.filePath === proxyPath);
assert.notEqual(bundleResult, null);
assert.equal(bundleResult.warningCount, 1);
let m = bundleResult.messages.find(element => element.message === message);
assert.equal(m.ruleId, plugin.ruleId);
});
});
});

0 comments on commit f6dc514

Please sign in to comment.