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

Validating a deeply nested schema using custom "strict" draft 2020 metaschema can be very slow #1097

Closed
cweiske opened this issue May 8, 2023 · 5 comments
Labels
Needs Simplification An issue which is in need of simplifying the example or issue being demonstrated for diagnosis.

Comments

@cweiske
Copy link

cweiske commented May 8, 2023

I am using check-jsonschema 0.23.0 with jsonschema 4.17.3 to validate a JSON schema against a strict json schema schema.

When running check-jsonschema, it runs for at least a minute until I kill it. Looks like an infinite loop somewhere.
Validating against the "normal" json schema schema works.

$ check-jsonschema --schemafile ~/dev/tools/json-schema-schema/json-schema.schema.json api-rest-connect-stick-stick-xxx.schema.json 
ok -- validation done

$ check-jsonschema --schemafile ~/dev/tools/json-schema-schema/strict.json api-rest-connect-stick-stick-xxx.schema.json 
(ctrl-c pressed after 1 minute)

The schema I am validating against is this: https://github.com/orgs/json-schema-org/discussions/380#discussioncomment-5711007
The schema file I am writing (and which was generated by genson) is attached here: api-rest-connect-stick-stick-xxx.schema.json.gz

@Julian
Copy link
Member

Julian commented May 8, 2023

Hey! Did you mean to file this on the check-jsonschema repo? If not, can you minimize to something that doesn't go via the CLI?

@cweiske
Copy link
Author

cweiske commented May 8, 2023

I thought that check-jsonschema uses jsonschema internally, and that this problem is most likely a bug in jsonschema.
But since you insist I'll close this bug and create on in check-jsonschema.

@Julian
Copy link
Member

Julian commented May 8, 2023

I didn't insist -- or tell you to move it, I said if you think it's a likely jsonschema bug, please minimize your example :)

@sirosen
Copy link
Member

sirosen commented May 8, 2023

Can/should we reopen this? I don't really know what to do with the CLI ticket until/unless we determine that it is not a jsonschema issue, so I'm thinking just keep both open for a minute.

I've pulled down the schema and instance the user provided and validated that this does at least run for a long time for me -- 6 minutes -- but that it terminates and in fact succeeds.

Just using the CLI -- I don't have time to try harder to give a python-only repro this minute -- I was able to reproduce a slow validation pass by trimming the user's schema down to the following document:

{
  "type": "object",
  "properties": {
    "body": {
      "type": "object",
      "properties": {
        "config": {
          "type": "object",
          "properties": {
            "global": {
              "type": "object",
              "properties": {
                "uitranslation": {
                  "type": "object",
                  "properties": {
                    "country": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "language": {
                            "type": "array",
                            "items": {
                              "type": "object",
                              "properties": {
                                "entries": {
                                  "type": "array",
                                  "items": {
                                    "type": "object",
                                    "properties": {
                                      "text": {
                                        "type": "string"
                                      }
                                    }
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

and the metaschema from that thread was this:

{
  "$id": "https://example.com/draft/2020-12/schema/strict",
  "$schema": "https://json-schema.org/draft/2020-12/schema",

  "$vocabulary": {
    "https://json-schema.org/draft/2020-12/vocab/core": true,
    "https://json-schema.org/draft/2020-12/vocab/applicator": true,
    "https://json-schema.org/draft/2020-12/vocab/unevaluated": true,
    "https://json-schema.org/draft/2020-12/vocab/validation": true,
    "https://json-schema.org/draft/2020-12/vocab/meta-data": true,
    "https://json-schema.org/draft/2020-12/vocab/format-annotation": true,
    "https://json-schema.org/draft/2020-12/vocab/content": true
  },
  "$dynamicAnchor": "meta",

  "$ref": "https://json-schema.org/draft/2020-12/schema",
  "unevaluatedProperties": false
}

For me, on jsonschema v4.17.3, validating this takes just over a minute.
I haven't profiled it at all, but I'm guessing that a good chunk of time might be spent on ref resolution for the draft 2020 schema if that's complex on the inside.

@Julian Julian reopened this May 8, 2023
@Julian Julian added the Needs Simplification An issue which is in need of simplifying the example or issue being demonstrated for diagnosis. label May 16, 2023
@Julian Julian changed the title Infinite loop when validating json schema strictly Validating a deeply nested schema using custom "strict" draft 2020 metaschema can be very slow Jul 18, 2023
@Julian
Copy link
Member

Julian commented Jul 18, 2023

The example here seems to complete in around ~1 second on my machine on 4.18.3. I'm going to call whatever was happening here fixed via the performance optimizations that went in with the referencing implementation, but if you're still seeing an issue, speak up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Simplification An issue which is in need of simplifying the example or issue being demonstrated for diagnosis.
Projects
None yet
Development

No branches or pull requests

3 participants