Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Failing OpenAPI tests with python3.9 #38

Closed
akhait opened this issue Jun 23, 2020 · 0 comments
Closed

Failing OpenAPI tests with python3.9 #38

akhait opened this issue Jun 23, 2020 · 0 comments

Comments

@akhait
Copy link

akhait commented Jun 23, 2020

Hello,
I'd like to address 2 issues I had with running python -m pytest:

molten version: 1.0.1
typing_inspect version: 0.6.0
pytest version: 5.4.3
python version: 3.9.0b3

  1. Inmolten/openapi/documents.py:334, in _generate_field_schema function:
origin = get_origin(annotation)`

get_origin doesn't support typing.Dict, so will return None source. I'm not sure why, but that's exactly the case with test_complex_apps_can_return_openapi_document test:

_______________ test_complex_apps_can_return_openapi_document ________________

    def test_complex_apps_can_return_openapi_document():
        # Given that I have a complex app
        # When I visit its schema uri
        response = testing.TestClient(app).get("/schema.json")

        # Then I should get back a successful response
>       assert response.status_code == 200
E       assert 500 == 200
E         +500
E         -200

tests/openapi/test_openapi.py:216: AssertionError
----------------------------- Captured log call ------------------------------
ERROR    molten.app:app.py:139 An unhandled exception occurred.
Traceback (most recent call last):
  File "/root/molten-1.0.1/molten/app.py", line 192, in __call__
    response = handler()
  File "/root/molten-1.0.1/molten/dependency_injection.py", line 191, in resolved_fn
    return fn(**params)
  File "/root/molten-1.0.1/molten/middleware.py", line 34, in handle
    response = handler()
  File "/root/molten-1.0.1/molten/dependency_injection.py", line 191, in resolved_fn
    return fn(**params)
  File "/root/molten-1.0.1/molten/openapi/handlers.py", line 65, in __call__
    self.document = generate_openapi_document(
  File "/root/molten-1.0.1/molten/openapi/documents.py", line 210, in generate_openapi_document
    request_schema_name = _generate_schema(annotation, schemas)
  File "/root/molten-1.0.1/molten/openapi/documents.py", line 315, in _generate_schema
    is_optional, field_schema = _generate_field_schema(field_name, field, schemas)
  File "/root/molten-1.0.1/molten/openapi/documents.py", line 352, in _generate_field_schema
    raise ValueError(f"Unsupported type {origin} for field {field.name!r}.")
ValueError: Unsupported type None for field 'metadata'.

I was able to work this around with this patch:

--- molten/openapi/documents.py   	2020-06-23 11:08:41.914220550 -0400
+++ molten/openapi/documents.py	2020-06-23 11:08:59.673220550 -0400
@@ -332,6 +332,8 @@
 
     elif is_generic_type(annotation):
         origin = get_origin(annotation)
+        if origin is None:
+                origin = annotation
         if origin in _LIST_TYPES:
             arguments = get_args(annotation)
             if arguments and is_schema(arguments[0]):

  1. After applying the patch posted above, 2 test cases still fail:
FAILED tests/openapi/test_openapi.py::test_complex_apps_can_return_openapi_document
FAILED tests/openapi/test_openapi.py::test_openapi_can_render_lists_of_x[fields2-expected2]

I guess expected values for those test cases have to be updated. Posting truncated pytest output with diff for expected vs actual output bellow.

Thanks in advance

______________________________________________________ test_complex_apps_can_return_openapi_document ________________________________________________________

    def test_complex_apps_can_return_openapi_document():
        # Given that I have a complex app
        # When I visit its schema uri
        response = testing.TestClient(app).get("/schema.json")

        # Then I should get back a successful response
        assert response.status_code == 200

        with open("tests/openapi/fixtures/complex.json") as f:
>           assert response.json() == json.load(f)
E           AssertionError: assert {'components': {'schemas': {'tests.openapi.test_openapi.Category': {'properties': {'id': {'format': 'int64',\n
...
E                                           'tests.openapi.test_openapi.Settings': {'properties': {'last_updated_at': {'type': 'string'},
E             -                                                                                    'xs': {'items': {'description': 'Can '
E             -                                                                                                                    'be '
E             -                                                                                                                    'any '
E             -                                                                                                                    'value, '
E             -                                                                                                                    'including '
E             -                                                                                                                    'null.',
E             -                                                                                                     'nullable': True},
E             -                                                                                           'type': 'array'}},
E             ?                                                                                    ^^^^^^^         ^ ^^^
E             +                                                                                    'xs': {'type': 'string'}},
E             ?                                                                                    ^^^^^^^         ^^ ^^^
E                                                                                   'required': ['last_updated_at'],
...
tests/openapi/test_openapi.py:219: AssertionError
___________________________________________________ test_openapi_can_render_lists_of_x[fields2-expected2] ____________________________________________________

fields = {'xs': typing.List[typing.List]}
expected = {'xs': {'items': {'items': {'description': 'Can be any value, including null.', 'nullable': True}, 'type': 'array'}, 'type': 'array'}}

    @pytest.mark.parametrize("fields,expected", [
        (
            {"xs": List[str]},
            {"xs": {"type": "array", "items": {"type": "string"}}},
        ),
        (
            {"xs": List[List[str]]},
            {"xs": {"type": "array", "items": {"type": "array", "items": {"type": "string"}}}},
        ),
        (
            {"xs": List[List]},
            {"xs": {"type": "array", "items": {"type": "array", "items": {
                "description": "Can be any value, including null.",
                "nullable": True,
            }}}},
        )
    ])
    def test_openapi_can_render_lists_of_x(fields, expected):
        # Given that I have a schema that has a list of something in it
        A = type("A", (object,), fields)
        A.__annotations__ = fields
        A = schema(A)

        def index() -> A:
            pass

        # And an app
        app = App(routes=[Route("/", index)])

        # When I generate a document
        document = generate_openapi_document(app, Metadata("example", "an example", "0.0.0"), [])

        # Then the return schema should have an array of that thing
        response_schema = document["components"]["schemas"]["tests.openapi.test_openapi.A"]
>       assert response_schema["properties"] == expected
E       AssertionError: assert {'xs': {'items': {'type': 'string'}, 'type': 'array'}} == {'xs': {'items': {'items': {'description': 'Can be any value, including null.',\n                            'nullable': True},\n                  'type': 'array'},\n        'type': 'array'}}
E         Differing items:
E         {'xs': {'items': {'type': 'string'}, 'type': 'array'}} != {'xs': {'items': {'items': {'description': 'Can be any value, including null.', 'nullable': True}, 'type': 'array'}, 'type': 'array'}}
E         Full diff:
E           {
E         +  'xs': {'items': {'type': 'string'},
E         -  'xs': {'items': {'items': {'description': 'Can be any value, including null.',
E         -                             'nullable': True},
E         -                   'type': 'array'},
E                   'type': 'array'},
E           }

tests/openapi/test_openapi.py:268: AssertionError
================================================================== short test summary info ===================================================================
FAILED tests/openapi/test_openapi.py::test_complex_apps_can_return_openapi_document - AssertionError: assert {'components': {'schemas': {'tests.openapi.tes...
FAILED tests/openapi/test_openapi.py::test_openapi_can_render_lists_of_x[fields2-expected2] - AssertionError: assert {'xs': {'items': {'type': 'string'}, '...
=============================================================== 2 failed, 239 passed in 0.90s ================================================================
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant