Redocly / redoc

📘 OpenAPI/Swagger-generated API Reference Documentation
https://redocly.github.io/redoc/
MIT License
23.47k stars 2.29k forks source link

Infer nullable from schema #1503

Open santialbo opened 3 years ago

santialbo commented 3 years ago

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 commented 3 years ago

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