Skip to content

Commit 613eb06

Browse files
committed
fix(tests): print out openapi validation errors rather than just throwing
1 parent f6c6b41 commit 613eb06

File tree

4 files changed

+402
-44
lines changed

4 files changed

+402
-44
lines changed

config/test.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
"logging": {
44
"level": "warn"
55
},
6-
"routing": {
7-
"openapi": {
8-
"validateResponses": true
9-
}
10-
},
116
"server": {
127
"port": 0
138
}

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
}
5555
],
5656
"@semantic-release/npm",
57-
"@semantic-release/git"
57+
"@semantic-release/github"
5858
]
5959
},
6060
"homepage": "https://github.com/openapi-typescript-infra/service#readme",
@@ -99,10 +99,9 @@
9999
"@commitlint/cli": "^18.4.3",
100100
"@commitlint/config-conventional": "^18.4.3",
101101
"@openapi-typescript-infra/coconfig": "^4.2.2",
102-
"@semantic-release/changelog": "^6.0.3",
103102
"@semantic-release/commit-analyzer": "^11.1.0",
104103
"@semantic-release/exec": "^6.0.3",
105-
"@semantic-release/git": "^10.0.1",
104+
"@semantic-release/github": "^9.2.3",
106105
"@semantic-release/release-notes-generator": "^12.1.0",
107106
"@types/cookie-parser": "^1.4.6",
108107
"@types/express": "^4.17.21",

src/openapi.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import path from 'path';
33
import _ from 'lodash';
44
import * as OpenApiValidator from 'express-openapi-validator';
55
import { OpenAPIFramework } from 'express-openapi-validator/dist/framework/index';
6-
import type { Handler } from 'express';
6+
import type { Handler, Request } from 'express';
77

88
import type { AnyServiceLocals, ServiceExpress, ServiceLocals } from './types';
9+
import { getNodeEnv } from './env';
910
import { getFilesInDir, loadModule } from './express-app/modules';
1011
import { ConfigurationSchema } from './config/schema';
1112

@@ -116,6 +117,19 @@ export async function openApi<
116117

117118
const { routing } = app.locals.config;
118119
const combinedOptions = {
120+
// In test mode, validate returned swagger responses. This can easily be disabled
121+
// by setting validateResponses to false in the config.
122+
...(getNodeEnv() === 'test'
123+
? {
124+
validateResponses: {
125+
onError(error: Error, body: unknown, req: Request) {
126+
console.log('Response body fails validation: ', error);
127+
console.log('Emitted from:', req.originalUrl);
128+
console.debug(body);
129+
},
130+
},
131+
}
132+
: {}),
119133
...(typeof routing.openapi === 'object' ? routing.openapi : {}),
120134
...openApiOptions,
121135
};

0 commit comments

Comments
 (0)