Comments (2)
Hi,
I experienced the same issue and I investigated it a bit further.
Short answer:
Use res.send()
or res.json()
methods to respond with data and response validation will work as expected.
Long answer:
Response validation is hooked on res.json()
method in express-openapi-validator
:
https://github.com/cdimascio/express-openapi-validator/blob/3e803b586170e4fc079faedc2eb219276429826e/src/framework/modded.express.mung.ts#L59
Current implementation of the Pet example is using utils.writeJson(res, response)
:
and
utils.writeJson(res, response)
calls response.end(payload)
:That means hooked
res.json()
is never called so validation is not executed.
Also Express suggest to use res.json()
or res.send()
instead of res.end()
when returning data.
https://expressjs.com/en/api.html#res.end
Keep in mind, that validation will work only with JSON response.
I also tested, that it is possible to enable response validation which will remove all fields not mentioned in openAPI schema:
validateResponses: {
removeAdditional: 'all',
}
from oas3-tools.
to my previous answer:
I also tested, that it is possible to enable response validation which will remove all fields not mentioned in openAPI schema:
validateResponses: {
removeAdditional: 'all',
}
There is a problem with response validation when removeAdditional: 'all' option is used in this case:
- OAS contains oneOf statement without discriminator
- express-openapi-validator and ajv used in oas3-tools do not understand discriminator
oas3-tools v2.2.3 depends on express-openapi-validator ^4.4.3 (v4.13.5) which depends on ajv ^6.12.6 (6.12.6)
removeAdditional option is propagated to ajv which describes the problem here:
https://ajv.js.org/guide/modifying-data.html#removing-additional-properties
https://ajv.js.org/json-schema.html#discriminator
Solution could be to use express-openapi-validator
version which supports discriminator:
Discriminator support in ajv
was added in v8. Ajv v8 is used in express-openapi-validator
v4.13.8 https://github.com/cdimascio/express-openapi-validator/commits/v4.13.8
I did not test express-openapi-validator
v4.13.8 though.
from oas3-tools.
Related Issues (20)
- All methods require parameters
- update with v2.2.0 breaks semver rules HOT 4
- Security functions not working HOT 1
- Any working JWT validation example? HOT 2
- npm
- Using predefined express app object HOT 3
- Handling an undefined req.openapi in SwaggerRouter HOT 1
- js Files in extension .cjs and .mjs are not accepted
- impossible import in a "module"-type module
- Body-parser deprecated HOT 1
- No longer able to add custom routes or middlewares HOT 3
- Typescript example
- Update npm package HOT 1
- Not able to add middleware to the app HOT 2
- Request entity too large error
- This repo is 2.2.0 while the npm deployed version is 2.2.3, can you sync the latest repo here? HOT 1
- Patch package published - https://www.npmjs.com/package/oas3-tools-cors
- Custom Error Handler option is not persent
- OpenApi Parameter "Reference Object" are ignored
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from oas3-tools.