fix: support comma-separated strings in array query params (#1809)

* fix: support comma-separated strings in array query params

* fix: `asset_identifiers` array parsing
This commit is contained in:
Matthew Little
2024-01-09 15:46:45 +01:00
committed by GitHub
parent 775a62ede9
commit c9a4df8f43
4 changed files with 34 additions and 10 deletions

View File

@@ -319,7 +319,11 @@ export function parseEventTypeFilter(
}
} else if (typeof typeQuery === 'string') {
try {
eventTypeFilter = parseEventTypeStrings([typeQuery]);
if (typeQuery.includes(',')) {
eventTypeFilter = parseEventTypeStrings(typeQuery.split(','));
} else {
eventTypeFilter = parseEventTypeStrings([typeQuery]);
}
} catch (error) {
handleBadRequest(res, next, `invalid 'event type'`);
}

View File

@@ -33,18 +33,22 @@ export function createTokenRouter(db: PgStore): express.Router {
}
let assetIdentifiers: string[] | undefined;
if (req.query.asset_identifiers !== undefined) {
for (const assetIdentifier of [req.query.asset_identifiers].flat()) {
if (typeof req.query.asset_identifiers === 'string') {
if (req.query.asset_identifiers.includes(',')) {
assetIdentifiers = req.query.asset_identifiers.split(',');
} else {
assetIdentifiers = [req.query.asset_identifiers];
}
} else {
assetIdentifiers = req.query.asset_identifiers as string[];
}
for (const assetIdentifier of assetIdentifiers) {
if (
typeof assetIdentifier !== 'string' ||
!isValidPrincipal(assetIdentifier.split('::')[0])
) {
res.status(400).json({ error: `Invalid asset identifier ${assetIdentifier}` });
return;
} else {
if (!assetIdentifiers) {
assetIdentifiers = [];
}
assetIdentifiers?.push(assetIdentifier);
}
}
}

View File

@@ -52,7 +52,11 @@ export function createTxRouter(db: PgStore): express.Router {
if (Array.isArray(typeQuery)) {
txTypeFilter = parseTxTypeStrings(typeQuery as string[]);
} else if (typeof typeQuery === 'string') {
txTypeFilter = parseTxTypeStrings([typeQuery]);
if (typeQuery.includes(',')) {
txTypeFilter = parseTxTypeStrings(typeQuery.split(','));
} else {
txTypeFilter = parseTxTypeStrings([typeQuery]);
}
} else if (typeQuery) {
throw new Error(`Unexpected tx type query value: ${JSON.stringify(typeQuery)}`);
} else {
@@ -82,8 +86,13 @@ export function createTxRouter(db: PgStore): express.Router {
'/multiple',
asyncHandler(async (req, res, next) => {
if (typeof req.query.tx_id === 'string') {
// in case req.query.tx_id is a single tx_id string and not an array
req.query.tx_id = [req.query.tx_id];
// check if tx_id is a comma-seperated list of tx_ids
if (req.query.tx_id.includes(',')) {
req.query.tx_id = req.query.tx_id.split(',');
} else {
// in case req.query.tx_id is a single tx_id string and not an array
req.query.tx_id = [req.query.tx_id];
}
}
const txList: string[] = req.query.tx_id as string[];

View File

@@ -299,6 +299,13 @@ describe('tx tests', () => {
expect(jsonRes[dbTx3.tx_id].result.tx_id).toEqual(dbTx3.tx_id);
expect(jsonRes[dbTx3.tx_id].result.tx_type).toEqual('smart_contract');
expect(jsonRes[dbTx3.tx_id].result.smart_contract.clarity_version).toEqual(2);
// test comma-separated tx_id list
const txIds = [mempoolTx.tx_id, tx1.tx_id, notFoundTxId, dbTx2.tx_id, dbTx3.tx_id].join(',');
const txsListDetail2 = await supertest(api.server).get(
`/extended/v1/tx/multiple?tx_id=${txIds}`
);
expect(txsListDetail2.body).toEqual(txsListDetail.body);
});
test('getTxList returns object', async () => {