-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
[php-symfony] Symfony6 support #11810
Changes from 3 commits
adf2682
c37e7b2
b3ce222
b282ff4
ddcd3e3
5804252
de50943
24c5ed4
580c30c
06fdf15
ca47e62
9e20af4
0dc4187
f4e6744
0140c47
f37afb9
acbe959
5a2674a
511f311
2d87054
4241833
c536995
292b638
108c595
f84d2d7
5139f3d
5fc6f39
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
language: php | ||
dist: trusty | ||
php: | ||
- 7.1.3 | ||
- 7.2 | ||
- 8.1.1 | ||
|
||
install: | ||
- composer install --dev --no-interaction | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,18 +5,22 @@ namespace {{servicePackage}}; | |
use JMS\Serializer\SerializerBuilder; | ||
use JMS\Serializer\Naming\CamelCaseNamingStrategy; | ||
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy; | ||
use JMS\Serializer\Visitor\Factory\XmlDeserializationVisitorFactory; | ||
use JMS\Serializer\XmlDeserializationVisitor; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This unused import statement can be removed |
||
use DateTime; | ||
use RuntimeException; | ||
|
||
class JmsSerializer implements SerializerInterface | ||
{ | ||
protected $serializer; | ||
|
||
public function __construct() | ||
{ | ||
$naming_strategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy()); | ||
$namingStrategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy()); | ||
$this->serializer = SerializerBuilder::create() | ||
->setDeserializationVisitor('json', new StrictJsonDeserializationVisitor($naming_strategy)) | ||
->setDeserializationVisitor('xml', new XmlDeserializationVisitor($naming_strategy)) | ||
->setDeserializationVisitor('json', new StrictJsonDeserializationVisitorFactory()) | ||
->setDeserializationVisitor('xml', new XmlDeserializationVisitorFactory()) | ||
->setPropertyNamingStrategy($namingStrategy) | ||
->build(); | ||
} | ||
|
||
|
@@ -79,6 +83,11 @@ class JmsSerializer implements SerializerInterface | |
} | ||
|
||
break; | ||
case 'DateTime': | ||
case '\DateTime': | ||
return new DateTime($data); | ||
default: | ||
throw new RuntimeException(sprintf("Type %s is unsupported", $type)); | ||
} | ||
|
||
// If we end up here, just return data | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,58 +15,145 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace {{servicePackage}}; | ||
|
||
use JMS\Serializer\Context; | ||
use JMS\Serializer\JsonDeserializationVisitor; | ||
use JMS\Serializer\GraphNavigatorInterface; | ||
use JMS\Serializer\Metadata\ClassMetadata; | ||
use JMS\Serializer\Metadata\PropertyMetadata; | ||
use JMS\Serializer\Visitor\DeserializationVisitorInterface; | ||
|
||
class StrictJsonDeserializationVisitor extends JsonDeserializationVisitor | ||
class StrictJsonDeserializationVisitor implements DeserializationVisitorInterface | ||
{ | ||
protected $jsonDeserializationVisitor; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. type missing -> |
||
|
||
public function __construct( | ||
int $options = 0, | ||
int $depth = 512 | ||
) { | ||
$this->jsonDeserializationVisitor = new JsonDeserializationVisitor($options, $depth); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function visitString($data, array $type, Context $context) | ||
public function visitNull($data, array $type) | ||
{ | ||
return $this->jsonDeserializationVisitor->visitNull($data, $type); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function visitString($data, array $type): string | ||
{ | ||
if (!is_string($data)) { | ||
throw TypeMismatchException::fromValue('string', $data, $context); | ||
throw TypeMismatchException::fromValue('string', $data); | ||
} | ||
|
||
return parent::visitString($data, $type, $context); | ||
return $this->jsonDeserializationVisitor->visitString($data, $type); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function visitBoolean($data, array $type, Context $context) | ||
public function visitBoolean($data, array $type): bool | ||
{ | ||
if (!is_bool($data)) { | ||
throw TypeMismatchException::fromValue('boolean', $data, $context); | ||
throw TypeMismatchException::fromValue('boolean', $data); | ||
} | ||
|
||
return parent::visitBoolean($data, $type, $context); | ||
return $this->jsonDeserializationVisitor->visitBoolean($data, $type); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function visitInteger($data, array $type, Context $context) | ||
public function visitInteger($data, array $type): int | ||
{ | ||
if (!is_int($data)) { | ||
throw TypeMismatchException::fromValue('integer', $data, $context); | ||
throw TypeMismatchException::fromValue('integer', $data); | ||
} | ||
|
||
return parent::visitInteger($data, $type, $context); | ||
return $this->jsonDeserializationVisitor->visitInteger($data, $type); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function visitDouble($data, array $type, Context $context) | ||
public function visitDouble($data, array $type): float | ||
{ | ||
if (!is_float($data) && !is_integer($data)) { | ||
throw TypeMismatchException::fromValue('double', $data, $context); | ||
throw TypeMismatchException::fromValue('double', $data); | ||
} | ||
|
||
return parent::visitDouble($data, $type, $context); | ||
return $this->jsonDeserializationVisitor->visitDouble($data, $type); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function visitArray($data, array $type): array | ||
{ | ||
return $this->jsonDeserializationVisitor->visitArray($data, $type); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function visitDiscriminatorMapProperty($data, ClassMetadata $metadata): string | ||
{ | ||
return $this->jsonDeserializationVisitor->visitDiscriminatorMapProperty($data, $type); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
i guess you meant to use |
||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function startVisitingObject(ClassMetadata $metadata, object $object, array $type): void | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2nd param changed from |
||
{ | ||
$this->jsonDeserializationVisitor->startVisitingObject($metadata, $object, $type); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function visitProperty(PropertyMetadata $metadata, $data) | ||
{ | ||
return $this->jsonDeserializationVisitor->visitProperty($metadata, $data); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function endVisitingObject(ClassMetadata $metadata, $data, array $type): object | ||
{ | ||
return $this->jsonDeserializationVisitor->endVisitingObject($metadata, $data, $type); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getResult($data) | ||
{ | ||
return $this->jsonDeserializationVisitor->getResult($data); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function prepare($data) | ||
{ | ||
return $this->jsonDeserializationVisitor->prepare($data); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setNavigator(GraphNavigatorInterface $navigator): void | ||
{ | ||
$this->jsonDeserializationVisitor->setNavigator($navigator); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be breaking to keep also >= 7.4 supported?
>=7.4
>=7.4.0|>=8.0.2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The thing is: symfony6 only supports
php>=8.0.2
.But now that I think about it, I can currently not see an obvious reason why symfony5 should not work.
I'll try it in the next days.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There were no conflicts. So it works with symfony 5.
I'm not sure why I decided it needs to target symfony6 only in the first place.