fix(ai-openrouter): apply strict schema conversion inside $defs and definitions #2307
Openpcbeingused33(GitHub profile, opens in a new tab)wants to merge 2 commits into
main fromrefs/pull/2307/headopened View on GitHubtanstack-source added labelsbug
- @@ -68,0 +97,21 @@97+ */98+function definitionMaps(99+ original: JSONSchema,100+ wire: JSONSchema,101+): DefinitionMaps {102+ const maps: DefinitionMaps = new Map()103+ const slots: Array<{104+ map: NullWideningMap105+ properties: Record<string, NullWideningMap>106+ originalDef: JSONSchema | undefined107+ wireDef: JSONSchema | undefined108+ }> = []109+110+ for (const pool of ['$defs', 'definitions'] as const) {111+ const originalDefs = original[pool]112+ const wireDefs = wire[pool]113+ if (!originalDefs || !wireDefs) continue114+ for (const name of Object.keys(wireDefs)) {115+ const properties: Record<string, NullWideningMap> = {}116+ const map: NullWideningMap = { properties }117+ maps.set(`#/${pool}/${pointerToken(name)}`, map)@@ -68,0 +110,21 @@110+ for (const pool of ['$defs', 'definitions'] as const) {111+ const originalDefs = original[pool]112+ const wireDefs = wire[pool]113+ if (!originalDefs || !wireDefs) continue114+ for (const name of Object.keys(wireDefs)) {115+ const properties: Record<string, NullWideningMap> = {}116+ const map: NullWideningMap = { properties }117+ maps.set(`#/${pool}/${pointerToken(name)}`, map)118+ slots.push({119+ map,120+ properties,121+ originalDef: originalDefs[name],122+ wireDef: wireDefs[name],123+ })124+ }125+ }126+127+ for (const slot of slots) {128+ const filled = diffNullWidening(slot.originalDef, slot.wireDef, maps)129+ if (!filled) continue130+ if (filled.properties) Object.assign(slot.properties, filled.properties)@@ -71,10 +139,20 @@71139 * position where the converter added `null`. Diffing the actual wire schema72140 * keeps the map aligned with a subclass-supplied `schemaConverter`. A field73141 * that already allowed `null` (`.nullable()`, `.nullish()`) is not marked, so74- * its `null` survives.142+ * its `null` survives. `$ref` properties use the shared definition map, so143+ * nulls synthesized inside `$defs` / `definitions` are stripped too.75144 */76145 function diffNullWidening(77146 original: JSONSchema | undefined,78147 wire: JSONSchema | undefined,148+ maps?: DefinitionMaps,79149 ): NullWideningMap | undefined {80150 if (!original || !wire || typeof original !== 'object') return undefined151+ const defs = maps ?? definitionMaps(original, wire)152+ const source = unwrapSingleRefAllOf(original)153+ const sent = unwrapSingleRefAllOf(wire)154+155+ if (typeof source.$ref === 'string') {156+ const defMap = defs.get(source.$ref)157+ const widened = allowsNull(sent) && !allowsNull(source)158+ if (defMap) return widened ? { ...defMap, widened: true } : defMap@@ -75,8 +144,20 @@75144 */76145 function diffNullWidening(77146 original: JSONSchema | undefined,78147 wire: JSONSchema | undefined,148+ maps?: DefinitionMaps,79149 ): NullWideningMap | undefined {80150 if (!original || !wire || typeof original !== 'object') return undefined151+ const defs = maps ?? definitionMaps(original, wire)152+ const source = unwrapSingleRefAllOf(original)153+ const sent = unwrapSingleRefAllOf(wire)154+155+ if (typeof source.$ref === 'string') {156+ const defMap = defs.get(source.$ref)157+ const widened = allowsNull(sent) && !allowsNull(source)158+ if (defMap) return widened ? { ...defMap, widened: true } : defMap159+ return widened ? { widened: true } : undefined160+ }161+81162 const map: NullWideningMap = {}82- if (allowsNull(wire) && !allowsNull(original)) map.widened = true163+ if (allowsNull(sent) && !allowsNull(source)) map.widened = true@@ -0,0 +30,21 @@30+ if (Array.isArray(schema.enum) && !schema.enum.includes(null)) return false31+32+ if (typeof schema.type === 'string' && schema.type !== 'null') return false33+ if (Array.isArray(schema.type) && !schema.type.includes('null')) return false34+35+ if (36+ Array.isArray(schema.anyOf) &&37+ !schema.anyOf.some((variant: unknown) => acceptsNull(variant, root, seen))38+ ) {39+ return false40+ }41+42+ // An unresolvable or circular `$ref` is assumed to admit null.43+ if (typeof schema.$ref === 'string') {44+ const target = resolveRef(schema.$ref, root)45+ if (target !== undefined && !seen.has(target)) {46+ return acceptsNull(target, root, new Set(seen).add(target))47+ }48+ }49+50+ return true
github-actions[bot](GitHub profile, opens in a new tab) assignedjherr
github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: dde4d9e2630c622e7e271ba553835b6f8fa9e858
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR