Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

circular dependency triggers stackoverflow #41

Closed
itajaja opened this issue Dec 19, 2015 · 4 comments

Comments

@itajaja
Copy link
Contributor

commented Dec 19, 2015

The following schemas:

from marshmallow import fields

class SampleSchema(Schema):
    runs = fields.Nested('RunSchema', many=True, exclude=('sample',))

class RunSchema(Schema):
    sample = fields.Nested(SampleSchema, exclude=('runs',), required=True)

Causes an infinite recursion when trying to resolve the two schemas.

I am trying to debug the problem, but I am lost right now. Is someone able to point me to the right direction?

@sloria

This comment has been minimized.

Copy link
Member

commented Dec 19, 2015

I can't seem to reproduce the error.

        spec = APISpec(
            title='Test',
            version='1.0.0',
            plugins=[
                'apispec.ext.marshmallow',
            ],
        )

        class SampleSchema(Schema):
            runs = fields.Nested('RunSchema', many=True, exclude=('sample',))

        class RunSchema(Schema):
            sample = fields.Nested(SampleSchema, exclude=('runs',), required=True)

        spec.definition('Sample', schema=SampleSchema)
        spec.definition('Run', schema=RunSchema)
        spec.to_dict()

I do not get an error with the above code.

@itajaja Could you please provides steps to reproduce the bug?

@itajaja

This comment has been minimized.

Copy link
Contributor Author

commented Dec 20, 2015

The codebase the error occured is much bigger, I'll try to strip down a minimum reproducible example

@itajaja

This comment has been minimized.

Copy link
Contributor Author

commented Dec 20, 2015

Hey @sloria here is a reproducible example:

from apispec import APISpec
from marshmallow import fields, Schema

spec = APISpec(
            title='Test',
            version='1.0.0',
            plugins=('apispec.ext.marshmallow',),
        )


class SampleSchema(Schema):
    runs = fields.Nested('RunSchema', many=True, exclude=('sample',))

class RunSchema(Schema):
    sample = fields.Nested(SampleSchema, exclude=('runs',))


class AnalysisSchema(Schema):
    sample = fields.Nested(SampleSchema)


spec.definition('analysis', schema=AnalysisSchema)
spec.definition('sample', schema=SampleSchema)
spec.definition('run', schema=RunSchema)

print spec.to_dict()

now, reorganizing the definition calls like this:

spec.definition('sample', schema=SampleSchema)
spec.definition('run', schema=RunSchema)
spec.definition('analysis', schema=AnalysisSchema)

will actually work. The reason I cannot organize my calls, is because I am trying to build an automated tool that generates the schema definitions programmatically by inspecting all the Schema classes of a module.

now that I built this example though, even the PR I opened doesn't seem to solve the problem

@itajaja

This comment has been minimized.

Copy link
Contributor Author

commented Dec 21, 2015

@sloria I created a new PR. the only solution I found to not break the API is to delay the execution of field2property until all the schemas are declared

@sloria sloria closed this in #44 Dec 31, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.