mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-11-21 16:11:52 +01:00
Merge pull request #681 from eitchtee/fix/api-docs-warnings
fix(api): clear the API docs schema warnings
This commit is contained in:
5 files changed
+41
-1
No files matched your search
@@ -517,6 +517,11 @@ SPECTACULAR_SETTINGS = {
|
||||
"DESCRIPTION": "A no-frills expense tracker",
|
||||
"VERSION": "1.0.0",
|
||||
"SERVE_INCLUDE_SCHEMA": False,
|
||||
# Several models have a "type" field with different choices
|
||||
"ENUM_NAME_OVERRIDES": {
|
||||
"TransactionTypeEnum": "apps.transactions.models.Transaction.Type",
|
||||
"TemplateTypeEnum": "apps.transactions.models.QuickTransaction.Type",
|
||||
},
|
||||
# OTHER SETTINGS
|
||||
}
|
||||
|
||||
|
||||
@@ -4,3 +4,6 @@ from django.apps import AppConfig
|
||||
class ApiConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "apps.api"
|
||||
|
||||
def ready(self):
|
||||
import apps.api.schema # noqa: F401 registers the API token scheme for the docs
|
||||
@@ -10,7 +10,8 @@ from apps.transactions.models import (
|
||||
|
||||
@extend_schema_field(
|
||||
{
|
||||
"oneOf": [{"type": "string"}, {"type": "integer"}, {"type": "null"}],
|
||||
"oneOf": [{"type": "string"}, {"type": "integer"}],
|
||||
"nullable": True,
|
||||
"description": "TransactionCategory ID or name. If the name doesn't exist, a new one will be created. Can be null if no category is assigned.",
|
||||
}
|
||||
)
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
from drf_spectacular.extensions import OpenApiAuthenticationExtension
|
||||
|
||||
from apps.users.models import APIToken
|
||||
|
||||
|
||||
class APITokenAuthenticationScheme(OpenApiAuthenticationExtension):
|
||||
"""Describes APITokenAuthentication in the API docs."""
|
||||
|
||||
target_class = "apps.api.authentication.APITokenAuthentication"
|
||||
name = "apiToken"
|
||||
|
||||
def get_security_definition(self, auto_schema):
|
||||
return {
|
||||
"type": "apiKey",
|
||||
"in": "header",
|
||||
"name": "Authorization",
|
||||
"description": (
|
||||
f"A personal API token, sent as `Token {APIToken.TOKEN_PREFIX}…`."
|
||||
),
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import tempfile
|
||||
|
||||
from django.core.management import call_command
|
||||
from django.test import SimpleTestCase
|
||||
|
||||
|
||||
class SchemaTests(SimpleTestCase):
|
||||
def test_schema_is_valid_without_warnings(self):
|
||||
"""Keeps /api/docs/ complete: drf-spectacular warns when it can't describe something."""
|
||||
with tempfile.NamedTemporaryFile(suffix=".yml") as schema:
|
||||
call_command("spectacular", "--validate", "--fail-on-warn", "--file", schema.name)
|
||||
Reference in new issue
Block a user