diff --git a/README.md b/README.md index 1d4ac39..545d2cd 100644 --- a/README.md +++ b/README.md @@ -129,6 +129,9 @@ Schema tables handle: - `anyOf`/`oneOf` composition: typed by the alternatives joined with `|` (`object|null`, `string|integer`); a nullable object also lists the object's properties, and a request or response body lists each alternative in turn +- Null: OpenAPI 3.0's `nullable: true` and 3.1's type arrays + (`type: ["string", "null"]`) and `{ type: "null" }` alternatives all render + as `|null` (`string|null`, `string (date-time)|null`, `object|null`) - Property attributes: required, read-only, nullable, default, minimum/maximum, enum, example, format ### Field tables and `llms-txt` @@ -154,7 +157,7 @@ quarto-openapi/ package.json # the script's `yaml` dependency package-lock.json # pinned; `npm ci` installs from it lib/ - types.ts # OpenAPI 3.0.x type definitions + types.ts # OpenAPI 3.0 and 3.1 type definitions refs.ts # $ref resolution sections.ts # path grouping and endpoint rendering schema.ts # schema-to-table conversion diff --git a/_extensions/quarto-openapi/lib/schema.ts b/_extensions/quarto-openapi/lib/schema.ts index 49a2b5c..b20ee6b 100644 --- a/_extensions/quarto-openapi/lib/schema.ts +++ b/_extensions/quarto-openapi/lib/schema.ts @@ -26,7 +26,7 @@ export function renderSchema( lines.push(""); } - const type = resolved.type; + const type = typeOf(resolved); if (type === "array") { return renderArray(spec, resolved, lines); @@ -41,12 +41,12 @@ export function renderSchema( } const alternatives = resolved.anyOf ?? resolved.oneOf; - if (!type && alternatives) { + if (nonNullTypes(resolved).length === 0 && alternatives) { return renderAlternatives(spec, alternatives, lines); } // Primitive type - if (type) { + if (resolved.type !== undefined) { lines.push(formatPrimitiveType(spec, resolved)); } @@ -91,7 +91,37 @@ function isMap(schema: Schema): boolean { /** Whether a schema describes an object whose properties can be listed. */ function isObjectLike(schema: Schema): boolean { - return schema.type === "object" || !!schema.properties; + return typeOf(schema) === "object" || !!schema.properties; +} + +/** The types a schema's `type` keyword lists: one in OpenAPI 3.0, any number in 3.1. */ +function typesOf(schema: Schema): string[] { + if (schema.type === undefined) return []; + return Array.isArray(schema.type) ? schema.type : [schema.type]; +} + +/** The types a schema's `type` keyword lists, apart from "null". */ +function nonNullTypes(schema: Schema): string[] { + return typesOf(schema).filter((t) => t !== "null"); +} + +/** + * The one type a schema allows apart from null, such as "string" for + * `type: ["string", "null"]`, or undefined when it lists none or several. + */ +function typeOf(schema: Schema): string | undefined { + const types = nonNullTypes(schema); + return types.length === 1 ? types[0] : undefined; +} + +/** + * Whether a schema allows null as well as the types it lists: OpenAPI 3.0's + * `nullable: true`, or a 3.1 type array such as `["string", "null"]`. + */ +function isNullable(schema: Schema): boolean { + if (schema.nullable === true) return true; + const types = typesOf(schema); + return types.includes("null") && types.length > 1; } /** @@ -128,7 +158,10 @@ function renderArray( lines.push("Array of objects:"); lines.push(""); lines.push(...renderSchema(spec, items)); - } else if (!items.type && (items.anyOf || items.oneOf)) { + } else if ( + nonNullTypes(items).length === 0 && + (items.anyOf || items.oneOf) + ) { lines.push("Array of:"); lines.push(""); lines.push(...renderSchema(spec, items)); @@ -222,8 +255,8 @@ function flattenProperties( /** * The object whose properties a field's child rows list, and the suffix its * child names take after the field's name: `[]` steps into array items, `{}` - * into map values. A nullable object (`anyOf: [{ $ref }, { nullable: true }]`) - * lists the object's properties. + * into map values. A nullable object (`anyOf: [{ $ref }, { type: "null" }]`, + * or `{ nullable: true }` in OpenAPI 3.0) lists the object's properties. */ function nestedFields( spec: OpenAPISpec, @@ -232,7 +265,7 @@ function nestedFields( if (schema.properties) return { fields: schema, suffix: "" }; const inner = - schema.type === "array" && schema.items + typeOf(schema) === "array" && schema.items ? { schema: schema.items, suffix: "[]" } : isMap(schema) && typeof schema.additionalProperties === "object" ? { schema: schema.additionalProperties, suffix: "{}" } @@ -272,12 +305,14 @@ export function attributesOf(schema: Schema, isRequired: boolean): string { const semanticFormats = new Set(["date-time", "date", "time", "duration", "uuid", "uri", "uri-reference", "email", "hostname", "ipv4", "ipv6", "byte", "binary", "password"]); /** - * Whether a schema only marks its parent as nullable, like the second member - * of `anyOf: [{ $ref }, { nullable: true }]`. + * Whether a schema allows only null, like the second member of + * `anyOf: [{ $ref }, { type: "null" }]`, or only marks its parent as nullable, + * like OpenAPI 3.0's `anyOf: [{ $ref }, { nullable: true }]`. */ function isNullMarker(schema: Schema | Reference): boolean { if (isReference(schema)) return false; - if (schema.type === "null") return true; + const types = typesOf(schema); + if (types.length > 0) return types.every((t) => t === "null"); return ( schema.nullable === true && Object.keys(schema).every((k) => k === "nullable" || k === "description") @@ -303,15 +338,17 @@ function refName(ref: string): string { } /** - * A short name for the type a schema allows: its `type`; `[item]` for an - * array; `map[string, value]` for a map; the alternatives joined by `|` for - * `anyOf`/`oneOf`; the schema's name where a `$ref` would recurse; and `any` - * for a schema that does not constrain the type. + * A short name for the type a schema allows: its `type` apart from null, or + * `null` when that is all it allows; `[item]` for an array; `map[string, + * value]` for a map; the alternatives joined by `|` for `anyOf`/`oneOf`; the + * schema's name where a `$ref` would recurse; and `any` for a schema that does + * not constrain the type. */ function typeLabel(spec: OpenAPISpec, schema: Schema | Reference): string { const s = normalize(spec, schema); if (s["x-circular-ref"]) return refName(s["x-circular-ref"]); - if (s.type === "array" || (!s.type && s.items)) { + const types = nonNullTypes(s); + if (typeOf(s) === "array" || (types.length === 0 && s.items)) { return `[${s.items ? typeLabel(spec, s.items) : "any"}]`; } if (isMap(s)) { @@ -324,13 +361,14 @@ function typeLabel(spec: OpenAPISpec, schema: Schema | Reference): string { : typeLabel(spec, ap as Schema); return `map[string, ${value}]`; } - if (s.type) return s.type; + if (types.length > 0) return types.join("|"); + if (typesOf(s).length > 0) return "null"; const members = s.anyOf ?? s.oneOf; if (members) { const labels = members.flatMap((m) => { if (isNullMarker(m)) return ["null"]; const label = typeLabel(spec, m); - return !isReference(m) && m.nullable ? [label, "null"] : [label]; + return !isReference(m) && isNullable(m) ? [label, "null"] : [label]; }); return [...new Set(labels)].join("|"); } @@ -353,7 +391,7 @@ export function formatTypeString( if (schema.format && (anyFormat || semanticFormats.has(schema.format))) { type = `${type} (${schema.format})`; } - if (schema.nullable && !type.split("|").includes("null")) { + if (isNullable(schema) && !type.split("|").includes("null")) { type = `${type}|null`; } diff --git a/_extensions/quarto-openapi/lib/types.ts b/_extensions/quarto-openapi/lib/types.ts index 82b582d..44dd6c5 100644 --- a/_extensions/quarto-openapi/lib/types.ts +++ b/_extensions/quarto-openapi/lib/types.ts @@ -91,7 +91,8 @@ export interface Response { } export interface Schema { - type?: string; + /** One type, or in OpenAPI 3.1 several, such as `["string", "null"]`. */ + type?: string | string[]; format?: string; description?: string; properties?: Record; @@ -103,6 +104,7 @@ export interface Schema { enum?: unknown[]; default?: unknown; example?: unknown; + /** OpenAPI 3.0's way to allow null; 3.1 lists "null" in `type` instead. */ nullable?: boolean; readOnly?: boolean; writeOnly?: boolean; diff --git a/example/_extensions/posit-dev/quarto-openapi/lib/schema.ts b/example/_extensions/posit-dev/quarto-openapi/lib/schema.ts index 49a2b5c..b20ee6b 100644 --- a/example/_extensions/posit-dev/quarto-openapi/lib/schema.ts +++ b/example/_extensions/posit-dev/quarto-openapi/lib/schema.ts @@ -26,7 +26,7 @@ export function renderSchema( lines.push(""); } - const type = resolved.type; + const type = typeOf(resolved); if (type === "array") { return renderArray(spec, resolved, lines); @@ -41,12 +41,12 @@ export function renderSchema( } const alternatives = resolved.anyOf ?? resolved.oneOf; - if (!type && alternatives) { + if (nonNullTypes(resolved).length === 0 && alternatives) { return renderAlternatives(spec, alternatives, lines); } // Primitive type - if (type) { + if (resolved.type !== undefined) { lines.push(formatPrimitiveType(spec, resolved)); } @@ -91,7 +91,37 @@ function isMap(schema: Schema): boolean { /** Whether a schema describes an object whose properties can be listed. */ function isObjectLike(schema: Schema): boolean { - return schema.type === "object" || !!schema.properties; + return typeOf(schema) === "object" || !!schema.properties; +} + +/** The types a schema's `type` keyword lists: one in OpenAPI 3.0, any number in 3.1. */ +function typesOf(schema: Schema): string[] { + if (schema.type === undefined) return []; + return Array.isArray(schema.type) ? schema.type : [schema.type]; +} + +/** The types a schema's `type` keyword lists, apart from "null". */ +function nonNullTypes(schema: Schema): string[] { + return typesOf(schema).filter((t) => t !== "null"); +} + +/** + * The one type a schema allows apart from null, such as "string" for + * `type: ["string", "null"]`, or undefined when it lists none or several. + */ +function typeOf(schema: Schema): string | undefined { + const types = nonNullTypes(schema); + return types.length === 1 ? types[0] : undefined; +} + +/** + * Whether a schema allows null as well as the types it lists: OpenAPI 3.0's + * `nullable: true`, or a 3.1 type array such as `["string", "null"]`. + */ +function isNullable(schema: Schema): boolean { + if (schema.nullable === true) return true; + const types = typesOf(schema); + return types.includes("null") && types.length > 1; } /** @@ -128,7 +158,10 @@ function renderArray( lines.push("Array of objects:"); lines.push(""); lines.push(...renderSchema(spec, items)); - } else if (!items.type && (items.anyOf || items.oneOf)) { + } else if ( + nonNullTypes(items).length === 0 && + (items.anyOf || items.oneOf) + ) { lines.push("Array of:"); lines.push(""); lines.push(...renderSchema(spec, items)); @@ -222,8 +255,8 @@ function flattenProperties( /** * The object whose properties a field's child rows list, and the suffix its * child names take after the field's name: `[]` steps into array items, `{}` - * into map values. A nullable object (`anyOf: [{ $ref }, { nullable: true }]`) - * lists the object's properties. + * into map values. A nullable object (`anyOf: [{ $ref }, { type: "null" }]`, + * or `{ nullable: true }` in OpenAPI 3.0) lists the object's properties. */ function nestedFields( spec: OpenAPISpec, @@ -232,7 +265,7 @@ function nestedFields( if (schema.properties) return { fields: schema, suffix: "" }; const inner = - schema.type === "array" && schema.items + typeOf(schema) === "array" && schema.items ? { schema: schema.items, suffix: "[]" } : isMap(schema) && typeof schema.additionalProperties === "object" ? { schema: schema.additionalProperties, suffix: "{}" } @@ -272,12 +305,14 @@ export function attributesOf(schema: Schema, isRequired: boolean): string { const semanticFormats = new Set(["date-time", "date", "time", "duration", "uuid", "uri", "uri-reference", "email", "hostname", "ipv4", "ipv6", "byte", "binary", "password"]); /** - * Whether a schema only marks its parent as nullable, like the second member - * of `anyOf: [{ $ref }, { nullable: true }]`. + * Whether a schema allows only null, like the second member of + * `anyOf: [{ $ref }, { type: "null" }]`, or only marks its parent as nullable, + * like OpenAPI 3.0's `anyOf: [{ $ref }, { nullable: true }]`. */ function isNullMarker(schema: Schema | Reference): boolean { if (isReference(schema)) return false; - if (schema.type === "null") return true; + const types = typesOf(schema); + if (types.length > 0) return types.every((t) => t === "null"); return ( schema.nullable === true && Object.keys(schema).every((k) => k === "nullable" || k === "description") @@ -303,15 +338,17 @@ function refName(ref: string): string { } /** - * A short name for the type a schema allows: its `type`; `[item]` for an - * array; `map[string, value]` for a map; the alternatives joined by `|` for - * `anyOf`/`oneOf`; the schema's name where a `$ref` would recurse; and `any` - * for a schema that does not constrain the type. + * A short name for the type a schema allows: its `type` apart from null, or + * `null` when that is all it allows; `[item]` for an array; `map[string, + * value]` for a map; the alternatives joined by `|` for `anyOf`/`oneOf`; the + * schema's name where a `$ref` would recurse; and `any` for a schema that does + * not constrain the type. */ function typeLabel(spec: OpenAPISpec, schema: Schema | Reference): string { const s = normalize(spec, schema); if (s["x-circular-ref"]) return refName(s["x-circular-ref"]); - if (s.type === "array" || (!s.type && s.items)) { + const types = nonNullTypes(s); + if (typeOf(s) === "array" || (types.length === 0 && s.items)) { return `[${s.items ? typeLabel(spec, s.items) : "any"}]`; } if (isMap(s)) { @@ -324,13 +361,14 @@ function typeLabel(spec: OpenAPISpec, schema: Schema | Reference): string { : typeLabel(spec, ap as Schema); return `map[string, ${value}]`; } - if (s.type) return s.type; + if (types.length > 0) return types.join("|"); + if (typesOf(s).length > 0) return "null"; const members = s.anyOf ?? s.oneOf; if (members) { const labels = members.flatMap((m) => { if (isNullMarker(m)) return ["null"]; const label = typeLabel(spec, m); - return !isReference(m) && m.nullable ? [label, "null"] : [label]; + return !isReference(m) && isNullable(m) ? [label, "null"] : [label]; }); return [...new Set(labels)].join("|"); } @@ -353,7 +391,7 @@ export function formatTypeString( if (schema.format && (anyFormat || semanticFormats.has(schema.format))) { type = `${type} (${schema.format})`; } - if (schema.nullable && !type.split("|").includes("null")) { + if (isNullable(schema) && !type.split("|").includes("null")) { type = `${type}|null`; } diff --git a/example/_extensions/posit-dev/quarto-openapi/lib/types.ts b/example/_extensions/posit-dev/quarto-openapi/lib/types.ts index 82b582d..44dd6c5 100644 --- a/example/_extensions/posit-dev/quarto-openapi/lib/types.ts +++ b/example/_extensions/posit-dev/quarto-openapi/lib/types.ts @@ -91,7 +91,8 @@ export interface Response { } export interface Schema { - type?: string; + /** One type, or in OpenAPI 3.1 several, such as `["string", "null"]`. */ + type?: string | string[]; format?: string; description?: string; properties?: Record; @@ -103,6 +104,7 @@ export interface Schema { enum?: unknown[]; default?: unknown; example?: unknown; + /** OpenAPI 3.0's way to allow null; 3.1 lists "null" in `type` instead. */ nullable?: boolean; readOnly?: boolean; writeOnly?: boolean; diff --git a/tests/schema_test.ts b/tests/schema_test.ts index 4b099f0..f4b3486 100644 --- a/tests/schema_test.ts +++ b/tests/schema_test.ts @@ -639,3 +639,122 @@ test("renderSchema: a top-level oneOf renders each alternative", () => { assert(purrs > 0 && or > purrs && barks > or, output); assertStringIncludes(output, "Or `null`."); }); + +test("renderSchema: an OpenAPI 3.1 type array with null shows type|null", () => { + const output = rendered(specWithSchemas(), { + type: "object", + properties: { + email: { type: ["string", "null"] }, + id: { type: ["string", "integer", "null"] }, + tags: { type: ["array", "null"], items: { type: "string" } }, + }, + }); + + assertStringIncludes(output, "* * `email`\n * `string|null`"); + assertStringIncludes(output, "* * `id`\n * `string|integer|null`"); + assertStringIncludes(output, "* * `tags`\n * `[string]|null`"); +}); + +test("renderSchema: an OpenAPI 3.1 type array keeps its format before |null", () => { + const output = rendered(specWithSchemas(), { + type: "object", + properties: { + created: { type: ["string", "null"], format: "date-time" }, + count: { type: ["integer", "null"], format: "int64" }, + }, + }); + + assertStringIncludes(output, "* * `created`\n * `string (date-time)|null`"); + assertStringIncludes(output, "* * `count`\n * `integer|null`"); + assertFalse(output.includes("int64"), output); +}); + +test("renderSchema: an OpenAPI 3.1 nullable object type lists its properties", () => { + const output = rendered(specWithSchemas(), { + type: "object", + properties: { + owner: { + type: ["object", "null"], + properties: { guid: { type: "string" } }, + }, + }, + }); + + assertStringIncludes(output, "* * `owner`\n * `object|null`"); + assertStringIncludes(output, "[`owner.guid`]{.schema-nest-1}"); +}); + +test("renderSchema: a { type: \"null\" } alternative makes a reference nullable", () => { + const spec = specWithSchemas({ + Source: { + type: "object", + properties: { gitUrl: { type: "string" } }, + }, + }); + const output = rendered(spec, { + type: "object", + properties: { + source: { + anyOf: [ + { $ref: "#/components/schemas/Source" } as unknown as Schema, + { type: "null" }, + ], + }, + label: { oneOf: [{ type: "string" }, { type: "null" }] }, + }, + }); + + assertStringIncludes(output, "* * `source`\n * `object|null`"); + assertStringIncludes(output, "[`source.gitUrl`]{.schema-nest-1}"); + assertStringIncludes(output, "* * `label`\n * `string|null`"); +}); + +test("renderSchema: a top-level { type: \"null\" } alternative reads Or `null`", () => { + const spec = specWithSchemas({ + Cat: { type: "object", properties: { purrs: { type: "boolean" } } }, + }); + const output = rendered(spec, { + oneOf: [ + { $ref: "#/components/schemas/Cat" } as unknown as Schema, + { type: "null" }, + ], + }); + + assertStringIncludes(output, "`purrs`"); + assertStringIncludes(output, "Or `null`."); +}); + +test("renderSchema: OpenAPI 3.0 and 3.1 null forms render the same", () => { + const spec = specWithSchemas({ + Tag: { type: "object", properties: { label: { type: "string" } } }, + }); + const tag = { $ref: "#/components/schemas/Tag" } as unknown as Schema; + const v30: Schema = { + type: "object", + properties: { + name: { type: "string", nullable: true }, + created: { type: "string", format: "date-time", nullable: true }, + tag: { anyOf: [tag, { nullable: true }] }, + tags: { anyOf: [{ type: "array", items: tag }, { nullable: true }] }, + id: { anyOf: [{ type: "string", nullable: true }, { type: "integer" }] }, + }, + }; + const v31: Schema = { + type: "object", + properties: { + name: { type: ["string", "null"] }, + created: { type: ["string", "null"], format: "date-time" }, + tag: { anyOf: [tag, { type: "null" }] }, + tags: { anyOf: [{ type: "array", items: tag }, { type: "null" }] }, + id: { anyOf: [{ type: ["string", "null"] }, { type: "integer" }] }, + }, + }; + + const output = rendered(spec, v30); + assertEquals(rendered(spec, v31), output); + assertStringIncludes(output, "* * `name`\n * `string|null`"); + assertStringIncludes(output, "* * `created`\n * `string (date-time)|null`"); + assertStringIncludes(output, "* * `tag`\n * `object|null`"); + assertStringIncludes(output, "* * `tags`\n * `[object]|null`"); + assertStringIncludes(output, "* * `id`\n * `string|null|integer`"); +});