Skip to content

Commit

Permalink
Merge pull request #487 from hinashi/feature/auto_generate/custom_view
Browse files Browse the repository at this point in the history
Added DRF Spectacular settings for customview
  • Loading branch information
hinashi authored May 6, 2022
2 parents 3cba997 + 0e5e10e commit f2ad7a1
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 622 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ If you have any change on API V2, you need to run this command before you build:
```
$ npm run generate:client
```
```
(For Customview)
$ npm run generate:custom_client
```

You can also auto-format .js files with [prettier](https://prettier.io/):

Expand Down
6 changes: 6 additions & 0 deletions airone/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from copy import deepcopy
from airone.settings_common import Common


Expand All @@ -7,3 +8,8 @@ class Dev(Common):

class Prd(Common):
pass


class DRFSpectacularCustomView(Common):
SPECTACULAR_SETTINGS = deepcopy(Common.SPECTACULAR_SETTINGS)
SPECTACULAR_SETTINGS['PREPROCESSING_HOOKS'].remove('airone.spectacular.exclude_customview_hook')
7 changes: 7 additions & 0 deletions airone/settings_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,10 @@ class Common(Configuration):
],
"PAGE_SIZE": 30,
}

SPECTACULAR_SETTINGS = {
"PREPROCESSING_HOOKS": [
"airone.spectacular.exclude_customview_hook",
"airone.spectacular.filter_apiv2_hook",
]
}
24 changes: 24 additions & 0 deletions airone/spectacular.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# These methods are used for filtering API endpoints to generate
# OpenAPI schema using drf-spectacular.
# (c.f. https://drf-spectacular.readthedocs.io/en/latest/settings.html)

def exclude_customview_hook(endpoints):
"""This excludes API endpoints of custom-view's ones. Because it's not always
necessary to generage custom-view's OpenAPI schema.
"""
result = []
for (path, path_regex, method, callback) in endpoints:
if "/custom/" not in path:
result.append((path, path_regex, method, callback))
return result


def filter_apiv2_hook(endpoints):
"""This excludes API endpoints for AirOne API(v1). React views refer to only
AirOne API(v2). So it's not necessary to generate API(v1)'s OpenAPI schema.
"""
result = []
for (path, path_regex, method, callback) in endpoints:
if "/api/v2/" in path:
result.append((path, path_regex, method, callback))
return result
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.openapi-generator-ignore
apis/AclApi.ts
apis/ApiApi.ts
apis/EntityApi.ts
apis/EntryApi.ts
apis/GroupApi.ts
Expand Down
Loading

0 comments on commit f2ad7a1

Please sign in to comment.