php-json-schema icon indicating copy to clipboard operation
php-json-schema copied to clipboard

Definitions property is expected to be an object, not an array

Open uuf6429 opened this issue 1 year ago • 0 comments

When I try creating a schema with an array/hashmap of definitions it complains that definitions was expected to be an object.

JsonSchema\Schema::import((object)[
    'definitions' => [
        'aaa' => JsonSchema\Schema::import((object)['type' => 'object'])
    ]
]);

Swaggest\JsonSchema\Exception\TypeException: Object expected, {"aaa":{"type":"object"}} received at #->properties:definitions

Typecasting that array to an object fixes the problem (it actually shows a different error, but that's irrelevant):

JsonSchema\Schema::import((object)[
    'definitions' => (object)[       //    <------
        'aaa' => JsonSchema\Schema::import((object)['type' => 'object'])
    ]
]);

So I believe the PHPDoc here is wrong: https://github.com/swaggest/php-json-schema/blob/2b99251f31046f682b120fb807fea8ab688a493e/src/JsonSchema.php#L103

In this case it would have been object<string, JsonSchema>, but that's not a supported syntax. So maybe either \ArrayObject<string, JsonSchema> or simply an \object with an explanatory description.

uuf6429 avatar Jul 13 '24 13:07 uuf6429