Skip to content
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

[PropertyAccess][Serializer] Fix "type unknown" on denormalize #52879

Merged
merged 1 commit into from
Dec 8, 2023

Conversation

seferov
Copy link
Contributor

@seferov seferov commented Dec 3, 2023

Q A
Branch? 7.1
Bug fix? no
New feature? yes
Deprecations? no
License MIT

The bug can clearly be seen on tests for RequestPayloadValueResolver on https://github.com/symfony/symfony/blob/6.3/src/Symfony/Component/HttpKernel/Tests/Controller/ArgumentResolver/RequestPayloadValueResolverTest.php#L255

// ...
  public function testValidationNotPassed()
  {
      $content = '{"price": 50, "title": ["not a string"]}';
      $payload = new RequestPayload(50);
      $serializer = new Serializer([new ObjectNormalizer()], ['json' => new JsonEncoder()]);
  
      // removed for simplification
  
      try {
          $resolver->onKernelControllerArguments($event);
          $this->fail(sprintf('Expected "%s" to be thrown.', HttpException::class));
      } catch (HttpException $e) {
          $validationFailedException = $e->getPrevious();
          $this->assertInstanceOf(ValidationFailedException::class, $validationFailedException);
          $this->assertSame('This value should be of type unknown.', $validationFailedException->getViolations()[0]->getMessage());
          $this->assertSame('Test', $validationFailedException->getViolations()[1]->getMessage());
      }
  }
}

class RequestPayload
{
    #[Assert\Length(min: 10, groups: ['strict'])]
    public string $title;

    public function __construct(public readonly float $price)
    {
    }
}

Even though title is string, the error message shown to user is ambiguous: "This value should be of type unknown.". Instead, the message should be "This value should be of type string."

@seferov seferov requested a review from dunglas as a code owner December 3, 2023 23:55
@carsonbot carsonbot added this to the 6.3 milestone Dec 3, 2023
@carsonbot carsonbot changed the title [Serializer] [PropertyAccess] Fix "type unknown" on denormalize [PropertyAccess][Serializer] Fix "type unknown" on denormalize Dec 3, 2023
@seferov seferov force-pushed the serializer-fix-normalize-unknown-type branch 3 times, most recently from 003dbee to 927f1bd Compare December 4, 2023 10:58
@yceruto
Copy link
Member

yceruto commented Dec 4, 2023

Even if it looks like a bug, I have doubts about whether this should go to 7.1 as an improvement.

@seferov seferov force-pushed the serializer-fix-normalize-unknown-type branch from 927f1bd to 810f4dd Compare December 4, 2023 12:16
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if it looks like a bug, I have doubts about whether this should go to 7.1 as an improvement.

I agree with this sentiment. Technically it's a new feature because it adds a new class so let's target 7.1.

@seferov seferov force-pushed the serializer-fix-normalize-unknown-type branch from 810f4dd to 5eda809 Compare December 4, 2023 17:37
@seferov seferov changed the base branch from 6.3 to 7.1 December 4, 2023 17:37
Copy link
Member

@yceruto yceruto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, Thank you Farhad!

Copy link
Member

@xabbuh xabbuh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The low deps failures are related. We need to bump the dev requirement on symfony/property-access to ^7.1.

@seferov seferov force-pushed the serializer-fix-normalize-unknown-type branch from 8eb8e6c to f345c20 Compare December 6, 2023 08:39
@xabbuh
Copy link
Member

xabbuh commented Dec 6, 2023

the composer.json file to update is located in src/Symfony/Component/HttpKernel

@seferov
Copy link
Contributor Author

seferov commented Dec 6, 2023

the composer.json file to update is located in src/Symfony/Component/HttpKernel

@xabbuh correct me if I am wrong but these unit tests on HttpKernel component runs with Serializer & ProperyAccess with version 7.1.x-dev, not with the code that are on the PR. That's why they fail.

@xabbuh
Copy link
Member

xabbuh commented Dec 7, 2023

@seferov Can you please rebase your changes on the latest 7.1 branch?

@seferov seferov force-pushed the serializer-fix-normalize-unknown-type branch from 61acfb1 to 849db42 Compare December 7, 2023 09:35
@seferov
Copy link
Contributor Author

seferov commented Dec 7, 2023

@seferov Can you please rebase your changes on the latest 7.1 branch?

@xabbuh done

@xabbuh
Copy link
Member

xabbuh commented Dec 7, 2023

@xabbuh correct me if I am wrong but these unit tests on HttpKernel component runs with Serializer & ProperyAccess with version 7.1.x-dev, not with the code that are on the PR.

In fact, our pipeline is a bit more complex and takes the changes from the PR into account: https://github.com/symfony/symfony/blob/7.1/.github/workflows/unit-tests.yml#L91

@seferov
Copy link
Contributor Author

seferov commented Dec 7, 2023

In fact, our pipeline is a bit more complex and takes the changes from the PR into account: https://github.com/symfony/symfony/blob/7.1/.github/workflows/unit-tests.yml#L91

@xabbuh I see.

Btw, only 8.2 (high deps) is failing now. The rest is passing

@@ -33,7 +33,7 @@
"symfony/http-kernel": "^6.4|^7.0",
"symfony/messenger": "^6.4|^7.0",
"symfony/mime": "^6.4|^7.0",
"symfony/property-access": "^6.4|^7.0",
"symfony/property-access": "^7.1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need the bump here. Let's rather skip the added test when the new exception class does not exist. Otherwise we won't be able to test the Serializer component with the real lowest allowed release of the PropertyAccess component.

@fabpot fabpot modified the milestones: 6.3, 7.1 Dec 8, 2023
@fabpot fabpot force-pushed the serializer-fix-normalize-unknown-type branch from 34d6030 to 4a665ac Compare December 8, 2023 14:01
@fabpot
Copy link
Member

fabpot commented Dec 8, 2023

Thank you @seferov.

@fabpot fabpot merged commit 5d50fa8 into symfony:7.1 Dec 8, 2023
5 of 7 checks passed
@seferov seferov deleted the serializer-fix-normalize-unknown-type branch December 8, 2023 14:33
@yareckon
Copy link

Question, @fabpot set the milestone here for both 6.3 and 7.1, but the merge only happened for 7.1. This inscrutability for the error messages still persists on 6.4.x -- the LTS branch. Could we get an official patch for that?

@OskarStark
Copy link
Contributor

Even if it looks like a bug, I have doubts about whether this should go to 7.1 as an improvement.

I agree with this sentiment. Technically it's a new feature because it adds a new class so let's target 7.1.

This is a new feature and will not be backported to 6.4 and the milestone is set for 7.1 only.

@yareckon
Copy link

Thanks for the response even if it's not the one I hoped for.

@fabpot fabpot mentioned this pull request May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants