Skip to content
Advertisement

symfony Declaration of AppSwaggerSwaggerDecorator::normalize must be compatible

I use api platform to create api rest but in configuration package i found this error.

enter image description here

I copie and paste this code in documentation :

https://api-platform.com/docs/core/jwt/#adding-endpoint-to-swaggerui-to-retrieve-a-jwt-token

and this is my code:

<?php

declare(strict_types=1);

namespace AppSwagger;

use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentSerializerNormalizerNormalizerInterface;

final class SwaggerDecorator implements NormalizerInterface
{
    /**
     *
     * @var NormalizerInterface
     */
    private NormalizerInterface $decorated;

    public function __construct(NormalizerInterface $decorated)
    {
        $this->decorated = $decorated;
    }

    public function supportsNormalization($data, string $format = null): bool
    {
        return $this->decorated->supportsNormalization($data, $format);
    }

    public function normalize($object, string $format = null, array $context = [])
    {
        $docs = $this->decorated->normalize($object, $format, $context);
        $docs['components']['schemas']['Token'] = [
            'type' => 'object',
            'properties' => [
                'token' => [
                    'type' => 'string',
                    'readOnly' => true,
                ],
            ],
        ];

        $docs['components']['schemas']['Credentials'] = [
            'type' => 'object',
            'properties' => [
                'username' => [
                    'type' => 'string',
                    'example' => 'api',
                ],
                'password' => [
                    'type' => 'string',
                    'example' => 'api',
                ],
            ],
        ];

        $tokenDocumentation = [
            'paths' => [
                '/authentication_token' => [
                    'post' => [
                        'tags' => ['Token'],
                        'operationId' => 'postCredentialsItem',
                        'summary' => 'Get JWT token to login.',
                        'requestBody' => [
                            'description' => 'Create new JWT Token',
                            'content' => [
                                'application/json' => [
                                    'schema' => [
                                        '$ref' => '#/components/schemas/Credentials',
                                    ],
                                ],
                            ],
                        ],
                        'responses' => [
                            Response::HTTP_OK => [
                                'description' => 'Get JWT token',
                                'content' => [
                                    'application/json' => [
                                        'schema' => [
                                            '$ref' => '#/components/schemas/Token',
                                        ],
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ];

        return array_merge_recursive($docs, $tokenDocumentation);
    }
}

Advertisement

Answer

As explained in the error message, your method normalize shall be exactly the same as it was declared in the interface.

Replace:

    public function normalize($object, string $format = null, array $context = [])

By:

    public function normalize($object, $format = null, array $context = [])
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement