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:
Paresh Chiramel 2024-01-02 11:25:42 +11:00 committed by GitHub
parent cfd27b1786
commit 9be08a1956
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 1 deletions

View File

@ -13,7 +13,10 @@ def _retrieve_ref(path: str, schema: dict) -> dict:
)
out = schema
for component in components[1:]:
out = out[component]
if component.isdigit():
out = out[int(component)]
else:
out = out[component]
return deepcopy(out)

View File

@ -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