-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ [#175] added expand for detail endpoints in redoc
- Loading branch information
1 parent
0f7d5ba
commit 49ac3bd
Showing
8 changed files
with
192 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# SPDX-License-Identifier: EUPL-1.2 | ||
# Copyright (C) 2019 - 2020 Dimpact | ||
import logging | ||
from typing import List | ||
|
||
from django.conf import settings | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from drf_spectacular.openapi import AutoSchema as _AutoSchema | ||
from drf_spectacular.utils import OpenApiParameter | ||
from furl import furl | ||
from rest_framework import serializers, status | ||
from vng_api_common.inspectors.view import COMMON_ERRORS | ||
from vng_api_common.serializers import FoutSerializer, ValidatieFoutSerializer | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
COMMON_ERROR_STATUSES = [e.status_code for e in COMMON_ERRORS] | ||
# error responses | ||
COMMON_ERROR_RESPONSES = {status: FoutSerializer for status in COMMON_ERROR_STATUSES} | ||
VALIDATION_ERROR_RESPONSES = {status.HTTP_400_BAD_REQUEST: ValidatieFoutSerializer} | ||
FILE_ERROR_RESPONSES = {status.HTTP_413_REQUEST_ENTITY_TOO_LARGE: FoutSerializer} | ||
PRECONDITION_ERROR_RESPONSES = {status.HTTP_412_PRECONDITION_FAILED: FoutSerializer} | ||
|
||
|
||
def get_component_from_serializer(serializer: serializers.Serializer) -> str: | ||
return serializer.Meta.model._meta.app_label | ||
|
||
|
||
def get_external_schema_ref(serializer: serializers.Serializer) -> str: | ||
""" | ||
Constructs the schema references for external resource | ||
""" | ||
component = get_component_from_serializer(serializer) | ||
oas_url = settings.EXTERNAL_API_MAPPING[component].oas_url | ||
resource_name = serializer.Meta.model._meta.object_name | ||
|
||
f = furl(oas_url) | ||
f.fragment.path = f"/components/schemas/{resource_name}" | ||
return f.url | ||
|
||
|
||
class AutoSchema(_AutoSchema): | ||
def get_content_type_headers(self) -> List[OpenApiParameter]: | ||
if self.method not in ["POST", "PUT", "PATCH"]: | ||
return [] | ||
|
||
return [ | ||
OpenApiParameter( | ||
name="Content-Type", | ||
type=str, | ||
location=OpenApiParameter.HEADER, | ||
description=_("Content type of the request body."), | ||
enum=["application/json"], | ||
required=True, | ||
) | ||
] | ||
|
||
def get_filter_backends(self): | ||
"""support expand for detail views""" | ||
include_allowed = getattr(self.view, "include_allowed", lambda: False)() | ||
if self.method == "GET" and include_allowed: | ||
return getattr(self.view, "filter_backends", []) | ||
|
||
return super().get_filter_backends() |