Symfony 4 + Api platform Cannot validate values of type \"NULL\" automatically. on nativeSQL query - postgresql

i have been stuck for few hours with this:
Doing a simple query on my Variation base :
$query = "UPDATE variation SET status = 'FALSE' WHERE id IN
('ba21ac3f-0e7f-4bb9-b31f-dbd49053002e','a656e102-0339-4db0-a4b1-16451c2f1d90','64da7d32-2c9f-4478-bc6d-0b7b6c07af8e','8aba2925-7017-4899-bdfa-bf341c0fccec','6d4374cb-3713-4162-be53-2232202b5809','1810057a-6157-45bf-85d5-ade282b7d807','967d7495-6e93-4530-b6d8-d23640b39226','ebdcd88f-1f58-44b6-a14d-d0db9a564671')";
$result = $this->entityManager->getConnection()->prepare($query);
$result->execute();
And using postman it's returning me :
"#context": "/contexts/Error",
"#type": "hydra:Error",
"hydra:title": "An error occurred",
"hydra:description": "Cannot validate values of type \"NULL\" automatically. Please provide a constraint."`
my variation class:
/**
* #var bool
*
* #ORM\Column(type="boolean")
*
* #Groups({"variation"})
*/
protected $status;
How comes it try to validate somethings. Ps the query is fully working ( POSTGRES - Symfony 4.2 - PHP72 )

If body content is empty in request, its possible to correct that if content type are is also empty in header request

Related

Punchout Iframe issue with Magento 2

I am trying to integrate the punchout system with Magento 2.
Magento does not allow opening iframe due to security reasons. I have added 'x-frame-options' => '*' option to the env.php file but still not allow to open the Iframe.
Also, I have changed the below file but I did not get the exact result.
vendor\magento\framework\App\Response\HeaderProvider\XFrameOptions.php
I am getting the below response from punchout. After that I am getting a blank screen.
In the punchout response x-frame-options showing twice.
Can you guide which settings are required to open the site in the Iframe?
Thanks in advance
I found a solution:
1 - Remove the x-frame-options from the env.php
2 - Override the \Magento\Framework\App\Response\HeaderProvider\XFrameOptions.php file ( don't forget the di.xml ) and comment the content of the __construct
class XFrameOptions extends \Magento\Framework\App\Response\HeaderProvider\XFrameOptions
{
/** Deployment config key for frontend x-frame-options header value */
const DEPLOYMENT_CONFIG_X_FRAME_OPT = 'x-frame-options';
/** Always send SAMEORIGIN in backend x-frame-options header */
const BACKEND_X_FRAME_OPT = 'SAMEORIGIN';
/**
* x-frame-options Header name
*
* #var string
*/
protected $headerName = Http::HEADER_X_FRAME_OPT;
/**
* x-frame-options header value
*
* #var string
*/
protected $headerValue;
/**
* #param string $xFrameOpt
*/
public function __construct($xFrameOpt = 'SAMEORIGIN')
{
//$this->headerValue = $xFrameOpt;
}
}
I think this will work!

Unexpected data found during update on eloquent / Laravel

I am using ajax to update a model that contains timestamps, but it throw me an exception:
{message: "Unexpected data found.", exception: "InvalidArgumentException",…}
message: "Unexpected data found."
exception: "InvalidArgumentException"
file: "/home/asus/Devagnos/almada/vendor/nesbot/carbon/src/Carbon/Traits/Creator.php"
line: 623
trace: [,…]
i have disabled the timestamps and i set the dateformat like this:
protected $dateFormat = 'Y-m-d H:i:s.u';
public $timestamps = false;
protected $dates = [
'created_at',
'updated_at'
];
alse I added these methods
/**
* #param $val
*/
public function setCreatedAtAttribute($val)
{
return Carbon::createFromFormat('Y-m-d H:i:s.u', $val);
}
/**
* #param $val
*/
public function setUpdatedAtAttribute($val)
{
return Carbon::createFromFormat('Y-m-d H:i:s.u', $val);
}
but I am always getting the same error, What am I doing wrong ?
I'm using laravel 6.8 and postgresql
If you're trying to use microseconds, then you should refer to this guide from the documentation:
https://carbon.nesbot.com/laravel/
I don't get what you tried with setCreatedAtAttribute and setUpdatedAtAttribute, setters are supposed to change the inner property, not to return a value.
Then check you gave to your DB columns enough precision (such as TIMESTAMP(6)) in your migration schemas.

Set content-type HTTP header for 404 page in TYPO3 9.5

I am making a webservice using TYPO3. Everything in the frontend should be JSON with HTTP header Content-Type: application/json, however I am unable to change the Content-Type header for the 404 page. Whatever I try, it's always Content-Type: text/html; charset=utf-8. How can I change this?
This is my basic page TypoScript configuration:
page = PAGE
page {
config {
disableAllHeaderCode = 1
disablePrefixComment = 1
xhtml_cleaning = 0
admPanel = 0
}
10 = USER
10 {
userFunc = TYPO3\CMS\Extbase\Core\Bootstrap->run
extensionName = MyExt
pluginName = MyPlugin
vendorName = MyVendor
}
}
The extension uses TYPO3\CMS\Extbase\Mvc\View\JsonView.
I've already tried adding the header using config.additionalHeaders.10.header = Content-Type: application/json. I've also tried setting [FE][pageNotFound_handling] to USER_FUNCTION:... and setting the headers in PHP.
I found the solution myself. From TYPO3 9.5 the default page not found handling can be overridden in the site configuration:
errorHandling:
-
errorCode: 404
errorHandler: PHP
errorPhpClassFQCN: Vendor\MyExt\PageErrorHandler\PageNotFoundHandler
And in EXT:MyExt/Classes/PageErrorHandler/PageNotFoundHandler.php:
<?php
namespace Vendor\MyExt\PageErrorHandler;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Error\PageErrorHandler\PageErrorHandlerInterface;
/**
* Class PageNotFoundHandler
*/
class PageNotFoundHandler implements PageErrorHandlerInterface
{
/**
* Handle page error
*
* #param ServerRequestInterface $request
* #param string $message
* #param array $reasons
* #return ResponseInterface
*/
public function handlePageError(ServerRequestInterface $request, string $message, array $reasons = []): ResponseInterface
{
$response = new Response(404, ['Content-Type' => 'application/json'], '{"error":"Not found"}');
return $response;
}
}

Creating Routing Paths using FosRestFundle in Symfony

I am developing an Api for one of my projects in Symfony and I want create several routing paths like the following:
/api/messages/{id}
/api/messages/creator/{username}
/api/messages/receiver/{username}
My routing files are configured in this way (I will post just the info relating to the api config):
routing.yml:
NelmioApiDocBundle:
resource: "#NelmioApiDocBundle/Resources/config/routing.yml"
prefix: "/doc"
api:
type: rest
resource: "routing_api.yml"
prefix: /api
routing_api.yml:
api_message:
type: rest
resource: AppBundle\Controller\ApiMessagesController
api_chats:
type: rest
resource: AppBundle\Controller\ApiChatController
And my ApiMessagesController:
<?php
namespace AppBundle\Controller;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\View\RouteRedirectView;
use FOS\RestBundle\Controller\Annotations\RouteResource;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
/**
* Class ApiMessagesController
* #package AppBundle\Controller
*
* #RouteResource("messages")
*/
class ApiMessagesController extends FOSRestController{
/**
* Gets an individual Message
*
* #param int $id
* #return mixed
* #throws \Doctrine\ORM\NoResultException
* #throws \Doctrine\ORM\NonUniqueResultException
*
* #ApiDoc(
* output="AppBundle\Entity\Message",
* statusCodes={
* 200 = "Returned when successful",
* 404 = "Return when not found"
* }
* )
*/
public function getAction($id){;
return $this->get('app.message.manager')->findById($id);
}
/**
* Gets all messages sent by each users
*
* #param int $id
* #return mixed
* #throws \Doctrine\ORM\NoResultException
* #throws \Doctrine\ORM\NonUniqueResultException
*
* #ApiDoc(
* output="AppBundle\Entity\Message",
* statusCodes={
* 200 = "Returned when successful",
* 404 = "Return when not found"
* }
* )
*/
public function getCreatorAction($creator){
return $this->get('app.message.manager')->findByCreator($creator);
}
}
The route /api/messages/{id} works as I expected but I am really stuck right now about how to define the route /api/messages/creator/{creator} because Nelmio is telling me that I created this route /api/messages/{creator}/creator instead of /api/messages/creator/{username}
I think that I put all info regarding to my issue, but if something is missing tell me and I will update my question. Thanks
If you're not happy with URLs FOSRest does, you can always use #Route annotation and redefine everything you need.

doctrine migrations bundle and postgres schema: diff does not work properly

I'm using doctrine in my Symfony project, by connecting to an already existent postgres database.
The DB has several schemas, but the symfony app will use nothing but its own schema. The first Entity class I created is the following one:
namespace Belka\TestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="app_auth.User", schema="app_auth")
*/
class User {
/**
* #ORM\Column(type="string")
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $username;
/**
* #ORM\Column(type="string")
*/
private $email;
/**
* #ORM\Column(type="string")
*/
private $password;
}
as you can see, the Entity is specifying its own schema app_auth.
Next, I tried to use the migrations bundle. So, I installed and configured it, in order not to consider anything but my schema:
Extract of config.yml:
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
schema_filter: ~^app_auth\..*~
Extract of config_dev.yml:
doctrine_migrations:
dir_name: "%kernel.root_dir%/../.container/update/DoctrineMigrations"
namespace: Application\Migrations
table_name: "app_auth.migration_versions"
name: Application Migrations
And I run the diff:
php app/console doctrine:migrations:diff
Unfortunately, the migration class generated is the following one:
namespace Application\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
/**
* Auto-generated Migration: Please modify to your needs!
*/
class Version20160422171409 extends AbstractMigration
{
/**
* #param Schema $schema
*/
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
}
/**
* #param Schema $schema
*/
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
$this->addSql('CREATE SCHEMA app_auth');
}
}
what's wrong with my configuration then?
Dirty but working solution:
Tested with doctrine/migrations: 3.0.1
We can patch out the schema removing functionality of the DiffGenerator. I personally used this https://github.com/cweagans/composer-patches
Install the package, and then in composer.json "extra" section add:
"patches": {
"doctrine/migrations": {
"Change doctrine behavior to correctly pass PostgreSQL schema to schema_filter while diffing": "patches/doctrine-diff-generator.patch"
}
}
The patch file in patches/doctrine-diff-generator.patch:
--- lib/Doctrine/Migrations/Generator/DiffGenerator.php 2020-06-21 10:55:42.000000000 +0200
+++ lib/Doctrine/Migrations/Generator/DiffGenerator.patched.php 2020-12-23 12:33:02.689405221 +0100
## -142,8 +142,6 ##
*/
private function resolveTableName(string $name) : string
{
- $pos = strpos($name, '.');
-
- return $pos === false ? $name : substr($name, $pos + 1);
+ return $name;
}
}
Of course you have to bare in mind, that updates to doctrine could break the patch. And you are changing this functionality globally for your doctrine, so beware of breaking changes.
Personally, for my use case, which is adding doctrine entities into legacy database, without breaking it, this works nicely, and saves me from adding every new table managed by doctrine into the schema_filter.
Explanation:
When we look into DiffGenerator implementation it actually decodes the table names with schema correctly, but only when collecting the current database schema. This happens in PostgreSqlSchemaManager.php:
/**
* {#inheritdoc}
*/
protected function _getPortableTableDefinition($table)
{
$schemas = $this->getExistingSchemaSearchPaths();
$firstSchema = array_shift($schemas);
if ($table['schema_name'] === $firstSchema) {
return $table['table_name'];
}
return $table['schema_name'] . '.' . $table['table_name'];
}
But then then when calculating the target schema, this information is lost here in DiffGenerator.php:
/**
* Resolve a table name from its fully qualified name. The `$name` argument
* comes from Doctrine\DBAL\Schema\Table#getName which can sometimes return
* a namespaced name with the form `{namespace}.{tableName}`. This extracts
* the table name from that.
*/
private function resolveTableName(string $name) : string
{
$pos = strpos($name, '.');
return $pos === false ? $name : substr($name, $pos + 1);
}
And only afterwards the return value of this function is passed into the schema_filter. Unfortunate, but i guess there was a reason for this :)