Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
edx-platform-release
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hsin-Yu Chien
edx-platform-release
Commits
28d2788a
Commit
28d2788a
authored
5 years ago
by
Ned Batchelder
Browse files
Options
Downloads
Patches
Plain Diff
OpenAPI schema generation helpers with drf-yasg
parent
71e5faf3
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
openedx/core/openapi.py
+67
-2
67 additions, 2 deletions
openedx/core/openapi.py
with
67 additions
and
2 deletions
openedx/core/openapi.py
+
67
−
2
View file @
28d2788a
...
...
@@ -2,9 +2,73 @@
Open API support.
"""
from
rest_framework
import
permissions
from
drf_yasg.views
import
get_schema_view
import
textwrap
from
drf_yasg
import
openapi
from
drf_yasg.generators
import
OpenAPISchemaGenerator
from
drf_yasg.utils
import
swagger_auto_schema
as
drf_swagger_auto_schema
from
drf_yasg.views
import
get_schema_view
from
rest_framework
import
permissions
# -- Code that will eventually be in another openapi-helpers repo -------------
class
ApiSchemaGenerator
(
OpenAPISchemaGenerator
):
"""
A schema generator for /api/*
Only includes endpoints in the /api/* url tree, and sets the path prefix
appropriately.
"""
def
get_endpoints
(
self
,
request
):
endpoints
=
super
(
ApiSchemaGenerator
,
self
).
get_endpoints
(
request
)
subpoints
=
{
p
:
v
for
p
,
v
in
endpoints
.
items
()
if
p
.
startswith
(
"
/api/
"
)}
return
subpoints
def
determine_path_prefix
(
self
,
paths
):
return
"
/api/
"
def
dedent
(
text
):
"""
Dedent multi-line text nicely.
An initial empty line is ignored so that triple-quoted strings don
'
t need
to start with a backslash.
"""
if
"
\n
"
in
text
:
first
,
rest
=
text
.
split
(
"
\n
"
,
1
)
if
not
first
.
strip
():
# First line is blank, discard it.
text
=
rest
return
textwrap
.
dedent
(
text
)
def
swagger_auto_schema
(
**
kwargs
):
"""
Decorator for documenting an OpenAPI endpoint.
Identical to `drf_yasg.utils.swagger_auto_schema`__ except that
description fields will be dedented properly. All description fields
should be in Markdown.
__ https://drf-yasg.readthedocs.io/en/stable/drf_yasg.html#drf_yasg.utils.swagger_auto_schema
"""
if
'
operation_description
'
in
kwargs
:
kwargs
[
'
operation_description
'
]
=
dedent
(
kwargs
[
'
operation_description
'
])
for
param
in
kwargs
.
get
(
'
manual_parameters
'
,
()):
param
.
description
=
dedent
(
param
.
description
)
return
drf_swagger_auto_schema
(
**
kwargs
)
def
is_schema_request
(
request
):
"""
Is this request serving an OpenAPI schema?
"""
return
request
.
query_params
.
get
(
'
format
'
)
==
'
openapi
'
# -----------------------------------------------------
openapi_info
=
openapi
.
Info
(
title
=
"
Open edX API
"
,
...
...
@@ -17,6 +81,7 @@ openapi_info = openapi.Info(
schema_view
=
get_schema_view
(
openapi_info
,
generator_class
=
ApiSchemaGenerator
,
public
=
True
,
permission_classes
=
(
permissions
.
AllowAny
,),
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment