Skip to content
This repository has been archived by the owner on Nov 19, 2023. It is now read-only.

related_field not recognised in URL path #286

Open
darduf opened this issue Nov 23, 2022 · 7 comments
Open

related_field not recognised in URL path #286

darduf opened this issue Nov 23, 2022 · 7 comments

Comments

@darduf
Copy link

darduf commented Nov 23, 2022

We are using RelationshipView from the DRF JSON:API package, and are experiencing limitations when using the schema tester on these type of endpoints.

We have the following path/view that handles the application of team members (POST/DELETE)

    re_path(
        r"^team/(?P<pk>\d+)/relationships/(?P<related_field>[-\w]+)/?$",
        views.TeamMembersRelationshipView.as_view(),
        name="team-members-relation",
    ),

When SchemaTester(schema_file_path=path/to/schemae/file.yaml) hits this path

        response = self.client.delete(
            reverse(
                "teams:team-members-relation",
                kwargs={
                    "pk": self.team_001.pk,
                    "related_field": "members",
                },
            ),
            payload,
            content_type="application/vnd.api+json",
        )
        self.assertResponse(response)

We receive an UndocumentedSchemaSectionError

Traceback (most recent call last):
  File "../.venv/lib/python3.9/site-packages/openapi_tester/schema_tester.py", line 102, in get_key_value
    return schema[key]
KeyError: '/api/team/{id}/relationships/{related_field}'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "../.venv/lib/python3.9/site-packages/openapi_tester/schema_tester.py", line 403, in validate_response
    response_schema = self.get_response_schema_section(response)
  File "../.venv/lib/python3.9/site-packages/openapi_tester/schema_tester.py", line 144, in get_response_schema_section
    route_object = self.get_key_value(
  File "../.venv/lib/python3.9/site-packages/openapi_tester/schema_tester.py", line 104, in get_key_value
    raise UndocumentedSchemaSectionError(
openapi_tester.exceptions.UndocumentedSchemaSectionError: Error: Unsuccessfully tried to index the OpenAPI schema by `/api/team/{id}/relationships/{related_field}`. 

Undocumented route /api/team/{id}/relationships/{related_field}.

Documented routes:
        • ...
        • /api/teams
        • /api/team/{id}
        • /api/team/{id}/relationships/members

Why isn't the (undocumented) path resolving /api/team/{id}/relationships/{related_field} to the (documented) desired path /api/team/{id}/relationships/members as expected?
Is there something additional that needs to be defined in order for the schema tester to recognise these related_field path keys?

@sondrelg
Copy link
Member

This is the relevant code tasked with resolving paths: https://github.com/snok/drf-openapi-tester/blob/master/openapi_tester/loaders.py#L141

My guess is that the answer will be found there if you take a look. A PR with a fix is, as always, welcome 🙂 I could also take a look at this at some point, but would need reproducible code in some form or another

@darduf
Copy link
Author

darduf commented Nov 23, 2022

I could also take a look at this at some point, but would need reproducible code in some form or another

Thanks for your reply. I will try to push reproducible code as you have described here

@darduf
Copy link
Author

darduf commented Nov 23, 2022

I was able to reproduce this locally, however when I push to the PR it fails. Seems it cannot import rest_framework_json_api module?
Example:
FAILED tests/test_django_framework.py::TeamsAPITests::test_schema_using_assert_response - ModuleNotFoundError: No module named 'rest_framework_json_api'

Could you please have a look and assist me in how to get this operational. Thanks

@sondrelg
Copy link
Member

I'll try to take a look at both issues tomorrow 👍

@darduf
Copy link
Author

darduf commented Nov 24, 2022

@sondrelg I fixed the failing test in relation to the missing rest_framework_json_api module. (I updated .github/workflows/testing.yml and .pre-commit-config.yaml)

The latest commit failing in the PR now reproduces the error.

I have updated the PR description with latest.

@darduf
Copy link
Author

darduf commented Dec 1, 2022

@sondrelg Did you get the opportunity to look into this issue, from the reproducing PR?

@sondrelg
Copy link
Member

sondrelg commented Dec 7, 2022

Hi @darduf, thanks for pushing the reproducible code. That's helpful. Unfortunately, I do not have time to look at this in the near future, so I would suggest looking at it yourself if you need this fixed.

I'm happy to answer questions and guide you if there's anything you need. Just let me know 👍

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

2 participants