Skip to content

Commit e420155

Browse files
committed
Add APIFAIRY_APISPEC_VERSION config option
1 parent f2583d0 commit e420155

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

docs/intro.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Name Type Default Description
9191
``APIFAIRY_TITLE`` String No title The API's title.
9292
``APIFAIRY_VERSION`` String No version The API's version.
9393
``APIFAIRY_APISPEC_PATH`` String */apispec.json* The URL path where the JSON OpenAPI specification for this project is served.
94+
``APIFAIRY_APISPEC_VERSION`` String ``None`` The version of the OpenAPI specification to generate for this project.
9495
``APIFAIRY_APISPEC_DECORATORS`` List [] A list of decorators to apply to the JSON OpenAPI endpoint.
9596
``APIFAIRY_UI`` String redoc The documentation format to use. Supported formats are "redoc", "swagger_ui", "rapidoc" and "elements".
9697
``APIFAIRY_UI_PATH`` String */docs* The URL path where the documentation is served.

src/apifairy/core.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def init_app(self, app):
4646
self.version = app.config.get('APIFAIRY_VERSION', 'No version')
4747
self.apispec_path = app.config.get('APIFAIRY_APISPEC_PATH',
4848
'/apispec.json')
49+
self.apispec_version = app.config.get('APIFAIRY_APISPEC_VERSION', None)
4950
self.apispec_decorators = app.config.get(
5051
'APIFAIRY_APISPEC_DECORATORS', [])
5152
self.ui = app.config.get('APIFAIRY_UI', 'redoc')
@@ -147,10 +148,11 @@ def resolver(schema):
147148
tags[name] = tag
148149
tag_list = [tags[name] for name in tag_names]
149150
ma_plugin = MarshmallowPlugin(schema_name_resolver=resolver)
151+
oas_version = '3.1.0' if _webhooks else self.apispec_version or '3.0.3'
150152
spec = APISpec(
151153
title=self.title,
152154
version=self.version,
153-
openapi_version='3.1.0' if _webhooks else '3.0.3',
155+
openapi_version=oas_version,
154156
plugins=[ma_plugin],
155157
info=info,
156158
servers=servers,

tests/test_apifairy.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,25 @@ class Schema(ma.Schema):
2323
class Meta:
2424
unknown = EXCLUDE
2525

26-
id = ma.Integer(default=123)
26+
id = ma.Integer(dump_default=123)
2727
name = ma.Str(required=True)
2828

2929

3030
class Schema2(ma.Schema):
3131
class Meta:
3232
unknown = EXCLUDE
3333

34-
id2 = ma.Integer(default=123)
34+
id2 = ma.Integer(load_default=123, dump_default=123)
3535
name2 = ma.Str(required=True)
3636

3737

3838
class FooSchema(ma.Schema):
39-
id = ma.Integer(default=123)
39+
id = ma.Integer(load_default=123, dump_default=123)
4040
name = ma.Str()
4141

4242

4343
class QuerySchema(ma.Schema):
44-
id = ma.Integer(missing=1)
44+
id = ma.Integer(load_default=1)
4545

4646

4747
class FormSchema(ma.Schema):
@@ -140,6 +140,26 @@ def test_no_apispec_path(self):
140140
rv = client.get('/apispec.json')
141141
assert rv.status_code == 404
142142

143+
def test_custom_apispec_version(self):
144+
app, _ = self.create_app(config={'APIFAIRY_APISPEC_VERSION': '3.1.0'})
145+
146+
client = app.test_client()
147+
rv = client.get('/apispec.json')
148+
assert rv.status_code == 200
149+
assert set(rv.json.keys()) == {
150+
'openapi', 'info', 'servers', 'paths', 'tags'}
151+
assert rv.json['openapi'] == '3.1.0'
152+
153+
def test_custom_apispec_no_version(self):
154+
app, _ = self.create_app(config={'APIFAIRY_APISPEC_VERSION': None})
155+
156+
client = app.test_client()
157+
rv = client.get('/apispec.json')
158+
assert rv.status_code == 200
159+
assert set(rv.json.keys()) == {
160+
'openapi', 'info', 'servers', 'paths', 'tags'}
161+
assert rv.json['openapi'] == '3.0.3'
162+
143163
def test_ui(self):
144164
app, _ = self.create_app(config={'APIFAIRY_UI': 'swagger_ui'})
145165

0 commit comments

Comments
 (0)