Skip to content

4.2 assertMatchesResourceItemJsonSchema fail on Dto not an ApiResource #7420

@Cafeine42

Description

@Cafeine42

API Platform version(s) affected: 4.2

Description
When running API tests on endpoints using DTOs for both input and output, the assertion assertMatchesResourceItemJsonSchema fails with the following error:

JsonSchema\Exception\UnresolvableJsonPointerException: File: internal://provided-schema/ is found, but could not resolve fragment: #/definitions/HydraItemBaseSchema

The issue occurs in src/Hydra/JsonSchema/SchemaFactory.php because of the following logic:

$baseName = Schema::TYPE_OUTPUT === $type ? self::ITEM_BASE_SCHEMA_NAME : self::ITEM_BASE_SCHEMA_OUTPUT_NAME;

if ($this->isResourceClass($inputOrOutputClass)) {
    if (!isset($definitions[$baseName])) {
        $definitions[$baseName] = Schema::TYPE_OUTPUT === $type ? self::ITEM_BASE_SCHEMA_OUTPUT : self::ITEM_BASE_SCHEMA;
    }
}

Since isResourceClass returns false for a DTO, the HydraItemBaseSchema component is never created.

How to reproduce
Use an output: SomeDto where SomeDto has not an ApiResource attribute.

Possible Solution
Could the isResourceClass condition be removed, and what would be the consequences of doing so?
Is this assertion (assertMatchesResourceItemJsonSchema) no longer supported for DTOs that are not ApiResource classes, both now and in the future?

This change seems to originate from this commit.

Additional Context

JsonSchema\Exception\UnresolvableJsonPointerException: File: internal://provided-schema/ is found, but could not resolve fragment: #/definitions/HydraItemBaseSchema
/app/vendor/justinrainbow/json-schema/src/JsonSchema/SchemaStorage.php:154
/app/vendor/justinrainbow/json-schema/src/JsonSchema/SchemaStorage.php:179
/app/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Constraint.php:115
/app/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/UndefinedConstraint.php:313
/app/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/UndefinedConstraint.php:40
/app/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Constraint.php:115
/app/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/SchemaConstraint.php:95
/app/vendor/justinrainbow/json-schema/src/JsonSchema/Validator.php:67
/app/vendor/api-platform/core/src/Symfony/Bundle/Test/Constraint/MatchesJsonSchema.php:53
/app/vendor/api-platform/core/src/Symfony/Bundle/Test/ApiTestAssertionsTrait.php:113
/app/vendor/api-platform/core/src/Symfony/Bundle/Test/ApiTestAssertionsTrait.php:147

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions