Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions core/common/swagger.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
"""Swagger helpers for OCL-specific OpenAPI generation."""

from copy import deepcopy

from django.conf import settings
from drf_yasg.generators import EndpointEnumerator, OpenAPISchemaGenerator

from core.fhir.base import FhirViewMixin, is_fhir_path
from core.fhir.swagger import FhirSwaggerSchemaGenerator, fhir_paths, fhir_swagger_url, on_fhir_host


class OCLSwaggerEndpointEnumerator(EndpointEnumerator):
"""Preserve literal dollar signs when deriving OpenAPI paths from Django URL regexes."""
Expand All @@ -18,3 +24,26 @@ class OCLSwaggerSchemaGenerator(OpenAPISchemaGenerator):
"""Use the OCL endpoint enumerator so Swagger paths match the published URLs."""

endpoint_enumerator_class = OCLSwaggerEndpointEnumerator

def get_endpoints(self, request):
"""Replace every FHIR view entry, including malformed paths from no-slash includes."""
return {path: endpoint for path, endpoint in super().get_endpoints(request).items()
if not is_fhir_path(path) and not issubclass(endpoint[0], FhirViewMixin)}

def get_paths(self, endpoints, components, request, public):
"""Keep the other API operations intact, adding FHIR only when both share a host."""
paths, prefix = super().get_paths(endpoints, components, request, public)
if not settings.FHIR_SUBDOMAIN:
paths.update(fhir_paths(components))
return paths, prefix

def get_schema(self, request=None, public=False):
"""Select the host's schema and link the API documentation to the separate FHIR service."""
if on_fhir_host(request):
return FhirSwaggerSchemaGenerator(self.info).get_schema(request, public)
schema = super().get_schema(request, public)
if settings.FHIR_SUBDOMAIN:
schema.info = deepcopy(schema.info)
link = fhir_swagger_url(request, self.url or settings.API_BASE_URL)
schema.info.description = schema.info.get('description', '') + f'\n\n[FHIR terminology Swagger]({link}).'
return schema
Loading
Loading