Possible to use marshmallow_jsonapi
- Dominant language
- Python
- Stars
- 652
- Forks
- 151
- PR merge metrics
- No merged PRs in 30d
Description
Hi,
For my REST api I make use of the jsonapi specification and therefore I use the marshmallow_jsonapi module with Flask.
But I also would like to have auto-generated documentation and came across this flask-apispec module.
Is it possible to generate documentation with Schemas from marshmallow_jsonapi and display it in the swagger-ui or is only the OpenApi 2 specification allowed?
The code I have so far is:
``` Python
from datetime import date
from pprint import pprint
from marshmallow import ValidationError
from marshmallow_jsonapi import fields, Schema
from flask import Flask, jsonify, views
from flask_apispec import ResourceMeta, doc, marshal_with
from flask_apispec import FlaskApiSpec
class UserModel:
def __init__(self, id, name, age, email, bsn, date_of_birth):
self.id = id
self.name = name
self.age = age
self.email = email
self.bsn = bsn
self.date_of_birth = date_of_birth
class UserSchema(Schema):
id = fields.Str(dump_only=True)
# Added extra fields name options, those will also be used in the doc
name = fields.Str(missing='firstname lastname', default=' ', example='Frank Best')
age = fields.Int()
email = fields.Email()
bsn = fields.Int()
date_of_birth = fields.Date()
class Meta:
strict = True
type_ = "user"
def J(*args, **kwargs):
"""Wrapper around jsonify that sets the Content-Type of the response to
application/vnd.api+json.
"""
# print(args)
# print(kwargs)
response = jsonify(*args, **kwargs)
response.mimetype = "application/vnd.api+json"
return response
app = Flask(__name__)
docs = FlaskApiSpec(app)
class MethodResourceMeta(ResourceMeta, views.MethodViewType):
pass
class MethodResource(views.MethodView, metaclass=MethodResourceMeta):
methods = None
@doc(
tags=['info']
)
class UserResource(MethodResource):
# marshal_with(UserSchema) is necessary for the FlaskapiSpec docs
@marshal_with(UserSchema)
def get(self):
try:
user = UserModel(id='2345', name="Pietje Bell", age=35, email="pietje@gmail.com", bsn=123456789, date_of_birth=date(1980, 1, 1))
# Serialize objects by passing them to your schema’s dump method, which returns the formatted result.
schema = UserSchema()
data = schema.dump(user)
pprint(data)
except ValidationError as err:
return J(err.messages), 422
return J(data)
app.add_url_rule('/user', view_func=UserResource.as_view('UserResource'))
docs.register(UserResource, endpoint='UserResource')
if __name__ == '__main__':
app.run(debug=True)
```
But this gives me not the documentation I would expect for the jsonapi:

Thank you in advance for your help,
Best regards,
Gerard
Contributor guide
No contributing guide indexed for this repository
Research direction
Start from the FlaskApiSpec registration and marshal_with(UserSchema) example in the issue, then inspect how marshmallow_jsonapi schemas are represented in generated Swagger/OpenAPI documentation. Define the expected JSON:API representation and verify it in Swagger UI; done when a documented example or focused test demonstrates compatible schema generation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- flask, python
- Domain
- api, documentation
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100