oncall-engine/engine/engine/schema.py
Vadim Stepanov d0904ca405
Improve OpenAPI schema coverage (#3629)
# What this PR does

Improves OpenAPI schema coverage for internal API:

- Fixes/Improves `alert group` and `feature` endpoints
- Adds `integration` and `user` endpoints

## Which issue(s) this PR fixes

https://github.com/grafana/oncall/issues/3444

## Checklist

- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)
2024-01-12 15:11:22 +00:00

47 lines
1.8 KiB
Python

from drf_spectacular.openapi import AutoSchema
from drf_spectacular.plumbing import get_view_model
from common.api_helpers.mixins import PublicPrimaryKeyMixin
class CustomAutoSchema(AutoSchema):
def _get_serializer(self):
"""Makes so that extra actions (@action on viewset) don't inherit the serializer from the viewset."""
if self._is_extra_action:
return None
return super()._get_serializer()
def _get_paginator(self):
"""Makes so that extra actions (@action on viewset) don't inherit the paginator from the viewset."""
if self._is_extra_action:
return None
return super()._get_paginator()
def get_filter_backends(self):
"""Makes so that extra actions (@action on viewset) don't inherit the filter backends from the viewset."""
if self._is_extra_action:
return []
return super().get_filter_backends()
def _resolve_path_parameters(self, variables):
"""A workaround to make public primary keys appear as strings in the OpenAPI schema."""
parameters = super()._resolve_path_parameters(variables)
if not isinstance(self.view, PublicPrimaryKeyMixin):
return parameters
for parameter in parameters:
if parameter["name"] == "id" and parameter["in"] == "path":
parameter["schema"]["type"] = "string"
model = get_view_model(self.view, emit_warnings=False)
model_name = model._meta.verbose_name if model else "resource"
parameter["description"] = f"A string identifying this {model_name}."
return parameters
@property
def _is_extra_action(self) -> bool:
try:
return self.view.action in [action.__name__ for action in self.view.get_extra_actions()]
except AttributeError:
return False