genoc 0.5.1 → 0.5.2
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.
|
@@ -49,10 +49,18 @@ export interface DiscriminatorVariantEntry {
|
|
|
49
49
|
isNullable: boolean;
|
|
50
50
|
/** Owning family id — the discriminator base's raw component name. */
|
|
51
51
|
familyId: string;
|
|
52
|
-
/** The exact `$ref` string that produced this entry (mapping value
|
|
52
|
+
/** The exact `$ref` string that produced this entry (mapping value, oneOf/anyOf member ref, canonical base ref, or implicit allOf child ref). */
|
|
53
53
|
refStr: string;
|
|
54
54
|
/** Rename-aware type name for the ref (same derivation as the mapper's `typeNameGenerator`). */
|
|
55
55
|
typeName: string;
|
|
56
|
+
/**
|
|
57
|
+
* True for the family base's own entry — the base participates in its own
|
|
58
|
+
* family union (self-mapped, or no `oneOf`/`anyOf` of its own). Consumers:
|
|
59
|
+
* same-family-spine base-ref translation routes through the entry seam
|
|
60
|
+
* (`Omit<Base, P>`), and the mapper skips the `{ P: string }` placeholder on
|
|
61
|
+
* the base's own definition because the literal replaces it.
|
|
62
|
+
*/
|
|
63
|
+
isBaseMember: boolean;
|
|
56
64
|
}
|
|
57
65
|
/**
|
|
58
66
|
* One discriminator family: a component schema declaring `discriminator`
|
|
@@ -73,7 +81,7 @@ export interface DiscriminatorFamily {
|
|
|
73
81
|
propertyName: string;
|
|
74
82
|
/** Collision-safe `{Base}Variant` union name (D6 — via the reserved-name machinery). */
|
|
75
83
|
variantUnionName: string;
|
|
76
|
-
/** Union member type names (rename-aware, deduped;
|
|
84
|
+
/** Union member type names (rename-aware, deduped; entry insertion order: base, mapping, unmapped oneOf/anyOf refs, implicit allOf children). */
|
|
77
85
|
variantUnionMembers: string[];
|
|
78
86
|
/** Family entries keyed by the FULL `$ref` string (mapping value or oneOf/anyOf member ref). */
|
|
79
87
|
entriesByRef: Map<string, DiscriminatorVariantEntry>;
|
|
@@ -103,9 +103,36 @@ export function buildDiscriminatorRegistry(schemas, resolver, renameMap, allSche
|
|
|
103
103
|
};
|
|
104
104
|
if (!schemas)
|
|
105
105
|
return registry;
|
|
106
|
-
//
|
|
107
|
-
//
|
|
108
|
-
// the
|
|
106
|
+
// Implicit allOf children (issue #93): a schema whose `allOf` references a
|
|
107
|
+
// family base DIRECTLY is a variant of that family even when `mapping` and
|
|
108
|
+
// the base's own oneOf/anyOf leave it out. Index childName → the set of
|
|
109
|
+
// canonical `#/components/schemas/…` refs it allOfs, once, up front —
|
|
110
|
+
// grandchildren (allOf a child, not the base) stay out by construction.
|
|
111
|
+
const allOfBasesByChild = new Map();
|
|
112
|
+
for (const [childName, childSchema] of Object.entries(schemas)) {
|
|
113
|
+
let childResolved;
|
|
114
|
+
try {
|
|
115
|
+
childResolved = resolver.resolve(childSchema);
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (!childResolved.allOf)
|
|
121
|
+
continue;
|
|
122
|
+
const bases = new Set();
|
|
123
|
+
for (const member of childResolved.allOf) {
|
|
124
|
+
if (!isRefObject(member) || !member.$ref.startsWith('#/components/schemas/'))
|
|
125
|
+
continue;
|
|
126
|
+
bases.add(`#/components/schemas/${encodePointerSegment(rawRefSegment(member.$ref))}`);
|
|
127
|
+
}
|
|
128
|
+
if (bases.size > 0)
|
|
129
|
+
allOfBasesByChild.set(childName, bases);
|
|
130
|
+
}
|
|
131
|
+
// Pass 1 — discover families; per family, register the base's own entry
|
|
132
|
+
// when it joins its family (self-mapped, or no oneOf/anyOf of its own),
|
|
133
|
+
// then mapping entries (D3: mapping key wins), then implicit oneOf/anyOf
|
|
134
|
+
// variants absent from the mapping (literal = own const > single-value
|
|
135
|
+
// enum > RAW ref segment), then implicit allOf children absent from both.
|
|
109
136
|
for (const [name, schema] of Object.entries(schemas)) {
|
|
110
137
|
const resolved = resolver.resolve(schema);
|
|
111
138
|
const disc = resolved.discriminator;
|
|
@@ -121,12 +148,46 @@ export function buildDiscriminatorRegistry(schemas, resolver, renameMap, allSche
|
|
|
121
148
|
variantUnionMembers: [],
|
|
122
149
|
entriesByRef: new Map(),
|
|
123
150
|
};
|
|
151
|
+
// Base membership (#93): a CONCRETE base (no oneOf/anyOf of its own) is a
|
|
152
|
+
// variant of its own family even when `mapping` omits it — with no other
|
|
153
|
+
// members the family would otherwise reference an empty union. A base
|
|
154
|
+
// listed in its own mapping joins too (its key wins the literal, D3).
|
|
155
|
+
// Insertion order matters: the base entry lands FIRST so union members
|
|
156
|
+
// read `{Base} | <mapped…> | <implicit…>`.
|
|
157
|
+
const baseHasOwnUnion = (resolved.oneOf?.length ?? 0) > 0 || (resolved.anyOf?.length ?? 0) > 0;
|
|
158
|
+
const selfMappingKey = disc.mapping
|
|
159
|
+
? Object.entries(disc.mapping).find(([, refStr]) => refStr === family.baseRefStr)?.[0]
|
|
160
|
+
: undefined;
|
|
161
|
+
if (selfMappingKey !== undefined || !baseHasOwnUnion) {
|
|
162
|
+
const ownBaseLiteral = resolveOwnDiscriminatorLiteral(resolved, disc.propertyName, resolver);
|
|
163
|
+
if (selfMappingKey !== undefined &&
|
|
164
|
+
ownBaseLiteral !== undefined &&
|
|
165
|
+
ownBaseLiteral.value !== selfMappingKey) {
|
|
166
|
+
warn(`Warning: Discriminator mapping key "${selfMappingKey}" for "${family.baseRefStr}" conflicts with its own declared ${ownBaseLiteral.source} literal "${ownBaseLiteral.value}"; the mapping key wins.\n`);
|
|
167
|
+
}
|
|
168
|
+
family.entriesByRef.set(family.baseRefStr, {
|
|
169
|
+
propertyName: disc.propertyName,
|
|
170
|
+
// Base literal precedence: self-mapping key > own const/enum > raw
|
|
171
|
+
// component name (the runtime value, NOT the sanitized type name).
|
|
172
|
+
literalValue: escapeStringLiteral(selfMappingKey ?? ownBaseLiteral?.value ?? name),
|
|
173
|
+
isNamed: true, // the base is by definition an emitted component schema
|
|
174
|
+
isUnion: baseHasOwnUnion,
|
|
175
|
+
isNullable: resolved.nullable === true ||
|
|
176
|
+
(Array.isArray(resolved.type) ? resolved.type.includes('null') : false),
|
|
177
|
+
isBaseMember: true,
|
|
178
|
+
familyId,
|
|
179
|
+
refStr: family.baseRefStr,
|
|
180
|
+
typeName: family.baseTypeName,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
124
183
|
const mappedRefStrs = new Set();
|
|
125
184
|
if (disc.mapping) {
|
|
126
185
|
for (const [mappingKey, refStr] of Object.entries(disc.mapping)) {
|
|
127
186
|
if (mappedRefStrs.has(refStr))
|
|
128
187
|
continue; // two keys → same target: first key wins
|
|
129
188
|
mappedRefStrs.add(refStr);
|
|
189
|
+
if (family.entriesByRef.has(refStr))
|
|
190
|
+
continue; // base self-mapping — the base entry already carries it
|
|
130
191
|
const target = resolveTargetSchema(resolver, refStr);
|
|
131
192
|
const own = resolveOwnDiscriminatorLiteral(target, disc.propertyName, resolver);
|
|
132
193
|
if (own !== undefined && own.value !== mappingKey) {
|
|
@@ -140,6 +201,7 @@ export function buildDiscriminatorRegistry(schemas, resolver, renameMap, allSche
|
|
|
140
201
|
isUnion: (target?.oneOf?.length ?? 0) > 0 || (target?.anyOf?.length ?? 0) > 0,
|
|
141
202
|
isNullable: target?.nullable === true ||
|
|
142
203
|
(Array.isArray(target?.type) ? target.type.includes('null') : false),
|
|
204
|
+
isBaseMember: false,
|
|
143
205
|
familyId,
|
|
144
206
|
refStr,
|
|
145
207
|
typeName: renameAwareTypeName(renameMap, rawSegment),
|
|
@@ -153,8 +215,8 @@ export function buildDiscriminatorRegistry(schemas, resolver, renameMap, allSche
|
|
|
153
215
|
implicitRefs.push(member.$ref);
|
|
154
216
|
}
|
|
155
217
|
for (const refStr of implicitRefs) {
|
|
156
|
-
if (mappedRefStrs.has(refStr))
|
|
157
|
-
continue; // already registered
|
|
218
|
+
if (mappedRefStrs.has(refStr) || family.entriesByRef.has(refStr))
|
|
219
|
+
continue; // already registered
|
|
158
220
|
const target = resolveTargetSchema(resolver, refStr);
|
|
159
221
|
const own = resolveOwnDiscriminatorLiteral(target, disc.propertyName, resolver);
|
|
160
222
|
const rawSegment = rawRefSegment(refStr);
|
|
@@ -168,17 +230,46 @@ export function buildDiscriminatorRegistry(schemas, resolver, renameMap, allSche
|
|
|
168
230
|
isUnion: (target?.oneOf?.length ?? 0) > 0 || (target?.anyOf?.length ?? 0) > 0,
|
|
169
231
|
isNullable: target?.nullable === true ||
|
|
170
232
|
(Array.isArray(target?.type) ? target.type.includes('null') : false),
|
|
233
|
+
isBaseMember: false,
|
|
171
234
|
familyId,
|
|
172
235
|
refStr,
|
|
173
236
|
typeName: renameAwareTypeName(renameMap, rawSegment),
|
|
174
237
|
};
|
|
175
238
|
family.entriesByRef.set(refStr, entry);
|
|
176
239
|
}
|
|
240
|
+
// Implicit allOf children (#93): direct `allOf` refs to the base count as
|
|
241
|
+
// variants even when `mapping` leaves them out; anything already present
|
|
242
|
+
// (mapping target, implicit oneOf/anyOf ref, the base itself) is skipped,
|
|
243
|
+
// so children land in component order after the mapped/implicit members.
|
|
244
|
+
for (const [childName, bases] of allOfBasesByChild) {
|
|
245
|
+
if (!bases.has(family.baseRefStr))
|
|
246
|
+
continue;
|
|
247
|
+
const childRefStr = `#/components/schemas/${encodePointerSegment(childName)}`;
|
|
248
|
+
if (family.entriesByRef.has(childRefStr))
|
|
249
|
+
continue;
|
|
250
|
+
const target = resolveTargetSchema(resolver, childRefStr);
|
|
251
|
+
const own = resolveOwnDiscriminatorLiteral(target, disc.propertyName, resolver);
|
|
252
|
+
const entry = {
|
|
253
|
+
propertyName: disc.propertyName,
|
|
254
|
+
// Same D3 fallback chain as unmapped oneOf/anyOf variants.
|
|
255
|
+
literalValue: escapeStringLiteral(own?.value ?? childName),
|
|
256
|
+
isNamed: allSchemaNames.has(renameAwareTypeName(renameMap, childName)),
|
|
257
|
+
isUnion: (target?.oneOf?.length ?? 0) > 0 || (target?.anyOf?.length ?? 0) > 0,
|
|
258
|
+
isNullable: target?.nullable === true ||
|
|
259
|
+
(Array.isArray(target?.type) ? target.type.includes('null') : false),
|
|
260
|
+
isBaseMember: false,
|
|
261
|
+
familyId,
|
|
262
|
+
refStr: childRefStr,
|
|
263
|
+
typeName: renameAwareTypeName(renameMap, childName),
|
|
264
|
+
};
|
|
265
|
+
family.entriesByRef.set(childRefStr, entry);
|
|
266
|
+
}
|
|
177
267
|
registry.families.set(familyId, family);
|
|
178
268
|
}
|
|
179
269
|
// Pass 2 — flat lookups + per-family name index + union members
|
|
180
|
-
// (
|
|
181
|
-
//
|
|
270
|
+
// (entry insertion order: base, then mapping, then unmapped oneOf/anyOf
|
|
271
|
+
// refs, then implicit allOf children; deduped by refStr and by typeName
|
|
272
|
+
// — alias refs to one emitted name collapse).
|
|
182
273
|
for (const family of registry.families.values()) {
|
|
183
274
|
const memberNames = [];
|
|
184
275
|
const seenNames = new Set();
|
|
@@ -76,6 +76,7 @@ function spineForEntry(entry) {
|
|
|
76
76
|
familyId: entry.familyId,
|
|
77
77
|
propertyName: entry.propertyName,
|
|
78
78
|
literalValue: entry.literalValue,
|
|
79
|
+
isBaseMember: entry.isBaseMember,
|
|
79
80
|
};
|
|
80
81
|
}
|
|
81
82
|
/**
|
|
@@ -354,9 +355,21 @@ export class SchemaMapper {
|
|
|
354
355
|
const canonical = this.canonicalSchemasRef(refStr);
|
|
355
356
|
const baseFamily = registry.baseRefs.get(refStr) ?? registry.baseRefs.get(canonical);
|
|
356
357
|
if (baseFamily) {
|
|
358
|
+
const baseEntry = baseFamily.entriesByRef.get(baseFamily.baseRefStr);
|
|
359
|
+
if (spine && spine.familyId === baseFamily.familyId && baseEntry) {
|
|
360
|
+
// Member base inside its own family spine (#93): the base joins as a
|
|
361
|
+
// variant through the entry seam — `Omit<Base, P>` — so the child's
|
|
362
|
+
// literal append never collides with the base's own.
|
|
363
|
+
return this.mapSameFamilySpineRef(baseEntry, context, visited, indent);
|
|
364
|
+
}
|
|
357
365
|
if (spine && spine.familyId === baseFamily.familyId) {
|
|
358
366
|
return { tsType: baseFamily.baseTypeName, imports: [baseFamily.baseTypeName] };
|
|
359
367
|
}
|
|
368
|
+
// Empty family (#93): no members at all — refs fall back to the bare
|
|
369
|
+
// base name instead of an undeclared `{Base}Variant` union.
|
|
370
|
+
if (baseFamily.variantUnionMembers.length === 0) {
|
|
371
|
+
return { tsType: baseFamily.baseTypeName, imports: [baseFamily.baseTypeName] };
|
|
372
|
+
}
|
|
360
373
|
return { tsType: baseFamily.variantUnionName, imports: [baseFamily.variantUnionName] };
|
|
361
374
|
}
|
|
362
375
|
const entry = registry.byRef.get(refStr) ?? registry.byRef.get(canonical);
|
|
@@ -650,7 +663,9 @@ export class SchemaMapper {
|
|
|
650
663
|
const memberIndent = indentBy(indent + 1);
|
|
651
664
|
const memberLines = [];
|
|
652
665
|
const allImports = [];
|
|
653
|
-
if (schema.discriminator) {
|
|
666
|
+
if (schema.discriminator && !spine?.isBaseMember) {
|
|
667
|
+
// A member base's placeholder (`"${discPropName}": string;`) is skipped:
|
|
668
|
+
// its literal append replaces the declared property outright (#93).
|
|
654
669
|
const discPropName = schema.discriminator.propertyName;
|
|
655
670
|
const discIndex = filteredPropNames.indexOf(discPropName);
|
|
656
671
|
if (discIndex !== -1) {
|