Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not derive type of path parameter "id" but the path fragment is "pk" ? #194

Closed
eLvErDe opened this issue Nov 9, 2020 · 7 comments
Closed
Labels
bug Something isn't working

Comments

@eLvErDe
Copy link

eLvErDe commented Nov 9, 2020

Hello,

Could you help me understanding why spectacular says that ?

I have the following code snippets:

group_preference_multi_by_id = SelfGroupMultiPreferenceViewSet.as_view({
    "get": "retrieve",
    "put": "update",
    "delete": "destroy",
})

group_preference_urlpatterns = [
    path(
        "api/preferences/multi/by-group/<path:group>/<path:application>/<path:preference>/<int:pk>/",
        group_preference_multi_by_id,
        name="multi_preferences_by_given_group_by_id",
    ),
]

But the generated OpenAPI route uses "id" instead of "pk" and keep warning me that type could not be infered (and defaults to string while being actually integer).

Thanks in advance,

@tfranzel
Copy link
Owner

tfranzel commented Nov 9, 2020

Hi @eLvErDe,

this is a remnant of the base implementation. pk is coerced to id by a function belonging to DRF. there is even a DRF schema settings for that:

https://www.django-rest-framework.org/api-guide/settings/#schema_coerce_path_pk
https://github.com/encode/django-rest-framework/blob/bb795674f86828fc5f15d6d61501cc781811e053/rest_framework/schemas/generators.py#L210

typing path params should always work, but due to the pk coercion it fails here. the code is looking for id (but pk was typed in the path). i guess this counts as a bug.

however, this is usually not a problem as the type of pk/id is then retrieved from the model of the viewset, but i guess your SelfGroupMultiPreferenceViewSet is not a ModelViewSet or even a GenericAPIView.

@tfranzel tfranzel added the bug Something isn't working label Nov 9, 2020
@tfranzel
Copy link
Owner

@eLvErDe does this fix work for you?

@eLvErDe
Copy link
Author

eLvErDe commented Nov 10, 2020

@tfranzel Can you wait until thursday ?

@tfranzel
Copy link
Owner

normally yes but the fix is on master and i need to do a fast minor release, due to an unrelated bug in swagger ui. would be much appreciated if you could check it beforehand.

@eLvErDe
Copy link
Author

eLvErDe commented Nov 10, 2020

Ok, give me half an hour

@eLvErDe
Copy link
Author

eLvErDe commented Nov 10, 2020

Yes it works for me, warning is gone, path parameter is typed as integer and API is still working, so I guess your patch is OK, thanks :-)

@tfranzel
Copy link
Owner

thanks so much for doing it so quickly! release will happen very soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants