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

Fix parsing of schemas that are combinations of unnamed schemas and oneOf/allOf/... #566

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

anzev
Copy link

@anzev anzev commented Jan 3, 2024

This is an attempt to fix the issue described in my comment here.

Basically, the issue arises when parsing a schema that is a combination of a oneOf/allOf/... and an unnamed schema (see example below), for example, the output interface for the array items would completely ignore the properties part of the schema.

This was due to the way how schema type matching was done. It would match only as ONE_OF and not a combination of [ONE_OF, UNNAMED_SCHEMA]. If we would add an $id to the definition, the schema would be parsed correctly as [ONE_OF, NAMED_SCHEMA].

This PR attempts to fix this issue by changing how UNNAMED_SCHEMA is matched by adding an explicit condition. So far it was handled as: if nothing else matches, default to UNNAMED_SCHEMA, which is why it never matched in scenarios described above.

I've added two tests and updated several existing tests, since the new condition generates types that were previously missed.

Please check it out and let me know any improvement and if this is a valid solution in your opinion.

Thanks!


Example:

{
  "type": "object",
  "$schema": "http://json-schema.org/draft-07/schema",
  "properties": {
    "things": {
      "type": "array",
      "minLength": 1,
      "description": "A list of things",
      "items": {
        "type": "object",
        "required": ["thingProp1", "thingProp2"],
        "oneOf": [
          {
            "type": "object",
            "properties": {
              "propOption1": {
                "type": "string"
              }
            },
            "required": ["propOption1"]
          },
          {
            "type": "object",
            "properties": {
              "propOption2": {
                "type": "number"
              }
            },
            "required": ["propOption2"]
          }
        ],
        "properties": {
          "thingProp1": {
            "type": "string"
          },
          "thingProp2": {
            "type": "string"
          }
        }
      }
    }
  }
}

@anzev anzev changed the title Correctly parse schemas that are combinations of unnamed schemas and … Fix parsing of schemas that are combinations of unnamed schemas and oneOf/allOf/... Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant