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

OpenAPI Schemas when request/response objects differ #54

Open
ChristopherChudzicki opened this issue Aug 25, 2023 · 1 comment
Open

Comments

@ChristopherChudzicki
Copy link
Contributor

When constructing an OpenAPI schema for a ModelViewSet, drf-spectacular has particular difficulty when a serializer field treats request/response data differently. An example of this is LearningResource.topics: Responses serializer topics as objects, but requests (assuming #34 is merged) expect integer topic ids or objects:

// GET /api/v1/learningpaths/39/
{
  "id": 39,
  "topics": [
      { "id": 15, "name": "Systems Engineering" },
      { "id": 12, "name": "Physics" }
  ]
  // ...
}
// POST /api/v1/learningpaths/
{
  "topics": [15, 12] // Or objects, though IDs are seem convenient
}

Issue: But drf-spectacular reports topics as an object in both cases.

@ChristopherChudzicki ChristopherChudzicki added the bug Something isn't working label Aug 25, 2023
@ChristopherChudzicki
Copy link
Contributor Author

ChristopherChudzicki commented Aug 25, 2023

Any solution to this will require enabling COMPONENT_SPLIT_REQUEST as a first step; we should do that anyway, but it is not sufficient to resolve this issue.

Then, I can think of a few ways to deal with the current spec's inaccurate modeling:

  1. Decide we don't care: Just use full topic objects when making POST/PATCH etc requests.
  2. Use separate fields for requests/responses
    • topics: obj[], read-only for responses
    • topic_ids: int[], write-only for requests
  3. Possibly fixable by anotating view methods with @extend_schema and separate request/response serializers. This would be quite manual, I think, and require creating those separate serializers.
  4. Use a custom field meant to handle this case, and register the custom field as an extension in drf-spectacular.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant