JMSSerializerBundle icon indicating copy to clipboard operation
JMSSerializerBundle copied to clipboard

The format_output option is not working ?

Open NicolasFz opened this issue 5 years ago • 4 comments

I wanted to remove the indentation from my XML output, but the format_output option doesn't seem to work. The other options seem to work well.

My config/packages/jms_serializer.yaml file :

jms_serializer:
    visitors:
        xml_serialization:
            format_output: false
    property_naming:
        id: 'jms_serializer.identical_property_naming_strategy'

My jms bundle version :

jms/serializer:3.6.0  
jms/serializer-bundle :3.5.0

Is that a bug or did I miss someting on the configuration documentation ?

NicolasFz avatar Apr 03 '20 12:04 NicolasFz

can you please send a failing test case?

goetas avatar Apr 04 '20 09:04 goetas

The class I want to serialize:

<?php

namespace App\DTO\Connect;

use JMS\Serializer\Annotation as Serializer;

/**
 * @Serializer\XmlRoot("xml")
 */
class testOutputDTO {
	/**
	 * @Serializer\XmlElement(cdata=false)
	 * @Serializer\Type("string")
	 */
	public $firstname;
	
	/**
	 * @Serializer\XmlElement(cdata=false)
	 * @Serializer\Type("string")
	 */
	public $lastname;
	
	/**
	 * @Serializer\XmlElement(cdata=false)
	 * @Serializer\Type("string")
	 */
	public $email;
	
	public function hydrateDTO($firstname, $lastname, $email) {
		$this->firstname = $firstname;
		$this->lastname = $lastname;
		$this->email = $email;
		return $this;
	}
	
}

My test controller :

	/**
	 *  @Route("test-serialize", name="test_serialize")
	 */
	public function  testSerialize(SerializerInterface $serializer){
		
		$expeditionDTO = new testOutputDTO();
		$expeditionDTO->hydrateDTO("John","Doe","[email protected]");
		$xml = $serializer->serialize($expeditionDTO, 'xml');
		dump($xml);
		return new Response();
	}

The dump output :

"""
<?xml version="1.0" encoding="UTF-8"?>
<xml>
  <firstname>John</firstname>
  <lastname>Doe</lastname>
  <email>[email protected]</email>
</xml>
"""

I hope that's enough, thanks.

NicolasFz avatar Apr 06 '20 07:04 NicolasFz

Are you sure that you don't have in the packages dev folder another jms_serializer.yml with the settings to true?

goetas avatar Apr 06 '20 19:04 goetas

In JMSSerializerExtension has line if (!empty($config['visitors']['xml_serialization']['format_output'])) { and if format_output is false, !empty return false, condition will not work, format_output will remain with the default true.

andreydubinin avatar Jan 11 '22 10:01 andreydubinin