Comments (9)
Some more research suggested to change the syntax as following:
old:
nullable: true
allOf:
- $ref: "#/components/schemas/StateInfo"
That did not change anything.
from exegesis.
I think this is a side effect of the differences in Open API spec and JSON schema. The validator library that exegesis uses is AJV. I came across this bug ajv-validator/ajv#486 which suggests adding a custom keyword.
from exegesis.
Ah, I'm afraid I misread the spec:
Other than the JSON Schema subset fields, the following fields MAY be used for further schema documentation:
nullable - boolean - Allows sending a null value for the defined schema. Default value is false.
I assumed "for further schema documentation" meant these fields were mostly informational (and, indeed, most of these we don't care about from a schema validation viewpoint, except maybe readOnly
and writeOnly
).
I assumed you could do something like:
old:
nullable: true
oneOf:
- $ref: "#/components/schemas/StateInfo"
- type: null
Except, for some weird reason, null is not supported as a type. So yeah, we're going to need to add a custom keyword.
from exegesis.
Glad to see this is indeed a bug, so it can be fixed. :-) Thank you both for the quick responses. I meanwhile confirmed that the code example @jwalton gave in his comment indeed does not work (schema is then invalid and app will not start).
from exegesis.
Oh and @jwalton, if I can do anything to help fix this issue let me know!
from exegesis.
@jwalton It hasn't been released yet but there is a fix for this on the master branch ajv-validator/ajv@f2010f4 so worth keeping an eye on.
@cgredig Writing a failing test, that is small and concise, is always a good way to contribute. For example, writing a patch like the following which can be used to confirm the issue and also to validate the fix.
diff --git a/test/oas3/Schema/validatorsTest.ts b/test/oas3/Schema/validatorsTest.ts
index 8f32482..bbaba7c 100644
--- a/test/oas3/Schema/validatorsTest.ts
+++ b/test/oas3/Schema/validatorsTest.ts
@@ -49,6 +49,16 @@ const openApiDoc : oas3.OpenAPIObject = Object.assign(
}
}
},
+ aNullableObject: {
+ type: 'object',
+ required: ['a'],
+ nullable: true,
+ properties: {
+ a: {
+ type: 'string',
+ }
+ }
+ },
withDefault: {
type: 'object',
properties: {
@@ -234,6 +244,15 @@ describe('schema validators', function() {
expect(validator(undefined).errors).to.eql(null);
});
+ it('should not error for a missing object if nullable', function() {
+ const context = makeContext(openApiDoc, '#/components/schemas/aNullableObject');
+
+ const validator = validators.generateRequestValidator(context, QUERY_PARAM_LOCATION, false);
+
+ expect(validator(null).errors).to.eql(null);
+ expect(validator(undefined).errors).to.eql(null);
+ });
+
it('should fill in default values', function() {
const context = makeContext(openApiDoc, '#/components/schemas/withDefault');
from exegesis.
@cgredig If you want to take a stab at writing a custom keyword for AJV, I wouldn't say no. :) I was hoping I could get to it but I'm buried in a rewrite of our front end right now. :(
from exegesis.
🎉 This issue has been resolved in version 1.2.1 🎉
The release is available on:
Your semantic-release bot 📦🚀
from exegesis.
Awesome! Thanks a lot for fixing this, I upgraded exegesis in my app and can confirm the fix resolves my issues. :)
from exegesis.
Related Issues (20)
- Can not demand an array to be one or more items long HOT 2
- Crash with recursive definition in referenced OpenAPI file HOT 5
- Incorrectly failing validation under a weird condition HOT 4
- Validation error of request body when oneOf is used HOT 5
- Crash with recursive definition in referenced OpenAPI file is back again
- Does not work well with newest json-ptr (1.3.1) HOT 2
- Your .dependabot/config.yml contained invalid details HOT 1
- Export class from controller HOT 2
- Could not find controller if controller is a .ts file HOT 2
- Can we access "ExegesisContext" at express middleware(app.use) not controllers/*.ts HOT 1
- How to filter the request body by only passing the defined requestBody schema? HOT 2
- Security vulnerability in `json-ptr` HOT 4
- Exegesis 3.0.0 using updated ajv outputs issues with OpenAPI spec, but 2.0.0 did not HOT 5
- res.redirect not supported HOT 3
- Is OpenAPI 3.1 supported?
- Bump json-ptr to fix security vulnerability HOT 1
- Support for additional string formats HOT 2
- json-schema-ref-parser dependency breaks node engine < 17 HOT 3
- How to use exegesis with multipart/form-data?
- Mime-types HOT 2
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 exegesis.