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.

Polymorphic schema usage

See original GitHub issue

Hi, I have a question regarding usage of polymorphic schema objects.

Say, I have a base Config class object, with only id, name and type fields:

class Config(Schema):
    id = base_fields.Integer(description='Configuration ID', required=True)
    name = base_fields.String(description='Configuration name', required=True)
    type = base_fields.Integer(description='Configuration type', required=True)

Now, assuming that there will be many schema types that derives from Config but each with specific field(s), like:

class ConfigFile(Schema):
    path = base_fields.String(description='Config file path', required=True)

class ConfigDB(Schema):
    user = base_fields.String(description='DB username', required=True)
    password = base_fields.String(description='DB password', required=True)

When e.g. I pull data from the DB, I will get list of SQLAlchemy objects that are polymorphic, and type is a discriminator:

__mapper_args__ = {'polymorphic_identity': 'ConfigFile'} 
...
__mapper_args__ = {'polymorphic_identity': 'ConfigDB'} 

Is it possible to use ConfigFile schema when for example, creating a response which can hold list of objects that are sub-classes of it like:

class ConfigResponse(Schema):
    response = base_fields.Nested(ConfigFile, description='', many=True, required=True)

Is usage of marshmallow-oneofschema safe or some custom fix is recommended? Additional reason to ask is because we also use flasgger and I am not sure how it will ‘dance’ with it.

One additional question, not related to this topic: what are performance penalties comparing to usage of custom serialize method/class based on plain dictionaries? I like the library, but one of my coworkers keeps undermine usage of it based on suspicions regarding speed and not only that but also of ease of usage. I think second can not be further from the truth, but for speed I am not sure how much is the overhead. Are there some figures already, or to make my own test?

Thank you in advance

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
deckar01commented, Oct 9, 2018

https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/

Apispec uses a “discriminator” to solve the problem of determining which schema an object belongs to. It’s not a perfect solution, but I think it is something that marshmallow/apispec could implement.

0reactions
nikoladspcommented, Oct 13, 2018

Thank you all!

Read more comments on GitHub >

github_iconTop Results From Across the Web

2. Polymorphic Schemas - MongoDB Applied Design Patterns ...
In a well-designed application, however, it is more frequently the case that a collection will contain documents of identical, or closely related, structure....
Read more >
Building with Patterns: The Polymorphic Pattern - MongoDB
The Polymorphic Pattern is used when documents have more similarities than they have differences. Typical use cases for this type of schema design...
Read more >
Inheritance and Polymorphism - Swagger
OAS 3 This guide is for OpenAPI 3.0. Inheritance and Polymorphism. Model Composition. In your API, you may have model schemas that share...
Read more >
What is polymorphism? - Hackolade
This is known as 'schema combination', and can be represented in JSON Schema with the use of subschemas and the keywords: anyOf, allOf,...
Read more >
Polymorphic relationships in Laravel and their use cases
A one-to-one polymorphic relationship is a situation where one model can belong to more than one type of model but on only one...
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