Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Compatibility issue with Symfony 7.2: Cannot create a "Symfony\Component\TypeInfo\Type\NullableType" error #59137

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

Closed
scorpion3dd opened this issue Dec 8, 2024 · 7 comments

Comments

@scorpion3dd
Copy link

Symfony version(s) affected

7.2.0

Description

After upgrading Symfony to version 7.2, my project stopped working with the following error:
Cannot create a "Symfony\Component\TypeInfo\Type\NullableType" with "mixed" because it is already nullable in . (which is being imported from "/var/www/back/config/routes/api_platform.yaml"). Make sure there is a loader supporting the "api_platform" type.

This error points to the api_platform type in my config/routes/api_platform.yaml file:
api_platform:
resource: .
type: api_platform
prefix: /api

The issue seems to be related to changes introduced in Symfony 7.2, specifically in the redesigned TypeInfo component, as explained in this blog post: New in Symfony 7.2: Redesigned TypeInfo Component.

It appears that the mixed type is now always nullable, and API Platform may not handle this properly. Before the update, everything worked fine with Symfony 7.1.

How to reproduce

Steps to Reproduce:

Use Symfony 7.2 and the latest version of API Platform.
Create a basic api_platform.yaml file like the one above.
Access any route managed by API Platform.
Expected Behavior:
The routes should be processed normally without any errors.

Actual Behavior:
The error mentioned above is thrown, and the application fails to start.
http://prntscr.com/tT42Y1r46TGI

Steps Taken:
Updated all dependencies to their latest versions.
Cleared the Symfony cache (php bin/console cache:clear).
Verified that the error disappears when downgrading Symfony to version 7.1.

Please let me know if further details or logs are needed.

Possible Solution

No response

Additional Context

No response

@chalasr
Copy link
Member

chalasr commented Dec 8, 2024

/cc @mtarld

@xabbuh
Copy link
Member

xabbuh commented Dec 8, 2024

Here is a test that allows to reproduce the error:

public function testNullable()
{
    $mixed = Type::mixed();

    $this->assertSame($mixed, Type::nullable($mixed));
}

Status: Reviewed

@soyuka
Copy link
Contributor

soyuka commented Dec 9, 2024

Isn't that fixed already? api-platform/core@4d7deea

Versions: v4.0.11 v3.4.8

Also, not a symfony issue imo.

@mtarld
Copy link
Contributor

mtarld commented Dec 9, 2024

Indeed, sounds like an issue in API Platform here.
But, with a fresh install (Symfony 7.2 and API Platform 4.0), I cannot reproduce the issue.

And, IMHO that doesn't make sense to convert a mixed to its nullable version as it's already nullable. Therefore, this exception is logical.
But it might be room for improvement by making Type::nullable method no-op when dealing with a mixed type.

@xabbuh
Copy link
Member

xabbuh commented Dec 9, 2024

What's a bit unfortunate though is that mixed and null are not instances of NullableType and thus passing them to Type::nullable() and thus behaves differently than passing a NullableType object.

@mtarld
Copy link
Contributor

mtarld commented Dec 9, 2024

Agreed. What I had in mind was to update the nullable method to handle them properly:

public static function nullable(Type $type): Type
{
    if ($type->isNullable()) {
        return $type;
    }

    return new NullableType($type);
}

But, even if 7.2 just has been released, this will update the return type and therefore will be a BC break 🫤

@chalasr
Copy link
Member

chalasr commented Dec 9, 2024

If it's the best fix for the case at hand I'd agree to do it as a bugfix given it is broken today.

fabpot added a commit that referenced this issue Dec 11, 2024
…lable type (mtarld)

This PR was merged into the 7.2 branch.

Discussion
----------

[TypeInfo] Make `Type::nullable` method no-op on every nullable type

| Q             | A
| ------------- | ---
| Branch?       | 7.2
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Issues        | Fix #59137
| License       | MIT

Make `Type::nullable` method no-op on every nullable type, not only on `NullableType` types.

Commits
-------

640a8d5 [TypeInfo] Fix handle nullable with mixed
@fabpot fabpot closed this as completed Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants