Skip to content

Commit

Permalink
Fix openapi validator validateResponses config (0.117) (#9737)
Browse files Browse the repository at this point in the history
Fix openapi validator validateResponses config (#9736)

- Fix openapi validator validateResponses config
- Change create2 evm address not found message
- Fix openapi spec

Signed-off-by: Xin Li <[email protected]>
  • Loading branch information
xin-hedera authored Nov 11, 2024
1 parent 66eb8f7 commit 2b1575d
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 29 deletions.
8 changes: 4 additions & 4 deletions hedera-mirror-rest/__tests__/service/contractService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -972,14 +972,14 @@ describe('ContractService.getContractStateChangesByTimestamps tests', () => {

describe('ContractService.getContractIdByEvmAddress tests', () => {
test('No match', async () => {
const evmAddressFilter = {shard: 0, realm: 0, create2_evm_address: Buffer.from('123', 'hex')};
const evmAddressFilter = {shard: 0, realm: 0, create2_evm_address: 'deadbeaf'};
await expect(() => ContractService.getContractIdByEvmAddress(evmAddressFilter)).rejects.toThrow(
new NotFoundError(`No contract with the given evm address: ${JSON.stringify(evmAddressFilter)} has been found.`)
new NotFoundError(`No contract with the given evm address 0xdeadbeaf has been found.`)
);
});

test('Multiple rows match', async () => {
const evmAddress = Buffer.from('3d4ffd867fac5d9c228d1dbeb7f218a29c94b', 'hex');
const evmAddress = 'a25db2e7595e094b1074122a048030f90b76d526';
await integrationDomainOps.loadContracts([
{
auto_renew_period: 7890000,
Expand Down Expand Up @@ -1042,7 +1042,7 @@ describe('ContractService.getContractIdByEvmAddress tests', () => {

const evmAddressFilter = {shard: 0, realm: 0, create2_evm_address: evmAddress};
await expect(() => ContractService.getContractIdByEvmAddress(evmAddressFilter)).rejects.toThrow(
new Error(`More than one contract with the evm address ${JSON.stringify(evmAddressFilter)} have been found.`)
new Error(`More than one contract with the evm address 0x${evmAddress} have been found.`)
);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"description": "Contract logs api call for a specific contract using contract id with varied filters and no matching results",
"setup": {},
"tests": [
{
"url": "/api/v1/contracts/0.0.5001/results/logs",
"responseStatus": 200,
"responseJson": {
"logs": [],
"links": {
"next": null
}
}
},
{
"url": "/api/v1/contracts/0x5555555555555555555555555555555555555555/results/logs",
"responseStatus": 404,
"responseJson": {
"_status": {
"messages": [
{
"message": "No contract with the given evm address 0x5555555555555555555555555555555555555555 has been found."
}
]
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
{
"description": "Contract results api call for a specific contract using contract id with varied filters and no matching results",
"setup": {
"contractresults": []
},
"urls": [
"/api/v1/contracts/0.0.5001/results",
"/api/v1/contracts/0.0.5001/results?from=987654",
"/api/v1/contracts/0.0.5001/results?timestamp=987654.000123456"
],
"responseStatus": 200,
"responseJson": {
"results": [],
"links": {
"next": null
"setup": {},
"tests": [
{
"urls": [
"/api/v1/contracts/0.0.5001/results",
"/api/v1/contracts/0.0.5001/results?from=987654",
"/api/v1/contracts/0.0.5001/results?timestamp=987654.000123456"
],
"responseStatus": 200,
"responseJson": {
"results": [],
"links": {
"next": null
}
}
},
{
"url": "/api/v1/contracts/0x5555555555555555555555555555555555555555/results",
"responseStatus": 404,
"responseJson": {
"_status": {
"messages": [
{
"message": "No contract with the given evm address 0x5555555555555555555555555555555555555555 has been found."
}
]
}
}
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"_status": {
"messages": [
{
"message": "No contract with the given evm address: {\"shard\":null,\"realm\":null,\"create2_evm_address\":{\"type\":\"Buffer\",\"data\":[94,23,201,136,204,20,174,95,11,243,40,36,94,175,136,240,129,195,166,20]}} has been found."
"message": "No contract with the given evm address 0x5e17c988cc14ae5f0bf328245eaf88f081c3a614 has been found."
}
]
}
Expand Down
4 changes: 4 additions & 0 deletions hedera-mirror-rest/api/v1/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,8 @@ paths:
$ref: "#/components/schemas/ContractResultsResponse"
400:
$ref: "#/components/responses/InvalidParameterError"
404:
$ref: "#/components/responses/NotFoundError"
tags:
- contracts
/api/v1/contracts/{contractIdOrAddress}/state:
Expand Down Expand Up @@ -758,6 +760,8 @@ paths:
$ref: "#/components/schemas/ContractLogsResponse"
400:
$ref: "#/components/responses/InvalidParameterError"
404:
$ref: "#/components/responses/NotFoundError"
tags:
- contracts
/api/v1/contracts/results/logs:
Expand Down
5 changes: 2 additions & 3 deletions hedera-mirror-rest/middleware/openapiHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,13 @@ const serveSwaggerDocs = (app) => {
};

const openApiValidator = (app) => {
const validateResponses = isTestEnv() ? {allErrors: true} : false;
app.use(
OpenApiValidator.middleware({
apiSpec: path.resolve(process.cwd(), getSpecPath(1)),
ignoreUndocumented: true,
validateRequests: false,
validateResponses: {
allErrors: isTestEnv(),
},
validateResponses,
})
);
};
Expand Down
11 changes: 4 additions & 7 deletions hedera-mirror-rest/service/contractService.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,19 +510,17 @@ class ContractService extends BaseService {
}

async getContractIdByEvmAddress(evmAddressFilter) {
const create2EvmAddress = evmAddressFilter.create2_evm_address;
evmAddressFilter.create2_evm_address = Buffer.from(create2EvmAddress, 'hex');
const {params, conditions} = this.computeConditionsAndParamsFromEvmAddressFilter({evmAddressFilter});
const query = `${ContractService.contractIdByEvmAddressQuery} and ${conditions.join(' and ')}`;
const rows = await super.getRows(query, params);
if (rows.length === 0) {
throw new NotFoundError(
`No contract with the given evm address: ${JSONStringify(evmAddressFilter)} has been found.`
);
throw new NotFoundError(`No contract with the given evm address 0x${create2EvmAddress} has been found.`);
}
// since evm_address is not a unique index, it is important to make this check.
if (rows.length > 1) {
throw new Error(
`More than one contract with the evm address ${JSONStringify(evmAddressFilter)} have been found.`
);
throw new Error(`More than one contract with the evm address 0x${create2EvmAddress} have been found.`);
}

return rows[0].id;
Expand All @@ -532,7 +530,6 @@ class ContractService extends BaseService {
const contractIdParts = EntityId.computeContractIdPartsFromContractIdValue(contractIdValue);

if (contractIdParts.hasOwnProperty('create2_evm_address')) {
contractIdParts.create2_evm_address = Buffer.from(contractIdParts.create2_evm_address, 'hex');
return this.getContractIdByEvmAddress(contractIdParts);
}

Expand Down

0 comments on commit 2b1575d

Please sign in to comment.