goetas-webservices / xsd2php

Convert XSD into PHP classes and JMS serializer definitions
MIT License
238 stars 93 forks source link

CDATA sections not being disabled in XML despite setting xml_cdata to false in goetas-webservices/xsd2php #155

Open NathanaelSantos opened 1 year ago

NathanaelSantos commented 1 year ago
"require": {
    "goetas-webservices/xsd2php-runtime": "^0.2.16"
  },
"require-dev": {
    "goetas-webservices/xsd2php": "^0.4.7"
  }
idEscola:
            expose: true
            access_type: public_method
            serialized_name: idEscola
            xml_element:
                cdata: false
                namespace: 'http://www.tce.se.gov.br/sagres2023/xml/sagresEdu'
            accessor:
                getter: getIdEscola
                setter: setIdEscola
            type: int

In my config.yml:

  configs_jms:  #optional
    xml_cdata: false # Disables CDATA

The generated XML:

<entry>
      <id_escola><![CDATA[28022386]]></id_escola>
      <turma/>
      <diretor>
        <cpf_diretor><![CDATA[39353893534]]></cpf_diretor>
        <nr_ato><![CDATA[]]></nr_ato>
      </diretor>
      <cardapio/>
</entry>

It appears that despite setting xml_cdata to false, the XML still contains CDATA sections. This could be a bug in the library "goetas-webservices/xsd2php" ?

fklee commented 1 year ago

I found another solution : in goetas-webservices/xsd2php/src/DependencyInjection/Configuration.php

replace this code segment :

                ->arrayNode('configs_jms')
                    ->addDefaultsIfNotSet()
                    ->children()
                        ->booleanNode('xml_cdata')
                            ->defaultTrue()
                        ->end()
                    ->end()
                ->end()

to :

                ->arrayNode('configs_jms')
                    ->addDefaultsIfNotSet()
                    ->children()
                        ->booleanNode('xml_cdata')
                            ->defaultFalse()
                        ->end()
                    ->end()
                ->end()

As change default from true to false.

nathanipti commented 1 year ago

Vlw 👍 fklee

I found a solution here: https://jmsyst.com/libs/serializer/master/reference/annotations

use JMS\Serializer\Annotation\Type;
use JMS\Serializer\Annotation\SerializedName;
use JMS\Serializer\Annotation\XmlElement;

class MyClass
{
      /**
       * @var \DateTime $data
       * @Type("DateTime<'Y-m-d'>")
       * @XmlElement(cdata=false)
       */
      private $data = null;
}