doctrine / orm

Doctrine Object Relational Mapper (ORM)
https://www.doctrine-project.org/projects/orm.html
MIT License
9.93k stars 2.51k forks source link

Use FQCN in type atribute in XML mapping for field and id #10627

Closed tmihalicka closed 1 year ago

tmihalicka commented 1 year ago

Bug Report

We'are using ValueObjects these value objects are mapped as doctrine types for example configuration from Symfony below:

doctrine:
  dbal:
    default_connection: default
    types:
       App\Project\Domain\Model\Project\ValueObject\ProjectId: App\Project\Infrastructure\Persistence\Doctrine\CustomTypes\Project\RamseyProjectIdType
     ... 

For entities mapping we're using XML files. After upgrade to doctrine/orm to version 2.14.2. In this release XSD schema validation was enabled. And now we're getting following exception:

libxml error: Element '{[http://doctrine-project.org/schemas/orm/doctrine-mapping}id](http://doctrine-project.org/schemas/orm/doctrine-mapping%7Did)', attribute 'type': App\Project\Domain\Model\Project\ValueObject\ProjectId' is not a valid value of the atomic type 'xs:NMTOKEN'.

Doctrine ORM XSD schema already contains fqcn type

  <xs:simpleType name="fqcn" id="fqcn">
    <xs:restriction base="xs:token">
      <xs:pattern value="[a-zA-Z_u01-uff][a-zA-Z0-9_u01-uff]+" id="fqcn.pattern">
      </xs:pattern>
    </xs:restriction>
  </xs:simpleType>

Why not use this type for type attribute for field and id?

Q A
BC Break no
Version 2.14.3

Summary

Use FQCN in type attribute in XML mapping for field and id

Current behavior

Exception is thrown when type attribute contains FQCN

How to reproduce

Just insert any FQCN for type attribute for field and id and exception in XML validation is thrown:

libxml error: Element '{[http://doctrine-project.org/schemas/orm/doctrine-mapping}id](http://doctrine-project.org/schemas/orm/doctrine-mapping%7Did)', attribute 'type': App\Project\Domain\Model\Project\ValueObject\ProjectId' is not a valid value of the atomic type 'xs:NMTOKEN'.

Expected behavior

Ability to use FQCN in type attribute in XML mapping for field and id

greg0ire commented 1 year ago

Please provide a failing mapping, or even better, a test case based on it. I'm wondering if you're getting the XSD file from doctrine-project.org, or from your vendor directory.

The XSD file hosted on doctrine-project.org does not have a version number, so I wouldn't use it.

tmihalicka commented 1 year ago

Sure here is used mapping:

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="App\Project\Domain\Model\Project\Project" table="project_project">
    <id name="projectId" type="App\Project\Domain\Model\Project\ValueObject\ProjectId" column="id"/>
    <one-to-many field="environments" target-entity="App\Project\Domain\Model\Environment\Environment" mapped-by="project"/>
    <field name="name" type="App\Project\Domain\Model\Project\ValueObject\Name"/>
    <field name="logo" type="App\Project\Domain\Model\Project\ValueObject\Logo" nullable="true"/>
    <field name="code" type="App\Project\Domain\Model\Project\ValueObject\Code" unique="true"/>
  </entity>
</doctrine-mapping>

i'm getting expcetion for example:

The value 'App\Project\Domain\Model\Project\ValueObject\Name' of attribute 'type' on element 'field' is not valid with respect to its type, 'NMTOKEN'.
greg0ire commented 1 year ago

Can you try this and let me know what happens:

 <?xml version="1.0" encoding="utf-8"?>
 <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
+                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping vendor/doctrine/orm/doctrine-mapping.xsd">
   <entity name="App\Project\Domain\Model\Project\Project" table="project_project">
     <id name="projectId" type="App\Project\Domain\Model\Project\ValueObject\ProjectId" column="id"/>
     <one-to-many field="environments" target-entity="App\Project\Domain\Model\Environment\Environment" mapped-by="project"/>
     <field name="name" type="App\Project\Domain\Model\Project\ValueObject\Name"/>
     <field name="logo" type="App\Project\Domain\Model\Project\ValueObject\Logo" nullable="true"/>
     <field name="code" type="App\Project\Domain\Model\Project\ValueObject\Code" unique="true"/>
   </entity>
 </doctrine-mapping>
tmihalicka commented 1 year ago

Thank you but issue is still present after changing XSD schema location. Schema in vendor/doctrine/orm/doctrine-mapping.xsd still have following type for attribute type in field:

<xs:attribute name="type" type="xs:NMTOKEN" default="string" />

this is also in attribute type in id as well.

I believe solution is change attribute type from xs:NMTOKEN to orm:fqcn or maybe for some new type which is compatible with FQCN or basic string.

greg0ire commented 1 year ago

Ah right. Can you please send a PR doing that, along with a test case that fails without your change?

tmihalicka commented 1 year ago

Fix is provided in pull request above.

tmihalicka commented 1 year ago

@greg0ire one question when we can expect release of version 2.14.3?