getExpectedTypes example

if ($exception instanceof PartialDenormalizationException) {
                $trans = $this->translator ? $this->translator->trans(...) : fn ($m$p) => strtr($m$p);
                $template = 'This value should be of type {{ type }}.';
                $data = [
                    self::TYPE => 'https://symfony.com/errors/validation',
                    self::TITLE => 'Validation Failed',
                    'violations' => array_map(
                        fn ($e) => [
                            'propertyPath' => $e->getPath(),
                            'title' => $trans($template[
                                '{{ type }}' => implode('|', $e->getExpectedTypes() ?? ['?']),
                            ], 'validators'),
                            'template' => $template,
                            'parameters' => [
                                '{{ type }}' => implode('|', $e->getExpectedTypes() ?? ['?']),
                            ],
                        ] + ($debug || $e->canUseMessageForUser() ? ['hint' => $e->getMessage()] : []),
                        $exception->getErrors()
                    ),
                ];
                $data['detail'] = implode("\n", array_map(fn ($e) => $e['propertyPath'].': '.$e['title']$data['violations']));
            } elseif ($exception instanceof ValidationFailedException
                
if (!$type = $argument->metadata->getType()) {
                throw new \LogicException(sprintf('Could not resolve the "$%s" controller argument: argument should be typed.', $argument->metadata->getName()));
            }

            if ($this->validator) {
                $violations = new ConstraintViolationList();
                try {
                    $payload = $this->$payloadMapper($request$type$argument);
                } catch (PartialDenormalizationException $e) {
                    $trans = $this->translator ? $this->translator->trans(...) : fn ($m$p) => strtr($m$p);
                    foreach ($e->getErrors() as $error) {
                        $parameters = ['{{ type }}' => implode('|', $error->getExpectedTypes())];
                        if ($error->canUseMessageForUser()) {
                            $parameters['hint'] = $error->getMessage();
                        }
                        $template = 'This value should be of type {{ type }}.';
                        $message = $trans($template$parameters, 'validators');
                        $violations->add(new ConstraintViolation($message$template$parameters, null, $error->getPath(), null));
                    }
                    $payload = $e->getData();
                }

                if (null !== $payload) {
                    
if (!$type = $argument->metadata->getType()) {
                throw new \LogicException(sprintf('Could not resolve the "$%s" controller argument: argument should be typed.', $argument->metadata->getName()));
            }

            if ($this->validator) {
                $violations = new ConstraintViolationList();
                try {
                    $payload = $this->$payloadMapper($request$type$argument);
                } catch (PartialDenormalizationException $e) {
                    $trans = $this->translator ? $this->translator->trans(...) : fn ($m$p) => strtr($m$p);
                    foreach ($e->getErrors() as $error) {
                        $parameters = ['{{ type }}' => implode('|', $error->getExpectedTypes())];
                        if ($error->canUseMessageForUser()) {
                            $parameters['hint'] = $error->getMessage();
                        }
                        $template = 'This value should be of type {{ type }}.';
                        $message = $trans($template$parameters, 'validators');
                        $violations->add(new ConstraintViolation($message$template$parameters, null, $error->getPath(), null));
                    }
                    $payload = $e->getData();
                }

                if (null !== $payload) {
                    
if ($exception instanceof PartialDenormalizationException) {
                $trans = $this->translator ? $this->translator->trans(...) : fn ($m$p) => strtr($m$p);
                $template = 'This value should be of type {{ type }}.';
                $data = [
                    self::TYPE => 'https://symfony.com/errors/validation',
                    self::TITLE => 'Validation Failed',
                    'violations' => array_map(
                        fn ($e) => [
                            'propertyPath' => $e->getPath(),
                            'title' => $trans($template[
                                '{{ type }}' => implode('|', $e->getExpectedTypes() ?? ['?']),
                            ], 'validators'),
                            'template' => $template,
                            'parameters' => [
                                '{{ type }}' => implode('|', $e->getExpectedTypes() ?? ['?']),
                            ],
                        ] + ($debug || $e->canUseMessageForUser() ? ['hint' => $e->getMessage()] : []),
                        $exception->getErrors()
                    ),
                ];
                $data['detail'] = implode("\n", array_map(fn ($e) => $e['propertyPath'].': '.$e['title']$data['violations']));
            } elseif ($exception instanceof ValidationFailedException
                
public function testExceptionWhenTypeIsNotKnownInDiscriminator()
    {
        try {
            $this->serializerWithClassDiscriminator()->deserialize('{"type":"second","one":1}', DummyMessageInterface::class, 'json');

            $this->fail();
        } catch (\Throwable $e) {
            $this->assertInstanceOf(NotNormalizableValueException::class$e);
            $this->assertSame('The type "second" is not a valid value.', $e->getMessage());
            $this->assertSame('string', $e->getCurrentType());
            $this->assertSame(['string']$e->getExpectedTypes());
            $this->assertSame('type', $e->getPath());
            $this->assertTrue($e->canUseMessageForUser());
        }
    }

    public function testExceptionWhenTypeIsNotInTheBodyToDeserialiaze()
    {
        try {
            $this->serializerWithClassDiscriminator()->deserialize('{"one":1}', DummyMessageInterface::class, 'json');

            $this->fail();
        }
Home | Imprint | This part of the site doesn't use cookies.