Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer nullable from schema #1503

Open
santialbo opened this issue Jan 21, 2021 · 1 comment
Open

Infer nullable from schema #1503

santialbo opened this issue Jan 21, 2021 · 1 comment
Assignees

Comments

@santialbo
Copy link

OAS 3.1 is removing nullable from the specification for the JSON schemas in favor of using the JSON-schema way which is

{ "type": ["string", "null"] }
{ "oneOf": [ { "type": "string" }, { "type": "null" } ] }

Redoc right now sort of supports the second option (won't show the Nullable label though) and doesn't have any support for array type.

The expected result would be to treat both options exactly the same way as

{
  "type": "string",
  "nullable": true
}
@santialbo
Copy link
Author

I needed to support this so I used patch-package with the following patch

diff --git a/node_modules/redoc/bundles/redoc.lib.js b/node_modules/redoc/bundles/redoc.lib.js
index c33a53f..a94ac20 100644
--- a/node_modules/redoc/bundles/redoc.lib.js
+++ b/node_modules/redoc/bundles/redoc.lib.js
@@ -9064,9 +9064,15 @@ var Schema_SchemaModel = /** @class */ (function () {
         this.isCircular = schema['x-circular-ref'];
         this.title = schema.title || isNamedDefinition(this.pointer) && JsonPointer_JsonPointer.baseName(this.pointer) || '';
         this.description = schema.description || '';
-        this.type = schema.type || detectType(schema);
+        if (Array.isArray(schema.type) && schema.type.includes("null")) {
+          this.nullable = true;
+          const other = schema.type.filter(t => t !== "null");
+          this.type = other.length === 1 ? other[0] : other;
+        } else  {
+          this.nulable = !!schema.nullable;
+          this.type = schema.type || detectType(schema);
+        }
         this.format = schema.format;
-        this.nullable = !!schema.nullable;
         this.enum = schema.enum || [];
         this.example = schema.example;
         this.deprecated = !!schema.deprecated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants