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

default values - RangeError: Maximum call stack size exceeded #802

Closed
fogine opened this issue Jun 8, 2018 · 4 comments
Closed

default values - RangeError: Maximum call stack size exceeded #802

fogine opened this issue Jun 8, 2018 · 4 comments

Comments

@fogine
Copy link

fogine commented Jun 8, 2018

What version of Ajv are you using? Does the issue happen if you use the latest version?

6.5.0

Ajv options object

{useDefaults: false}

JSON Schema

function Default() {}
Default.prototype.toJSON = function toJSON() {
    return {};
};

const def = new Default;

def.prop = {
    Default: def // it fails because of this circular enumerable property
};

const schema = {
    type: 'object',
    properties: {
        prop: {
            type: 'object',
            default: def
        }
    }
};

Your code

ajv.addSchema(schema, 'test');

Validation result, data AFTER validation, error messages

RangeError: Maximum call stack size exceeded
    at _traverse (/path/to/project/node_modules/json-schema-traverse/index.js:58:5)
    at _traverse (/path/to/project/node_modules/json-schema-traverse/index.js:72:9)
    at _traverse (/path/to/project/node_modules/json-schema-traverse/index.js:72:9)
    at _traverse (/path/to/project/node_modules/json-schema-traverse/index.js:72:9)
    at _traverse (/path/to/project/node_modules/json-schema-traverse/index.js:72:9)
    at _traverse (/path/to/project/node_modules/json-schema-traverse/index.js:72:9)
    at _traverse (/path/to/project/node_modules/json-schema-traverse/index.js:72:9)
    at _traverse (/path/to/project/node_modules/json-schema-traverse/index.js:72:9)
    at _traverse (/path/to/project/node_modules/json-schema-traverse/index.js:72:9)
    at _traverse (/path/to/project/node_modules/json-schema-traverse/index.js:72:9)


What results did you expect?
When useDefaults=false I'd expect that ajv is gonna ignore default keyword and not traverse its enumerable properties.
Although it should not fail even with useDefaults=true when the default value object implements toJSON method.

Are you going to resolve the issue?
possible bugfix provided here:
epoberezkin/json-schema-traverse#5

In ajv library, used here:
https://github.com/epoberezkin/ajv/blob/master/lib/compile/resolve.js#L239

@fogine
Copy link
Author

fogine commented Jun 8, 2018

note: I see now that default property value is serialized because ajv option serialize is not set to false...

@epoberezkin
Copy link
Member

epoberezkin commented Jun 9, 2018

@fogine ajv indeed doesn't support schemas with recursive data structures - they need to be serialisable (unless you disable it, as you wrote, but they would still be used as keys - so unless you use WeakMap as a cache, nothing will work). I will release json-schema-traverse.

@neopaf
Copy link

neopaf commented Sep 8, 2022

@epoberezkin
Evgeny,

ajv indeed doesn't support schemas with recursive data structures

Is it theoretically possible to change that? Пожалуйста?

@epoberezkin
Copy link
Member

epoberezkin commented Sep 8, 2022

Only theoretically, I am afraid... The cost would be prohibitive - it's a major design change.

Effectively, what you are asking is to detect cycles and translate them to $refs - this is a more complex operation than avoiding cycles in the first place...

It does seem though like some separate library could possibly do ref-ing, in the same way there is a library that does de-ref-ing, which means it doesn't need to be done as part of Ajv - it can be defined and solved as a relatively isolated CS problem on the level of tech interview questions...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants