Skip to content

Commit 66f1617

Browse files
committed
Custom Marshmallow APISpec plugin to handle Bytes field
1 parent b516110 commit 66f1617

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from apispec.ext.marshmallow import (
2+
MarshmallowPlugin as _MarshmallowPlugin,
3+
OpenAPIConverter,
4+
)
5+
from labthings.server.fields import Bytes as BytesField
6+
7+
8+
class ExtendedOpenAPIConverter(OpenAPIConverter):
9+
field_mapping = OpenAPIConverter.field_mapping
10+
field_mapping.update({BytesField: ("string", None)})
11+
12+
def init_attribute_functions(self, *args, **kwargs):
13+
OpenAPIConverter.init_attribute_functions(self, *args, **kwargs)
14+
self.attribute_functions.append(self.bytes2json)
15+
16+
def bytes2json(self, field, **kwargs):
17+
ret = {}
18+
if isinstance(field, BytesField):
19+
ret.update({"contentEncoding": "base64"})
20+
return ret
21+
22+
23+
class MarshmallowPlugin(_MarshmallowPlugin):
24+
Converter = ExtendedOpenAPIConverter

0 commit comments

Comments
 (0)