Skip to content

Commit

Permalink
Fixes util.deep_get when obj is a list (#1055)
Browse files Browse the repository at this point in the history
* Fixes util.deep_get when obj is a list

The example below will seem strange. It's the result of distilling a complex
API spec down to the root cause of the issue.

```yaml
---
openapi: 3.0.0
info:
  version: v0
  title: Repo an error
  description: Extremely contrived, but it illustrates the problem
paths:
  /:
    get:
      operationId: app.get
      responses:
        '200':
          description: Just for giggles
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/SomeWeirdResource"
components:
  schemas:
    Resource:
      allOf:
        - type: object
          properties:
            id:
              type: string
    SomeWeirdResource:
      allOf:
        - type: object
          properties:
            id:
              $ref: '#/components/schemas/Resource/allOf/0/properties/id'
```

While convoluted, this is a valid OpenAPI 3 spec.

```sh
$ yarn swagger-cli validate openapi/my_api.yaml
yarn run v1.17.3
$ /[redacted]/node_modules/.bin/swagger-cli validate openapi/my_api.yaml
openapi/my_api.yaml is valid
✨  Done in 0.23s.
```

`utils.deep_get` assumes the obj to be a dictionary when it can also be a list.

* Updates docstring for utils.deep_get

Comment on the pull request was asking for an explanation to clarify the
code. The updated docstring aims to be clear on how exactly the object
can be a list.
  • Loading branch information
ornj authored and hjacobs committed Oct 16, 2019
1 parent bd2552c commit 43003f8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
12 changes: 11 additions & 1 deletion connexion/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,20 @@ def deep_getattr(obj, attr):
def deep_get(obj, keys):
"""
Recurses through a nested object get a leaf value.
There are cases where the use of inheritance or polymorphism-- the use of allOf or
oneOf keywords-- will cause the obj to be a list. In this case the keys will
contain one or more strings containing integers.
:type obj: list or dict
:type keys: list of strings
"""
if not keys:
return obj
return deep_get(obj[keys[0]], keys[1:])
try:
return deep_get(obj[int(keys[0])], keys[1:])
except ValueError:
return deep_get(obj[keys[0]], keys[1:])


def get_function_from_name(function_name):
Expand Down
10 changes: 10 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,13 @@ def test_boolean():

with pytest.raises(ValueError):
utils.boolean(None)


def test_deep_get_dict():
obj = {'type': 'object', 'properties': {'id': {'type': 'string'}}}
assert utils.deep_get(obj, ['properties', 'id']) == {'type': 'string'}


def test_deep_get_list():
obj = [{'type': 'object', 'properties': {'id': {'type': 'string'}}}]
assert utils.deep_get(obj, ['0', 'properties', 'id']) == {'type': 'string'}

0 comments on commit 43003f8

Please sign in to comment.