Skip to content

Commit 332c3f7

Browse files
Merge pull request open5e#740 from open5e/586-reliable-openapi-spec-generated
586 reliable openapi spec generated
2 parents a1ca41e + 0cd8dcf commit 332c3f7

File tree

11 files changed

+486
-458
lines changed

11 files changed

+486
-458
lines changed

.github/workflows/rdme-openapi.yml

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,11 @@ jobs:
1919
with:
2020
python-version: "3.11"
2121

22-
- name: install pipenv
23-
run: |
24-
pip install pipenv
22+
- name: build schema
23+
run: sh ./scripts/generate_openapi_spec.sh
2524

26-
- name: install dependencies
27-
run: |
28-
pipenv install
2925

30-
- name: build oas file
31-
run: |
32-
pipenv run python manage.py spectacular --file openapi-schema.yml
33-
34-
- name: Run `openapi` command for v1🚀
26+
- name: Run `openapi` command 🚀
3527
uses: readmeio/rdme@v8
3628
with:
3729
rdme: openapi openapi-schema.yml --key=${{ secrets.README_API_KEY }} --id=6715b7fb7960ee004eb4a8cf

Pipfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ newrelic = "*"
1212
requests = "*"
1313
whitenoise = "*"
1414
gunicorn = "*"
15-
drf-spectacular = "*"
15+
drf-spectacular = {extras = ["sidecar"], version = "*"}
1616

1717
[dev-packages]
1818
pytest = "*"

Pipfile.lock

Lines changed: 442 additions & 440 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
from django.urls import path
44

55
from api import views
6+
from server.routers import DocumentedDefaultRouter
67

7-
router = routers.DefaultRouter()
8+
router = DocumentedDefaultRouter()
89
router.register(r'manifest', views.ManifestViewSet)
910
router.register(r'spells', views.SpellViewSet)
1011
router.register(r'spelllist',views.SpellListViewSet)

api_v2/models/characterclass.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class ClassFeature(HasName, HasDescription, FromDocument):
5252

5353
# Infer the type of this feature based on the `key`
5454
@property
55-
def feature_type(self):
55+
def feature_type(self) -> str:
5656
if "proficiency-bonus" in self.key: return "PROFICIENCY_BONUS"
5757
if "proficiencies" in self.key: return "PROFICIENCIES"
5858
if "equipment" in self.key: return "STARTING_EQUIPMENT"

api_v2/models/image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __str__(self):
3535
return (self.document.name + " - " + self.name)
3636

3737
@property
38-
def file_url(self):
38+
def file_url(self)->str:
3939
return static(self.file_path)
4040

4141
class Meta:

api_v2/serializers/creature.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class Meta:
7070
]
7171

7272
# Gathers 'uses_type' and 'uses_param' into a single 'usage_limits' obj.
73-
def get_usage_limits(self, obj):
73+
def get_usage_limits(self, obj)->dict:
7474
if obj.uses_type and obj.uses_param:
7575
return {
7676
'type': obj.uses_type,

scripts/generate_openapi_spec.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# This script takes the code repo as the input and generates an openapi spec as the output.
2+
# Parameters:
3+
# Version takes in a string that will be shown in the API docs.
4+
pip install pipenv
5+
pipenv install
6+
pipenv run python manage.py spectacular --color --file openapi-schema.yml --validate --fail-on-warn

server/routers.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from rest_framework import routers
2+
3+
class Open5eAPI(routers.APIRootView):
4+
"""
5+
Welcome to the Open5e API.
6+
You can review the basic DRF Browseable API here.
7+
You can review swagger-ui at /schema/swagger-ui/
8+
You can review redoc at /schema/redoc/
9+
"""
10+
11+
class DocumentedDefaultRouter(routers.DefaultRouter):
12+
APIRootView = Open5eAPI

server/settings.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
'api_v2',
6969
'search',
7070
'drf_spectacular',
71+
'drf_spectacular_sidecar',
7172

7273
# downloaded modules
7374
"rest_framework",
@@ -216,13 +217,20 @@
216217
) # This setting allows the header from NGINX to tell us that the request is secured.
217218

218219
SPECTACULAR_SETTINGS = {
220+
'VERSION' : 'development',
219221
'TITLE': 'Open5e',
220-
'DESCRIPTION': 'The Open5e API includes all monsters and spells from the SRD and other',
221-
'SERVERS': [{'url': 'https://api.open5e.com', 'description': 'Production server'}],
222+
'DESCRIPTION': 'The Open5e API. See [https://github.com/open5e/open5e-api] for more information.',
223+
'SERVERS': [
224+
{'url': 'https://api.open5e.com', 'description': 'Production server'},
225+
{'url': 'https://api-beta.open5e.com', 'description': 'Beta server'},
226+
{'url': 'http://localhost:8000', 'description': 'Localhost'}],
222227
'PREPROCESSING_HOOKS': [
223228
'server.oas.custom_preprocessing_hook'
224229
],
225230
'POSTPROCESSING_HOOKS': [
226231
'server.oas.custom_postprocessing_hook'
227232
],
233+
'SWAGGER_UI_DIST': 'SIDECAR',
234+
'SWAGGER_UI_FAVICON_HREF': 'SIDECAR',
235+
'REDOC_DIST': 'SIDECAR',
228236
}

0 commit comments

Comments
 (0)