JMS Serializer 3.3独立版:子类父类属性YAML映射配置问题
Hey there! Let's figure out how to fix this inheritance mapping issue with JMS Serializer 3.3. The core problem here is that when your subclass (like ApiRequest) inherits a property from the parent Request class, the serializer isn't automatically pulling in the parent's property metadata—especially since you're using YAML mappings for the subclass and only annotations for the parent.
Solution 1: Create a Parent Class YAML Mapping (Recommended)
The cleanest approach is to define a base YAML configuration for your parent Request class, then have your subclasses extend this configuration. This ensures consistent metadata inheritance and avoids hacking the driver code.
- Create the parent class YAML file (
CDB.Collection.DTO.Request.yml):
CDB\Collection\DTO\Request: exclusion_policy: ALL properties: signatureType: expose: true type: string # Keep base config here; subclasses can override serialized_name if needed
- Update your subclass YAML file (
CDB.Collection.DTO.ApiRequest.yml) to extend the parent config:
CDB\Collection\DTO\ApiRequest: extends: CDB\Collection\DTO\Request properties: signatureType: serialized_name: signatureType # Override the serialized name for this subclass
This way, the serializer loads the parent's property metadata first, then applies the subclass-specific overrides. Your FormRequest can follow the same pattern with its own serialized_name value.
Solution 2: Explicitly Declare Parent Properties in Subclass YAML (No Parent Config)
If you don't want to create a separate config for the parent class, you can explicitly reference the parent property in the subclass YAML, making sure to clarify the property path (though public properties usually don't need this, it helps the serializer resolve the parent property correctly):
CDB\Collection\DTO\ApiRequest: exclusion_policy: ALL properties: signatureType: expose: true type: string serialized_name: signatureType property_path: signatureType # Explicitly point to the parent class property
Critical Configuration Check: Enable Metadata Inheritance
Make sure your JMS Serializer configuration is set up to load metadata from parent classes. If you're using Symfony, check your config/packages/jms_serializer.yaml file:
jms_serializer: metadata: auto_detection: true directories: YourDTOs: namespace_prefix: "CDB\\Collection\\DTO" path: "%kernel.project_dir%/config/serializer" # Path to your YAML files # If you're using annotations on the parent class, enable the annotation driver too drivers: doctrine_annotation: ~ yaml: ~
Enabling both the annotation and YAML drivers ensures the serializer reads the parent's validation annotations (like @Assert\NotBlank()) and combines them with the subclass's YAML mappings.
Why Your Original Setup Failed
The issue occurred because the YAML driver was only processing properties directly declared on the subclass (ApiRequest), not inherited from Request. When you modified YamlDriver.php, you forced it to include parent class properties, but that's not a sustainable fix. The proper way is to use the serializer's built-in inheritance features (like extends in YAML) or explicitly declare parent properties in the subclass config.
内容的提问来源于stack exchange,提问作者shuba.ivan

