File: providers.py

package info (click to toggle)
python-openapi-core 0.22.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,104 kB
  • sloc: python: 19,979; makefile: 44
file content (31 lines) | stat: -rw-r--r-- 976 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""OpenAPI core contrib django providers module"""

import warnings
from typing import cast

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

from openapi_core import OpenAPI


def get_default_openapi_instance() -> OpenAPI:
    """
    Retrieves or initializes the OpenAPI instance based on Django settings
    (either OPENAPI or OPENAPI_SPEC).
    This function ensures the spec is only loaded once.
    """
    if hasattr(settings, "OPENAPI"):
        # Recommended (newer) approach
        return cast(OpenAPI, settings.OPENAPI)
    elif hasattr(settings, "OPENAPI_SPEC"):
        # Backward compatibility
        warnings.warn(
            "OPENAPI_SPEC is deprecated. Use OPENAPI in your settings instead.",
            DeprecationWarning,
        )
        return OpenAPI(settings.OPENAPI_SPEC)
    else:
        raise ImproperlyConfigured(
            "Neither OPENAPI nor OPENAPI_SPEC is defined in Django settings."
        )