GithubHelp home page GithubHelp logo

Comments (9)

cgredig avatar cgredig commented on July 22, 2024

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.

niallmccullagh avatar niallmccullagh commented on July 22, 2024

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.

jwalton avatar jwalton commented on July 22, 2024

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.

cgredig avatar cgredig commented on July 22, 2024

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.

cgredig avatar cgredig commented on July 22, 2024

Oh and @jwalton, if I can do anything to help fix this issue let me know!

from exegesis.

niallmccullagh avatar niallmccullagh commented on July 22, 2024

@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.

jwalton avatar jwalton commented on July 22, 2024

@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.

jwalton avatar jwalton commented on July 22, 2024

🎉 This issue has been resolved in version 1.2.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

from exegesis.

cgredig avatar cgredig commented on July 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.