How convert string to int with Symfony Serializer

I’m trying to denormalize an array into an object, but when the denormalize method comes across a code field that is a number, it gets an error since it is a string in the array, maybe it’s fashionable to fix it

{
  "name": "Exception",
  "message": "The type of the "code" attribute for class "AppHttpResponseFlightDriverResponse" must be one of "int" ("string" given).",
  "code": 0,
  "type": "SymfonyComponentSerializerExceptionNotNormalizableValueException",
  "file": "/app/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php",
  "line": 445,
  "stack-trace": [
    "#0 /app/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php(331): SymfonyComponentSerializerNormalizerAbstractObjectNormalizer->validateAndDenormalize('App\Http\Respon...', 'code', '1', 'array', Array)",
    "#1 /app/vendor/symfony/serializer/Serializer.php(185): SymfonyComponentSerializerNormalizerAbstractObjectNormalizer->denormalize(Array, 'App\Http\Respon...', 'array', Array)",
    "#2 /app/src/Http/Response/Transformer.php(96): SymfonyComponentSerializerSerializer->denormalize(Array, 'App\Http\Respon...', 'array')",
    "#3 [internal function]: AppHttpResponseTransformer->AppHttpResponse{closure}(Array)",
    "#4 /app/src/Http/Response/Transformer.php(98): array_map(Object(Closure), Array)",
    "#5 /app/src/Http/Controllers/Api/Flight/DriverController.php(124): AppHttpResponseTransformer->transform(Object(yiidataSqlDataProvider), 'drivers', 'App\Http\Respon...')",
    "#6 [internal function]: AppHttpControllersApiFlightDriverController->actionIndex()",
    "#7 /app/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)",
    "#8 /app/vendor/yiisoft/yii2/base/Controller.php(157): yiibaseInlineAction->runWithParams(Array)",
    "#9 /app/vendor/yiisoft/yii2/base/Module.php(528): yiibaseController->runAction('index', Array)",
    "#10 /app/vendor/yiisoft/yii2/web/Application.php(103): yiibaseModule->runAction('Api/Flight/driv...', Array)",
    "#11 /app/vendor/yiisoft/yii2/base/Application.php(386): yiiwebApplication->handleRequest(Object(yiiwebRequest))",
    "#12 /app/web/index.php(14): yiibaseApplication->run()",
    "#13 {main}"
  ]
}

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *