mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-10 21:35:08 +00:00
Update _retrieve_ref inside json_schema.py to include an isdigit() check (#14745)
- **Description:** Update _retrieve_ref inside json_schema.py to include an isdigit() check - **Issue:** This library is used inside dereference_refs inside langchain_community.agent_toolkits.openapi.spec. When I read in a yaml file which has references for "400", "401" etc; the line "out = out[component]" causes a KeyError. The isdigit() check ensures that if it is an integer like "400" or "401"; it converts it into integer before using it as a key to prevent the error. - **Dependencies:** No dependencies - **Tag maintainer:** @baskaryan --------- Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
This commit is contained in:
parent
cfd27b1786
commit
9be08a1956
@ -13,6 +13,9 @@ def _retrieve_ref(path: str, schema: dict) -> dict:
|
||||
)
|
||||
out = schema
|
||||
for component in components[1:]:
|
||||
if component.isdigit():
|
||||
out = out[int(component)]
|
||||
else:
|
||||
out = out[component]
|
||||
return deepcopy(out)
|
||||
|
||||
|
@ -149,3 +149,35 @@ def test_dereference_refs_remote_ref() -> None:
|
||||
}
|
||||
with pytest.raises(ValueError):
|
||||
dereference_refs(schema)
|
||||
|
||||
|
||||
def test_dereference_refs_integer_ref() -> None:
|
||||
schema = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"error_400": {"$ref": "#/$defs/400"},
|
||||
},
|
||||
"$defs": {
|
||||
400: {
|
||||
"type": "object",
|
||||
"properties": {"description": "Bad Request"},
|
||||
},
|
||||
},
|
||||
}
|
||||
expected = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"error_400": {
|
||||
"type": "object",
|
||||
"properties": {"description": "Bad Request"},
|
||||
},
|
||||
},
|
||||
"$defs": {
|
||||
400: {
|
||||
"type": "object",
|
||||
"properties": {"description": "Bad Request"},
|
||||
},
|
||||
},
|
||||
}
|
||||
actual = dereference_refs(schema)
|
||||
assert actual == expected
|
||||
|
Loading…
Reference in New Issue
Block a user