Skip to content

Commit 0d5a3a0

Browse files
scardinecarltongibson
authored andcommitted
Add schema to ObtainAuthToken
Add encoding parameter to ManualSchema Closes #5676 * Fixed lint errors * Added docs for ManualSchema encoding parameter
1 parent 878fe89 commit 0d5a3a0

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

docs/api-guide/schemas.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,8 @@ The `ManualSchema` constructor takes two arguments:
669669

670670
**`description`**: A string description. Optional.
671671

672+
**`encoding`**: Default `None`. A string encoding, e.g `application/json`. Optional.
673+
672674
---
673675

674676
## Core API

rest_framework/authtoken/views.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
from rest_framework.authtoken.serializers import AuthTokenSerializer
44
from rest_framework.response import Response
55
from rest_framework.views import APIView
6+
from rest_framework.schemas import ManualSchema
7+
import coreapi
8+
import coreschema
69

710

811
class ObtainAuthToken(APIView):
@@ -11,6 +14,29 @@ class ObtainAuthToken(APIView):
1114
parser_classes = (parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser,)
1215
renderer_classes = (renderers.JSONRenderer,)
1316
serializer_class = AuthTokenSerializer
17+
schema = ManualSchema(
18+
fields=[
19+
coreapi.Field(
20+
name="username",
21+
required=True,
22+
location='form',
23+
schema=coreschema.String(
24+
title="Username",
25+
description="Valid username for authentication",
26+
),
27+
),
28+
coreapi.Field(
29+
name="password",
30+
required=True,
31+
location='form',
32+
schema=coreschema.String(
33+
title="Password",
34+
description="Valid password for authentication",
35+
),
36+
),
37+
],
38+
encoding="application/json",
39+
)
1440

1541
def post(self, request, *args, **kwargs):
1642
serializer = self.serializer_class(data=request.data,

rest_framework/schemas/inspectors.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ class ManualSchema(ViewInspector):
445445
Allows providing a list of coreapi.Fields,
446446
plus an optional description.
447447
"""
448-
def __init__(self, fields, description=''):
448+
def __init__(self, fields, description='', encoding=None):
449449
"""
450450
Parameters:
451451
@@ -455,6 +455,7 @@ def __init__(self, fields, description=''):
455455
assert all(isinstance(f, coreapi.Field) for f in fields), "`fields` must be a list of coreapi.Field instances"
456456
self._fields = fields
457457
self._description = description
458+
self._encoding = encoding
458459

459460
def get_link(self, path, method, base_url):
460461

@@ -464,7 +465,7 @@ def get_link(self, path, method, base_url):
464465
return coreapi.Link(
465466
url=urlparse.urljoin(base_url, path),
466467
action=method.lower(),
467-
encoding=None,
468+
encoding=self._encoding,
468469
fields=self._fields,
469470
description=self._description
470471
)

0 commit comments

Comments
 (0)