Skip to content

Self-referential nested fields not parsed correctly #55

@whoiswes

Description

@whoiswes

Apologies if I'm missing something obvious here as I'm still figuring out Marshmallow and APISpec.

When we have a Model with a self-referential relationship, apispec is not able to parse the child relationships successfully. Here's an example (SQLAlchemy, Marshmallow-SQLAlchemy, APISpec, and Flask)

Models:

Parent_Child = Table('Parent_Child', Base.metadata,
    Column('parent_id', Integer, ForeignKey('Entity.id')),
    Column('child_id', Integer, ForeignKey('Entity.id'))
    )

class Entity(Base):
    __tablename__ = 'Entity'
    id = Column(Integer, primary_key=True)
    name = Column(String(80))
    children = relationship('Entity', secondary='Parent_Child', primaryjoin=(id == Parent_Child.c.parent_id), secondaryjoin=(id == Parent_Child.c.child_id), lazy='joined', backref='parent')

Schema:

class EntitySchema(ModelSchema):
    children = fields.Nested('self', many=True)
    class Meta:
        model = Entity
        sqla_session = session
        include_fk = True

EntitySerializer = EntitySchema()

APISpec to_dict output (note the Entity.children object reference)

{
    'parameters': {},
    'paths': {
        '/entities': {}
    },
    'swagger': '2.0',
    'info': {
        'version': '1.0.0',
        'title': 'M2M Nested Example'
    },
    'description': 'M2M Nested Example',
    'definitions': {
        'Entity': {
            'properties': {
                'children': .at 0x00000000048BF7B8 > ,
                'id': {
                    'format': 'int32',
                    'type': 'integer'
                },
                'parent': {
                    'type': 'array',
                    'items': {
                        'type': 'string'
                    }
                },
                'name': {
                    'type': 'string'
                }
            }
        }
    }
}

Again, apologies if I've missed something obvious or this is a known issue. I can provide a complete sample app via gist if that would be helpful.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions