question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use schema instance instead of class definitions when documenting API

See original GitHub issue

Hi, one question regarding using schema instances (objects) instead of class definitions when documenting API. Lets say we have a simple situation:

from marshmallow import Schema, fields


class UserSchema(Schema):
    id = fields.Integer(required=True)
    name = fields.String()
    email = fields.String()

create_schema = UserSchema(exclude=['id'])
read_schema = UserSchema(exclude=[])
update_schema = UserSchema(exclude=['name'])
delete_schema = UserSchema(exclude=[])

user1 = {
    'id': 1,
    'name': 'user1',
    'email': 'user1@gmail.com',
}

print create_schema.dump(user1)
print read_schema.dump(user1)
print update_schema.dump(user1)
print delete_schema.dump(user1)

Output looks like:

MarshalResult(data={u'name': u'user1', u'email': u'user1@gmail.com'}, errors={})
MarshalResult(data={u'email': u'user1@gmail.com', u'name': u'user1', u'id': 1}, errors={})
MarshalResult(data={u'id': 1, u'email': u'user1@gmail.com'}, errors={})
MarshalResult(data={u'email': u'user1@gmail.com', u'name': u'user1', u'id': 1}, errors={})

So we have one schema (User) and 4 instances of it; say for CRUD methods. So when we want to do read user(s), all fields are expected to be displayed (exclude list is empty). Similar is for delete. However, when for example we want to create a user object, we should not see nor pass the id field. Now if we use UserSchema to describe POST endpoint, we would see all fields from it, but in reality we should not see the id field. This can be solved by using separate schema classes, for each CRUD method.

So the question is as follows: shall we use separate schema class definition, depending on CRUD method in question or we maybe use something like LazyString to use concrete instance which will be used by the CRUD method itself to serialize/dump data? For more complex, especially nested class, we might have way too many classes to define. On the other hand, I am not sure if using instance serializer(s) instead of schemes is the way to go. Any thoughts?

Also, a side question: are anyOf and oneOf supported?

Thanks in advance

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
nikoladspcommented, Nov 8, 2018

I think we should maybe update swagger library to newer version. I will try this soon and report back here.

Cheers

1reaction
wuyusha93commented, Jun 11, 2020

Seems it’s been a while, any updates on this issue? In my project flasgger still not supports oneOf/anyOf/allOf

Read more comments on GitHub >

github_iconTop Results From Across the Web

Generating New Schema Classes - Pixar Graphics
usdGenSchema, our tool for generating C++ classes from a schema.usda file requires the jinja2 template substitution module and argparse modules be installed ...
Read more >
Schemas - Django REST framework
API schemas are a useful tool that allow for a range of use cases, including generating reference documentation, or driving dynamic client libraries...
Read more >
Schema Registry API Reference
The Schema Registry server can enforce certain compatibility rules when new schemas are registered in a subject. These are the compatibility types: BACKWARD...
Read more >
OpenAPI Specification - Version 2.0
Schema. Swagger Object. This is the root document object for the API specification. It combines what previously was the Resource Listing and API...
Read more >
API Reference: ApolloServer - Apollo GraphQL Docs
You can use the ApolloServer class to create an instance of Apollo Server that ... A valid Schema Definition Language (SDL) string, document,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found