Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes issue with "undefined" proxies #411

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
});