Skip to content

Reference resolution does not understand schema structure #15

Closed

Description

@JanTvrdik

Both Walker::parseSchema and Walker::applyConstraints relies on constraints to corrently traverse the schema / instance. Walker::resolveReferences does not and therefore e.g. the following test fails

{
  "title": "issue #15: References under unknown keywords should be ignored",
  "tests": [
    {
      "title": "$ref in unknown property",
      "schema": {
        "unknown": {
          "$ref": "#/foo"
        }
      },
      "valid": [
        {},
        123
      ]
    }
  ]
}

The possible solutions I can think of

  1. Add new method to the Constraint interface which would work similarly to Constraint::normalize
  2. Merge $ref resolution with the normalize phase, i.e. resolve references during normalize traversal
  3. Extract the traversing logic from normalize to a new method, e.g. Constraint::getSubSchemas(). This may require increasing the required PHP version from 5.4 to 5.5 because of yield.

Possible implementations of ItemsConstraint::getSubSchemas.

public function getSubSchemas(stdClass $schema)
{
    if (!isset($schema->items)) {
        // ignore
    } elseif (is_object($schema->items)) {
        yield 'items' => $schema->items;
    } elseif (is_array($schema->items)) {
        foreach ($schema->items as $index => $item) {
            yield "items/$index" => $schema->items[$index];
        }
    }

    if (!isset($schema->additionalItems)) {
        // ignore
    } elseif (is_object($schema->additionalItems)) {
        yield 'additionalItems' => $schema->additionalItems;
    }
}

Activity

  1. stefk commented on Apr 24, 2016

    @stefk
    Owner

    Thanks for reporting this. The spec states that unknown should be ignored and that's indeed not the case in the resolution step. For now, I'm gonna go a different, simpler route to solve this but I take note of your suggestions for a post v1 implementation I have my mind on.

  2. added a commit that references this issue on Apr 24, 2016
    b31b0a1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions