Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 7 additions & 1 deletion src/common/pipes/filter-validation.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ export class FilterValidationPipe implements PipeTransform<string, string> {
) {}
transform(inValue: string): string {
const allAllowedKeys: string[] = [...this.allowedObjectKeys];
let inValueParsed;
for (const key in this.filters) {
if (this.filters[key]) {
allAllowedKeys.push(...this.allowedFilterKeys[key]);
}
}
const inValueParsed = JSON.parse(inValue ?? "{}");
try {
inValueParsed = JSON.parse(inValue ?? "{}");
} catch (err) {
const error = err as Error;
throw new BadRequestException(`Invalid JSON in filter: ${error.message}`);
}
const flattenFilterKeys = Object.keys(flattenObject(inValueParsed));
const arbitraryObjectFields = [
"scientificMetadata",
Expand Down
23 changes: 23 additions & 0 deletions test/DatasetV4.js
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,29 @@ describe("2500: Datasets v4 tests", () => {
.expect(TestData.BadRequestStatusCode)
.expect("Content-Type", /json/);
});

it("0209: should return informative error on malfored json is passed in filter", async () => {
const filter = {
limits: {
limit: 1,
},
};
const malformedJson = JSON.stringify(filter).replace("}", "},");

return request(appUrl)
.get(`/api/v4/datasets`)
.query({ filter: malformedJson })
.auth(accessTokenAdminIngestor, { type: "bearer" })
.expect(TestData.BadRequestStatusCode)
.expect("Content-Type", /json/)
.then((res) => {
res.body.should.have
.property("message")
.and.be.equal(
"Invalid JSON in filter: Expected double-quoted property name in JSON at position 22",
);
});
});
});

describe("Datasets v4 findOne tests", () => {
Expand Down
Loading