@webpieces/openapi-generator 0.4.889 → 0.4.891
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webpieces/openapi-generator",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.891",
|
|
4
4
|
"description": "Render an ApiDocModel to OpenAPI 3.1.0 — the canonical internal document and the customer-published one, JSON and YAML, from one generation pass. Ships the wp-openapi bin.",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
"access": "public"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@webpieces/api-doc-model": "0.4.
|
|
27
|
-
"@webpieces/core-util": "0.4.
|
|
26
|
+
"@webpieces/api-doc-model": "0.4.891",
|
|
27
|
+
"@webpieces/core-util": "0.4.891",
|
|
28
28
|
"typescript": "5.9.3"
|
|
29
29
|
},
|
|
30
30
|
"bin": {
|
|
@@ -198,6 +198,12 @@ class SchemaRenderer {
|
|
|
198
198
|
}
|
|
199
199
|
const declared = schema.get('type');
|
|
200
200
|
if (typeof declared === 'string') {
|
|
201
|
+
const values = schema.get('enum');
|
|
202
|
+
if (Array.isArray(values) && !values.includes(null)) {
|
|
203
|
+
// `enum` is checked independently of `type`, so a nullable inline enum
|
|
204
|
+
// (`'a' | 'b' | null`) must list `null` too or the document rejects it (#1197).
|
|
205
|
+
schema.set('enum', [...values, null]);
|
|
206
|
+
}
|
|
201
207
|
return schema.set('type', [declared, 'null']);
|
|
202
208
|
}
|
|
203
209
|
return new JsonObject_1.JsonObject().set('anyOf', [schema, new JsonObject_1.JsonObject().set('type', 'null')]);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SchemaRenderer.js","sourceRoot":"","sources":["../../../../../../packages/docs/openapi-generator/src/generate/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,CAAC,IAAI,CAAC;IACvE,CAAC;CACJ;AA5QD,wCA4QC","sourcesContent":["import { DocumentedField, DocumentedType, TypeRef } from '@webpieces/api-doc-model';\nimport { JsonObject, JsonValue } from '../json/JsonObject';\nimport { SchemaPlacement } from './SchemaPlacement';\n\n/** ONE field the renderer could not give a shape to, and WHERE in the document it would have sat. */\nexport class UnmappedField {\n constructor(\n /** A JSON pointer into the document that would have been written. */\n readonly pointer: string,\n /** The verbatim TypeScript type text, so the cure names the thing to go and type. */\n readonly typeText: string,\n ) {}\n}\n\n/**\n * {@link TypeRef} / {@link DocumentedType} -> JSON Schema 2020-12, which IS OpenAPI 3.1's schema\n * dialect and IS what MCP `tools/list` speaks. That identity is the reason #982 specifies 3.1 rather\n * than 3.0: the two are not \"similar dialects\", they are the same schemas, so the MCP projection\n * (#984) is a selection and an inline pass, not a translation.\n *\n * ## Three things 3.1 lets this renderer state HONESTLY that 3.0 could not\n *\n * - `type: [T, \"null\"]` for a nullable field, instead of 3.0's `nullable: true` keyword, which is not\n * JSON Schema at all.\n * - `description` BESIDE a `$ref`. In 3.0 a sibling of `$ref` is ignored, so a documented field whose\n * type is a named DTO simply lost its prose.\n * - OPTIONAL and NULLABLE as separate facts — absent from `required` versus `\"null\"` in `type`. `{}`\n * and `{x: null}` are different wire documents and a schema that conflates them rejects one.\n *\n * ## It records what it cannot map rather than emitting an untyped field\n *\n * An unmapped type yields an EMPTY schema, which in JSON Schema means \"anything\". That is precisely\n * the green-build-publishes-a-shapeless-field defect the guard exists to stop, so every one is\n * recorded with the pointer it would have occupied and {@link OpenApiGenerator} refuses on the set.\n *\n * ## WHERE a schema is defined is the placement's answer, not this class's (#1058)\n *\n * The {@link SchemaPlacement} decides, per type, whether its schema is defined in THIS document's\n * `components.schemas` (and under which name) or referenced in another package's components\n * document. Every `$ref` — a field, an array item, a union branch, a discriminator `mapping` entry —\n * is written through it, so the split, the bundled and the MCP document are one renderer.\n */\nexport class SchemaRenderer {\n private readonly reached = new Set<string>();\n private readonly external = new Set<string>();\n private readonly unmappedFields: UnmappedField[] = [];\n\n constructor(\n /** The model's named types, by key (`DocumentedType.keyOf`). */\n private readonly types: ReadonlyMap<string, DocumentedType>,\n private readonly placement: SchemaPlacement,\n ) {}\n\n /** Keys of the named types this document DEFINES, reached directly or through another schema. */\n reachedTypes(): ReadonlySet<string> {\n return this.reached;\n }\n\n /** Keys of the named types this document REFERENCES in another package's components document. */\n externalTypes(): ReadonlySet<string> {\n return this.external;\n }\n\n /** Every field with no shape, with its pointer. Empty means the document is fully typed. */\n unmapped(): readonly UnmappedField[] {\n return this.unmappedFields;\n }\n\n /**\n * `components.schemas` for everything reachable from whatever has been rendered so far, expanded\n * to a FIXPOINT.\n *\n * A worklist and not recursion-with-a-guard because rendering a type reaches more types, and the\n * set has to close over that. It terminates for the same reason the extractor's walk does: a\n * named type is registered once, so the worklist strictly shrinks.\n */\n components(): JsonObject {\n const rendered = new Map<string, JsonObject | undefined>();\n let grew = true;\n while (grew) {\n grew = false;\n for (const key of Array.from(this.reached)) {\n const type = this.types.get(key);\n const name = type === undefined ? key : this.placement.localName(key, type);\n if (rendered.has(name)) {\n continue;\n }\n // Rendering it can add MORE names to `reached`, which is what the outer loop is for.\n rendered.set(name, type === undefined ? undefined : this.namedType(key, type));\n grew = true;\n }\n }\n const schemas = new JsonObject();\n // Alphabetical, because `components.schemas` is a lookup table and nothing reads it in order\n // — whereas discovery order would move every time an operation was added above another.\n for (const name of Array.from(rendered.keys()).sort()) {\n schemas.set(name, rendered.get(name));\n }\n return schemas;\n }\n\n /**\n * Mark `key` as DEFINED by this document even when nothing in it refers to the type — the\n * components document of a DTO library publishes every type the library exports (#1058).\n */\n define(key: string): void {\n const type = this.types.get(key);\n if (type !== undefined && this.placement.isLocal(key, type)) {\n this.reached.add(key);\n }\n }\n\n /** One named type: an object DTO, a string enum, or a union with its DERIVED discriminator. */\n private namedType(key: string, type: DocumentedType): JsonObject {\n const pointer = `#/components/schemas/${this.placement.localName(key, type)}`;\n if (type.enumValues.length > 0) {\n return new JsonObject()\n .set('type', 'string')\n .set('description', this.prose(type.description))\n .set('enum', type.enumValues.slice());\n }\n if (type.unionRefNames.length > 0) {\n return this.union(type);\n }\n const properties = new JsonObject();\n const required: string[] = [];\n for (const field of type.fields) {\n properties.set(field.name, this.field(field, `${pointer}/properties/${field.name}`));\n if (!field.optional) {\n required.push(field.name);\n }\n }\n return new JsonObject()\n .set('type', 'object')\n .set('description', this.prose(type.description))\n .set('properties', properties.isEmpty() ? undefined : properties)\n .set('required', required.length === 0 ? undefined : required)\n .set(\n 'additionalProperties',\n type.indexSignatureValue === undefined\n ? undefined\n : this.type(type.indexSignatureValue, `${pointer}/additionalProperties`),\n );\n }\n\n /**\n * A union, with a `discriminator` ONLY when the model derived one — which it does only when every\n * branch carries the same property typed as string values (a literal, a string-enum member, or a\n * union of them) that no two branches share. An invented discriminator\n * would claim a narrowing TypeScript itself cannot do.\n */\n private union(type: DocumentedType): JsonObject {\n const branches: JsonValue[] = type.unionRefNames.map((name: string) =>\n this.reference(name),\n );\n const schema = new JsonObject()\n .set('description', this.prose(type.description))\n .set('oneOf', branches);\n if (type.discriminator === undefined) {\n return schema;\n }\n const mapping = new JsonObject();\n // EVERY value a branch's property may hold maps to that branch — `mode: 'a' | 'b'` is two\n // entries pointing at one schema, which OpenAPI's mapping allows (#1023).\n for (const branch of type.unionRefNames) {\n for (const value of type.discriminator.branchValues.get(branch) ?? []) {\n mapping.set(value, this.pointerTo(branch));\n }\n }\n return schema.set(\n 'discriminator',\n new JsonObject()\n .set('propertyName', type.discriminator.propertyName)\n .set('mapping', mapping),\n );\n }\n\n /**\n * One FIELD: its type, plus the prose and the constraints that hang off the field rather than the\n * type. `@format`, `@WpMin` and `@WpMax` land on the SCALAR — on an array, on the ITEM — because\n * `minimum` on an array means nothing and a reader would have to guess which half was meant.\n */\n field(field: DocumentedField, pointer: string): JsonObject {\n const isArray = field.type.kind === 'array';\n const leafPointer = isArray ? `${pointer}/items` : pointer;\n const leaf = this.constrain(\n isArray ? this.type(field.type.items!, leafPointer) : this.type(field.type, pointer),\n field,\n );\n const schema = isArray\n ? new JsonObject().set('type', 'array').set('items', leaf)\n : this.nullable(leaf, field.nullable);\n if (isArray && field.nullable) {\n return this.described(this.nullable(schema, true), field);\n }\n return this.described(schema, field);\n }\n\n /** `description`, and the `@mcp` override as `x-mcp-description` when the author wrote one. */\n private described(schema: JsonObject, field: DocumentedField): JsonObject {\n return schema\n .set('description', this.prose(field.description))\n .set('x-mcp-description', this.prose(field.mcpDescription));\n }\n\n private constrain(leaf: JsonObject, field: DocumentedField): JsonObject {\n return leaf.set('format', field.format).set('minimum', field.min).set('maximum', field.max);\n }\n\n /** `type: [T, \"null\"]` where there is a type to widen; `anyOf` where the shape is a `$ref`. */\n private nullable(schema: JsonObject, isNullable: boolean): JsonObject {\n if (!isNullable) {\n return schema;\n }\n const declared = schema.get('type');\n if (typeof declared === 'string') {\n return schema.set('type', [declared, 'null']);\n }\n return new JsonObject().set('anyOf', [schema, new JsonObject().set('type', 'null')]);\n }\n\n /** One resolved type, with no field-level prose or constraints on it. */\n type(ref: TypeRef, pointer: string): JsonObject {\n switch (ref.kind) {\n case 'primitive':\n return this.primitive(ref);\n case 'ref':\n return this.reference(ref.refName!);\n case 'array':\n return new JsonObject()\n .set('type', 'array')\n .set('items', this.type(ref.items!, `${pointer}/items`));\n case 'openMap':\n return new JsonObject()\n .set('type', 'object')\n .set(\n 'additionalProperties',\n this.type(ref.values!, `${pointer}/additionalProperties`),\n );\n case 'enum':\n return new JsonObject().set('type', 'string').set('enum', ref.enumValues.slice());\n case 'union':\n return this.namedUnion(ref);\n default:\n this.unmappedFields.push(\n new UnmappedField(pointer, ref.unmappedText ?? '<unknown>'),\n );\n return new JsonObject();\n }\n }\n\n /**\n * A union, as a `$ref` at the NAMED alias when the model registered one.\n *\n * The model records a `type X = A | B` alias as its own entry — carrying the DERIVED\n * discriminator — while the FIELD that used it holds a bare union of branch names. Rendering the\n * field's union inline would therefore publish the `oneOf` and silently drop the discriminator,\n * which is the one part of a union a client actually needs to narrow on. So the alias is looked\n * up by its branch list and referenced.\n */\n private namedUnion(ref: TypeRef): JsonObject {\n const branches = ref.unionRefNames.join(',');\n for (const name of Array.from(this.types.keys())) {\n if (this.types.get(name)!.unionRefNames.join(',') === branches) {\n return this.reference(name);\n }\n }\n return new JsonObject().set(\n 'oneOf',\n ref.unionRefNames.map((each: string) => this.reference(each)),\n );\n }\n\n private primitive(ref: TypeRef): JsonObject {\n if (ref.primitive === 'unknown') {\n // No `type` at all: JSON Schema's honest spelling of \"any shape\". It is only ever reached\n // by a `void` return, which is why it is not the unmapped refusal.\n return new JsonObject();\n }\n return new JsonObject().set('type', ref.integer ? 'integer' : ref.primitive!);\n }\n\n /**\n * A `$ref` to the type registered under `key`, recording whether this document defines it or\n * references it in its declaring package's components document.\n */\n private reference(key: string): JsonObject {\n return new JsonObject().set('$ref', this.pointerTo(key));\n }\n\n private pointerTo(key: string): string {\n const type = this.types.get(key);\n if (type === undefined) {\n // Not in the model at all (a manifest naming an error type that does not exist): keep it\n // local so `components()` writes the hole the guards then name.\n this.reached.add(key);\n return `#/components/schemas/${key}`;\n }\n if (this.placement.isLocal(key, type)) {\n this.reached.add(key);\n } else {\n this.external.add(key);\n }\n return this.placement.pointer(key, type);\n }\n\n /** Empty prose is ABSENT prose. An empty `description` key is noise in every rendered page. */\n private prose(text: string | undefined): string | undefined {\n return text === undefined || text.trim() === '' ? undefined : text;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"SchemaRenderer.js","sourceRoot":"","sources":["../../../../../../packages/docs/openapi-generator/src/generate/SchemaRenderer.ts"],"names":[],"mappings":";;;AACA,mDAA2D;AAG3D,qGAAqG;AACrG,MAAa,aAAa;IAGT;IAEA;IAJb;IACI,qEAAqE;IAC5D,OAAe;IACxB,qFAAqF;IAC5E,QAAgB;QAFhB,YAAO,GAAP,OAAO,CAAQ;QAEf,aAAQ,GAAR,QAAQ,CAAQ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AAI,KAAK,SAAS,IAAI,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC;IACvE,CAAC;CACJ;AAlRD,wCAkRC","sourcesContent":["import { DocumentedField, DocumentedType, TypeRef } from '@webpieces/api-doc-model';\nimport { JsonObject, JsonValue } from '../json/JsonObject';\nimport { SchemaPlacement } from './SchemaPlacement';\n\n/** ONE field the renderer could not give a shape to, and WHERE in the document it would have sat. */\nexport class UnmappedField {\n constructor(\n /** A JSON pointer into the document that would have been written. */\n readonly pointer: string,\n /** The verbatim TypeScript type text, so the cure names the thing to go and type. */\n readonly typeText: string,\n ) {}\n}\n\n/**\n * {@link TypeRef} / {@link DocumentedType} -> JSON Schema 2020-12, which IS OpenAPI 3.1's schema\n * dialect and IS what MCP `tools/list` speaks. That identity is the reason #982 specifies 3.1 rather\n * than 3.0: the two are not \"similar dialects\", they are the same schemas, so the MCP projection\n * (#984) is a selection and an inline pass, not a translation.\n *\n * ## Three things 3.1 lets this renderer state HONESTLY that 3.0 could not\n *\n * - `type: [T, \"null\"]` for a nullable field, instead of 3.0's `nullable: true` keyword, which is not\n * JSON Schema at all.\n * - `description` BESIDE a `$ref`. In 3.0 a sibling of `$ref` is ignored, so a documented field whose\n * type is a named DTO simply lost its prose.\n * - OPTIONAL and NULLABLE as separate facts — absent from `required` versus `\"null\"` in `type`. `{}`\n * and `{x: null}` are different wire documents and a schema that conflates them rejects one.\n *\n * ## It records what it cannot map rather than emitting an untyped field\n *\n * An unmapped type yields an EMPTY schema, which in JSON Schema means \"anything\". That is precisely\n * the green-build-publishes-a-shapeless-field defect the guard exists to stop, so every one is\n * recorded with the pointer it would have occupied and {@link OpenApiGenerator} refuses on the set.\n *\n * ## WHERE a schema is defined is the placement's answer, not this class's (#1058)\n *\n * The {@link SchemaPlacement} decides, per type, whether its schema is defined in THIS document's\n * `components.schemas` (and under which name) or referenced in another package's components\n * document. Every `$ref` — a field, an array item, a union branch, a discriminator `mapping` entry —\n * is written through it, so the split, the bundled and the MCP document are one renderer.\n */\nexport class SchemaRenderer {\n private readonly reached = new Set<string>();\n private readonly external = new Set<string>();\n private readonly unmappedFields: UnmappedField[] = [];\n\n constructor(\n /** The model's named types, by key (`DocumentedType.keyOf`). */\n private readonly types: ReadonlyMap<string, DocumentedType>,\n private readonly placement: SchemaPlacement,\n ) {}\n\n /** Keys of the named types this document DEFINES, reached directly or through another schema. */\n reachedTypes(): ReadonlySet<string> {\n return this.reached;\n }\n\n /** Keys of the named types this document REFERENCES in another package's components document. */\n externalTypes(): ReadonlySet<string> {\n return this.external;\n }\n\n /** Every field with no shape, with its pointer. Empty means the document is fully typed. */\n unmapped(): readonly UnmappedField[] {\n return this.unmappedFields;\n }\n\n /**\n * `components.schemas` for everything reachable from whatever has been rendered so far, expanded\n * to a FIXPOINT.\n *\n * A worklist and not recursion-with-a-guard because rendering a type reaches more types, and the\n * set has to close over that. It terminates for the same reason the extractor's walk does: a\n * named type is registered once, so the worklist strictly shrinks.\n */\n components(): JsonObject {\n const rendered = new Map<string, JsonObject | undefined>();\n let grew = true;\n while (grew) {\n grew = false;\n for (const key of Array.from(this.reached)) {\n const type = this.types.get(key);\n const name = type === undefined ? key : this.placement.localName(key, type);\n if (rendered.has(name)) {\n continue;\n }\n // Rendering it can add MORE names to `reached`, which is what the outer loop is for.\n rendered.set(name, type === undefined ? undefined : this.namedType(key, type));\n grew = true;\n }\n }\n const schemas = new JsonObject();\n // Alphabetical, because `components.schemas` is a lookup table and nothing reads it in order\n // — whereas discovery order would move every time an operation was added above another.\n for (const name of Array.from(rendered.keys()).sort()) {\n schemas.set(name, rendered.get(name));\n }\n return schemas;\n }\n\n /**\n * Mark `key` as DEFINED by this document even when nothing in it refers to the type — the\n * components document of a DTO library publishes every type the library exports (#1058).\n */\n define(key: string): void {\n const type = this.types.get(key);\n if (type !== undefined && this.placement.isLocal(key, type)) {\n this.reached.add(key);\n }\n }\n\n /** One named type: an object DTO, a string enum, or a union with its DERIVED discriminator. */\n private namedType(key: string, type: DocumentedType): JsonObject {\n const pointer = `#/components/schemas/${this.placement.localName(key, type)}`;\n if (type.enumValues.length > 0) {\n return new JsonObject()\n .set('type', 'string')\n .set('description', this.prose(type.description))\n .set('enum', type.enumValues.slice());\n }\n if (type.unionRefNames.length > 0) {\n return this.union(type);\n }\n const properties = new JsonObject();\n const required: string[] = [];\n for (const field of type.fields) {\n properties.set(field.name, this.field(field, `${pointer}/properties/${field.name}`));\n if (!field.optional) {\n required.push(field.name);\n }\n }\n return new JsonObject()\n .set('type', 'object')\n .set('description', this.prose(type.description))\n .set('properties', properties.isEmpty() ? undefined : properties)\n .set('required', required.length === 0 ? undefined : required)\n .set(\n 'additionalProperties',\n type.indexSignatureValue === undefined\n ? undefined\n : this.type(type.indexSignatureValue, `${pointer}/additionalProperties`),\n );\n }\n\n /**\n * A union, with a `discriminator` ONLY when the model derived one — which it does only when every\n * branch carries the same property typed as string values (a literal, a string-enum member, or a\n * union of them) that no two branches share. An invented discriminator\n * would claim a narrowing TypeScript itself cannot do.\n */\n private union(type: DocumentedType): JsonObject {\n const branches: JsonValue[] = type.unionRefNames.map((name: string) =>\n this.reference(name),\n );\n const schema = new JsonObject()\n .set('description', this.prose(type.description))\n .set('oneOf', branches);\n if (type.discriminator === undefined) {\n return schema;\n }\n const mapping = new JsonObject();\n // EVERY value a branch's property may hold maps to that branch — `mode: 'a' | 'b'` is two\n // entries pointing at one schema, which OpenAPI's mapping allows (#1023).\n for (const branch of type.unionRefNames) {\n for (const value of type.discriminator.branchValues.get(branch) ?? []) {\n mapping.set(value, this.pointerTo(branch));\n }\n }\n return schema.set(\n 'discriminator',\n new JsonObject()\n .set('propertyName', type.discriminator.propertyName)\n .set('mapping', mapping),\n );\n }\n\n /**\n * One FIELD: its type, plus the prose and the constraints that hang off the field rather than the\n * type. `@format`, `@WpMin` and `@WpMax` land on the SCALAR — on an array, on the ITEM — because\n * `minimum` on an array means nothing and a reader would have to guess which half was meant.\n */\n field(field: DocumentedField, pointer: string): JsonObject {\n const isArray = field.type.kind === 'array';\n const leafPointer = isArray ? `${pointer}/items` : pointer;\n const leaf = this.constrain(\n isArray ? this.type(field.type.items!, leafPointer) : this.type(field.type, pointer),\n field,\n );\n const schema = isArray\n ? new JsonObject().set('type', 'array').set('items', leaf)\n : this.nullable(leaf, field.nullable);\n if (isArray && field.nullable) {\n return this.described(this.nullable(schema, true), field);\n }\n return this.described(schema, field);\n }\n\n /** `description`, and the `@mcp` override as `x-mcp-description` when the author wrote one. */\n private described(schema: JsonObject, field: DocumentedField): JsonObject {\n return schema\n .set('description', this.prose(field.description))\n .set('x-mcp-description', this.prose(field.mcpDescription));\n }\n\n private constrain(leaf: JsonObject, field: DocumentedField): JsonObject {\n return leaf.set('format', field.format).set('minimum', field.min).set('maximum', field.max);\n }\n\n /** `type: [T, \"null\"]` where there is a type to widen; `anyOf` where the shape is a `$ref`. */\n private nullable(schema: JsonObject, isNullable: boolean): JsonObject {\n if (!isNullable) {\n return schema;\n }\n const declared = schema.get('type');\n if (typeof declared === 'string') {\n const values = schema.get('enum');\n if (Array.isArray(values) && !values.includes(null)) {\n // `enum` is checked independently of `type`, so a nullable inline enum\n // (`'a' | 'b' | null`) must list `null` too or the document rejects it (#1197).\n schema.set('enum', [...values, null]);\n }\n return schema.set('type', [declared, 'null']);\n }\n return new JsonObject().set('anyOf', [schema, new JsonObject().set('type', 'null')]);\n }\n\n /** One resolved type, with no field-level prose or constraints on it. */\n type(ref: TypeRef, pointer: string): JsonObject {\n switch (ref.kind) {\n case 'primitive':\n return this.primitive(ref);\n case 'ref':\n return this.reference(ref.refName!);\n case 'array':\n return new JsonObject()\n .set('type', 'array')\n .set('items', this.type(ref.items!, `${pointer}/items`));\n case 'openMap':\n return new JsonObject()\n .set('type', 'object')\n .set(\n 'additionalProperties',\n this.type(ref.values!, `${pointer}/additionalProperties`),\n );\n case 'enum':\n return new JsonObject().set('type', 'string').set('enum', ref.enumValues.slice());\n case 'union':\n return this.namedUnion(ref);\n default:\n this.unmappedFields.push(\n new UnmappedField(pointer, ref.unmappedText ?? '<unknown>'),\n );\n return new JsonObject();\n }\n }\n\n /**\n * A union, as a `$ref` at the NAMED alias when the model registered one.\n *\n * The model records a `type X = A | B` alias as its own entry — carrying the DERIVED\n * discriminator — while the FIELD that used it holds a bare union of branch names. Rendering the\n * field's union inline would therefore publish the `oneOf` and silently drop the discriminator,\n * which is the one part of a union a client actually needs to narrow on. So the alias is looked\n * up by its branch list and referenced.\n */\n private namedUnion(ref: TypeRef): JsonObject {\n const branches = ref.unionRefNames.join(',');\n for (const name of Array.from(this.types.keys())) {\n if (this.types.get(name)!.unionRefNames.join(',') === branches) {\n return this.reference(name);\n }\n }\n return new JsonObject().set(\n 'oneOf',\n ref.unionRefNames.map((each: string) => this.reference(each)),\n );\n }\n\n private primitive(ref: TypeRef): JsonObject {\n if (ref.primitive === 'unknown') {\n // No `type` at all: JSON Schema's honest spelling of \"any shape\". It is only ever reached\n // by a `void` return, which is why it is not the unmapped refusal.\n return new JsonObject();\n }\n return new JsonObject().set('type', ref.integer ? 'integer' : ref.primitive!);\n }\n\n /**\n * A `$ref` to the type registered under `key`, recording whether this document defines it or\n * references it in its declaring package's components document.\n */\n private reference(key: string): JsonObject {\n return new JsonObject().set('$ref', this.pointerTo(key));\n }\n\n private pointerTo(key: string): string {\n const type = this.types.get(key);\n if (type === undefined) {\n // Not in the model at all (a manifest naming an error type that does not exist): keep it\n // local so `components()` writes the hole the guards then name.\n this.reached.add(key);\n return `#/components/schemas/${key}`;\n }\n if (this.placement.isLocal(key, type)) {\n this.reached.add(key);\n } else {\n this.external.add(key);\n }\n return this.placement.pointer(key, type);\n }\n\n /** Empty prose is ABSENT prose. An empty `description` key is noise in every rendered page. */\n private prose(text: string | undefined): string | undefined {\n return text === undefined || text.trim() === '' ? undefined : text;\n }\n}\n"]}
|