Skip to content

Fix #827 #828

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

Merged
merged 8 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Fixed
- Fix objects are non-unique despite key order ([#819](https://github.com/jsonrainbow/json-schema/pull/819))
- Id's not being resolved and id property affects sibling ref which it should not do ([#828](https://github.com/jsonrainbow/json-schema/pull/828))

### Changed
- Added extra breaking change to UPDATE-6.0.md regarding BaseConstraint::addError signature change ([#823](https://github.com/jsonrainbow/json-schema/pull/823))
Expand Down
16 changes: 3 additions & 13 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -646,22 +646,17 @@ parameters:
path: src/JsonSchema/SchemaStorage.php

-
message: "#^Argument of an invalid type object supplied for foreach, only iterables are supported\\.$#"
count: 1
path: src/JsonSchema/SchemaStorage.php

-
message: "#^Call to function is_array\\(\\) with object will always evaluate to false\\.$#"
message: "#^Argument of an invalid type array\\|stdClass supplied for foreach, only iterables are supported\\.$#"
count: 1
path: src/JsonSchema/SchemaStorage.php

-
message: "#^Method JsonSchema\\\\SchemaStorage\\:\\:addSchema\\(\\) has no return type specified\\.$#"
message: "#^Argument of an invalid type object supplied for foreach, only iterables are supported\\.$#"
count: 1
path: src/JsonSchema/SchemaStorage.php

-
message: "#^Method JsonSchema\\\\SchemaStorage\\:\\:expandRefs\\(\\) has no return type specified\\.$#"
message: "#^Call to function is_array\\(\\) with object will always evaluate to false\\.$#"
count: 1
path: src/JsonSchema/SchemaStorage.php

Expand Down Expand Up @@ -695,11 +690,6 @@ parameters:
count: 1
path: src/JsonSchema/SchemaStorage.php

-
message: "#^Method JsonSchema\\\\SchemaStorageInterface\\:\\:addSchema\\(\\) has no return type specified\\.$#"
count: 1
path: src/JsonSchema/SchemaStorageInterface.php

-
message: "#^Method JsonSchema\\\\Uri\\\\Retrievers\\\\Curl\\:\\:fetchMessageBody\\(\\) has no return type specified\\.$#"
count: 1
Expand Down
55 changes: 40 additions & 15 deletions src/JsonSchema/SchemaStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function getUriResolver()
/**
* {@inheritdoc}
*/
public function addSchema($id, $schema = null)
public function addSchema(string $id, $schema = null): void
{
if (is_null($schema) && $id !== self::INTERNAL_PROVIDED_SCHEMA_URI) {
// if the schema was user-provided to Validator and is still null, then assume this is
Expand All @@ -62,14 +62,16 @@ public function addSchema($id, $schema = null)
// workaround for bug in draft-03 & draft-04 meta-schemas (id & $ref defined with incorrect format)
// see https://github.com/json-schema-org/JSON-Schema-Test-Suite/issues/177#issuecomment-293051367
if (is_object($schema) && property_exists($schema, 'id')) {
if ($schema->id == 'http://json-schema.org/draft-04/schema#') {
if ($schema->id === 'http://json-schema.org/draft-04/schema#') {
$schema->properties->id->format = 'uri-reference';
} elseif ($schema->id == 'http://json-schema.org/draft-03/schema#') {
} elseif ($schema->id === 'http://json-schema.org/draft-03/schema#') {
$schema->properties->id->format = 'uri-reference';
$schema->properties->{'$ref'}->format = 'uri-reference';
}
}

$this->addSubschemas($schema, $id);

// resolve references
$this->expandRefs($schema, $id);

Expand All @@ -79,39 +81,39 @@ public function addSchema($id, $schema = null)
/**
* Recursively resolve all references against the provided base
*
* @param mixed $schema
* @param string $base
* @param mixed $schema
*/
private function expandRefs(&$schema, $base = null)
private function expandRefs(&$schema, ?string $parentId = null): void
{
if (!is_object($schema)) {
if (is_array($schema)) {
foreach ($schema as &$member) {
$this->expandRefs($member, $base);
$this->expandRefs($member, $parentId);
}
}

return;
}

if (property_exists($schema, 'id') && is_string($schema->id) && $base != $schema->id) {
$base = $this->uriResolver->resolve($schema->id, $base);
}

if (property_exists($schema, '$ref') && is_string($schema->{'$ref'})) {
$refPointer = new JsonPointer($this->uriResolver->resolve($schema->{'$ref'}, $base));
$refPointer = new JsonPointer($this->uriResolver->resolve($schema->{'$ref'}, $parentId));
$schema->{'$ref'} = (string) $refPointer;
}

foreach ($schema as &$member) {
$this->expandRefs($member, $base);
$childId = $parentId;
if (property_exists($schema, 'id') && is_string($schema->id) && $childId !== $schema->id) {
$childId = $this->uriResolver->resolve($schema->id, $childId);
}

$this->expandRefs($member, $childId);
}
}

/**
* {@inheritdoc}
*/
public function getSchema($id)
public function getSchema(string $id)
{
if (!array_key_exists($id, $this->schemas)) {
$this->addSchema($id);
Expand All @@ -123,7 +125,7 @@ public function getSchema($id)
/**
* {@inheritdoc}
*/
public function resolveRef($ref, $resolveStack = [])
public function resolveRef(string $ref, $resolveStack = [])
{
$jsonPointer = new JsonPointer($ref);

Expand Down Expand Up @@ -174,4 +176,27 @@ public function resolveRefSchema($refSchema, $resolveStack = [])

return $refSchema;
}

/**
* @param mixed $schema
*/
private function addSubschemas($schema, string $parentId): void
{
if (!$schema instanceof \stdClass && !is_array($schema)) {
return;
}

foreach ($schema as $potentialSubSchema) {
if (!is_object($potentialSubSchema)) {
continue;
}

// Found sub schema
if (property_exists($potentialSubSchema, 'id') && is_string($potentialSubSchema->id) && property_exists($potentialSubSchema, 'type')) {
$this->addSchema($parentId . $potentialSubSchema->id, $potentialSubSchema);
}

$this->addSubschemas($potentialSubSchema, $parentId);
}
}
}
11 changes: 3 additions & 8 deletions src/JsonSchema/SchemaStorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,23 @@ interface SchemaStorageInterface
/**
* Adds schema with given identifier
*
* @param string $id
* @param object $schema
*/
public function addSchema($id, $schema = null);
public function addSchema(string $id, $schema = null): void;

/**
* Returns schema for given identifier, or null if it does not exist
*
* @param string $id
*
* @return object
*/
public function getSchema($id);
public function getSchema(string $id);

/**
* Returns schema for given reference with all sub-references resolved
*
* @param string $ref
*
* @return object
*/
public function resolveRef($ref);
public function resolveRef(string $ref);

/**
* Returns schema referenced by '$ref' property
Expand Down