Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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
Expand Down
76 changes: 57 additions & 19 deletions _extensions/quarto-openapi/lib/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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));
}

Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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,
Expand All @@ -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: "{}" }
Expand Down Expand Up @@ -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")
Expand All @@ -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)) {
Expand All @@ -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("|");
}
Expand All @@ -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`;
}

Expand Down
4 changes: 3 additions & 1 deletion _extensions/quarto-openapi/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Schema | Reference>;
Expand All @@ -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;
Expand Down
76 changes: 57 additions & 19 deletions example/_extensions/posit-dev/quarto-openapi/lib/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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));
}

Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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,
Expand All @@ -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: "{}" }
Expand Down Expand Up @@ -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")
Expand All @@ -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)) {
Expand All @@ -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("|");
}
Expand All @@ -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`;
}

Expand Down
4 changes: 3 additions & 1 deletion example/_extensions/posit-dev/quarto-openapi/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Schema | Reference>;
Expand All @@ -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;
Expand Down
Loading
Loading