Skip to content

Commit e0bd88c

Browse files
jnjacobsonhntrl
andauthored
fix: add support for conversion of ref in array schema (#8719)
Co-authored-by: Hunter Lovell <[email protected]>
1 parent 9e843eb commit e0bd88c

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

.changeset/puny-poems-sink.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"langchain": patch
3+
---
4+
5+
add support for conversion of ref in array schema

langchain/src/chains/openai_functions/openapi.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,11 @@ export function convertOpenAPISchemaToJSONSchema(
193193
);
194194
}
195195
if (schema.type === "array") {
196+
const openAPIItems = spec.getSchema(schema.items) ?? {};
197+
196198
return {
197199
type: "array",
198-
items: convertOpenAPISchemaToJSONSchema(schema.items ?? {}, spec),
200+
items: convertOpenAPISchemaToJSONSchema(openAPIItems, spec),
199201
minItems: schema.minItems,
200202
maxItems: schema.maxItems,
201203
} as JsonSchema7ArrayType;

langchain/src/chains/openai_functions/tests/openapi.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,21 @@ test("Test convert OpenAPI params to JSON Schema", async () => {
6969
},
7070
},
7171
},
72+
{
73+
name: "refParam",
74+
in: "query",
75+
schema: {
76+
$ref: "#/components/schemas/RefObject",
77+
},
78+
},
79+
{
80+
name: "refArrayParam",
81+
in: "query",
82+
schema: {
83+
type: "array",
84+
items: { $ref: "#/components/schemas/RefObject" },
85+
},
86+
},
7287
{
7388
name: "nestedObjectInArrayParam",
7489
in: "query",
@@ -147,6 +162,21 @@ test("Test convert OpenAPI params to JSON Schema", async () => {
147162
},
148163
},
149164
},
165+
components: {
166+
schemas: {
167+
RefObject: {
168+
type: "object",
169+
properties: {
170+
foo: {
171+
type: "string",
172+
},
173+
bar: {
174+
type: "number",
175+
},
176+
},
177+
},
178+
},
179+
},
150180
});
151181

152182
const createWidget = spec.getOperation(
@@ -225,6 +255,26 @@ test("Test convert OpenAPI params to JSON Schema", async () => {
225255
expect(typedStringArrayParamSchema.items).not.toBeUndefined();
226256
expectType("string", typedStringArrayParamSchema.items);
227257

258+
const refParamSchema = convertOpenAPISchemaToJSONSchema(
259+
getParamSchema(createWidget, "refParam"),
260+
spec
261+
);
262+
const typedRefParamSchema = expectType("object", refParamSchema);
263+
expectType("string", typedRefParamSchema.properties.foo);
264+
expectType("number", typedRefParamSchema.properties.bar);
265+
266+
const refArrayParamSchema = convertOpenAPISchemaToJSONSchema(
267+
getParamSchema(createWidget, "refArrayParam"),
268+
spec
269+
);
270+
const typedRefArrayParamSchema = expectType("array", refArrayParamSchema);
271+
const typedRefArrayParamSchemaItems = expectType(
272+
"object",
273+
typedRefArrayParamSchema.items
274+
);
275+
expectType("string", typedRefArrayParamSchemaItems.properties.foo);
276+
expectType("number", typedRefArrayParamSchemaItems.properties.bar);
277+
228278
const nestedObjectInArrayParamSchema = convertOpenAPISchemaToJSONSchema(
229279
getParamSchema(createWidget, "nestedObjectInArrayParam"),
230280
spec

0 commit comments

Comments
 (0)