Skip to content

Make XSD more strict for other Document types #2766

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

Open
wants to merge 2 commits into
base: 2.12.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 61 additions & 6 deletions doctrine-mongo-mapping.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,80 @@
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="document" type="odm:document" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="embedded-document" type="odm:document" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="mapped-superclass" type="odm:document" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="query-result-document" type="odm:document" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="embedded-document" type="odm:embedded-document" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="mapped-superclass" type="odm:mapped-superclass" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="query-result-document" type="odm:query-result-document" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="view" type="odm:view" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="gridfs-file" type="odm:gridfs-file" minOccurs="0" maxOccurs="unbounded" />
</xs:choice>
</xs:complexType>
</xs:element>

<xs:complexType name="embedded-document">
<xs:attribute name="name" type="xs:NMTOKEN" />
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="id" type="odm:id" minOccurs="0" />
<xs:element name="field" type="odm:field" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="embed-one" type="odm:embed-one" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="embed-many" type="odm:embed-many" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="reference-one" type="odm:reference-one" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="reference-many" type="odm:reference-many" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="discriminator-field" type="odm:discriminator-field" minOccurs="0" />
<xs:element name="discriminator-map" type="odm:discriminator-map" minOccurs="0" />
<xs:element name="default-discriminator-value" type="odm:default-discriminator-value" minOccurs="0" />
<xs:element name="lifecycle-callbacks" type="odm:lifecycle-callbacks" minOccurs="0" />
<xs:element name="also-load-methods" type="odm:also-load-methods" minOccurs="0" />
<xs:element name="indexes" type="odm:indexes" minOccurs="0" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd have to confirm in tests, but IIRC embedded documents can be discriminated and also have lifecycle callbacks, so those elements should be added.

</xs:choice>

<xs:attribute name="name" type="xs:string" />
<xs:attribute name="inheritance-type" type="odm:inheritance-type" />
</xs:complexType>

<xs:complexType name="mapped-superclass">
<xs:attribute name="name" type="xs:NMTOKEN" />
<xs:choice minOccurs="0" maxOccurs="unbounded">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For mapped superclasses, we should check the inheritance logic in the mapping driver to confirm what elements are allowed. IIRC, attributes like the write concern, capped collection information, etc. is all inherited to documents, so we may want to add it here as well.

<xs:element name="id" type="odm:id" minOccurs="0" />
<xs:element name="field" type="odm:field" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="embed-one" type="odm:embed-one" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="embed-many" type="odm:embed-many" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="reference-one" type="odm:reference-one" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="reference-many" type="odm:reference-many" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="discriminator-field" type="odm:discriminator-field" minOccurs="0" />
<xs:element name="discriminator-map" type="odm:discriminator-map" minOccurs="0" />
<xs:element name="default-discriminator-value" type="odm:default-discriminator-value" minOccurs="0" />
<xs:element name="lifecycle-callbacks" type="odm:lifecycle-callbacks" minOccurs="0" />
<xs:element name="also-load-methods" type="odm:also-load-methods" minOccurs="0" />
<xs:element name="indexes" type="odm:indexes" minOccurs="0" />
</xs:choice>

<xs:attribute name="db" type="xs:NMTOKEN" />
<xs:attribute name="name" type="xs:string" />
<xs:attribute name="write-concern" type="xs:string" />
<xs:attribute name="collection" type="xs:NMTOKEN" />
<xs:attribute name="capped-collection" type="xs:boolean" />
<xs:attribute name="capped-collection-size" type="xs:integer" />
<xs:attribute name="capped-collection-max" type="xs:integer" />
<xs:attribute name="repository-class" type="xs:string" />
<xs:attribute name="inheritance-type" type="odm:inheritance-type" />
<xs:attribute name="change-tracking-policy" type="odm:change-tracking-policy" />
<xs:attribute name="read-only" type="xs:boolean" />
</xs:complexType>

<xs:complexType name="query-result-document">
<xs:attribute name="name" type="xs:NMTOKEN" />
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="id" type="odm:id" minOccurs="0" />
<xs:element name="field" type="odm:field" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="embed-one" type="odm:embed-one" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="embed-many" type="odm:embed-many" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="reference-one" type="odm:reference-one" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="reference-many" type="odm:reference-many" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="discriminator-field" type="odm:discriminator-field" minOccurs="0" />
<xs:element name="discriminator-map" type="odm:discriminator-map" minOccurs="0" />
<xs:element name="default-discriminator-value" type="odm:default-discriminator-value" minOccurs="0" />
<xs:element name="lifecycle-callbacks" type="odm:lifecycle-callbacks" minOccurs="0" />
<xs:element name="also-load-methods" type="odm:also-load-methods" minOccurs="0" />
</xs:choice>

<xs:attribute name="name" type="xs:string" />
</xs:complexType>

<xs:complexType name="view">
Expand Down
11 changes: 11 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Mapping/Driver/XmlDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use MongoDB\BSON\Document;
use TestDocuments\AlsoLoadDocument;
use TestDocuments\CustomIdGenerator;
use TestDocuments\InvalidEmbeddedDocument;
use TestDocuments\InvalidPartialFilterDocument;
use TestDocuments\SchemaInvalidDocument;
use TestDocuments\SchemaValidatedDocument;
Expand Down Expand Up @@ -75,6 +76,16 @@ public function testInvalidPartialFilterExpressions(): void
$this->driver->loadMetadataForClass(InvalidPartialFilterDocument::class, $classMetadata);
}

public function testInvalidEmbeddedDocument(): void
{
$classMetadata = new ClassMetadata(InvalidEmbeddedDocument::class);

$this->expectException(MappingException::class);
$this->expectExceptionMessageMatches('#The mapping file .+ is invalid#');

$this->driver->loadMetadataForClass(InvalidEmbeddedDocument::class, $classMetadata);
}

public function testWildcardIndexName(): void
{
$classMetadata = new ClassMetadata(WildcardIndexDocument::class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace TestDocuments;

class InvalidEmbeddedDocument
{
public string $id;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>

<doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">

<embedded-document name="TestDocuments\InvalidEmbeddedDocument" collection="embeddedDocument">
<id />
</embedded-document>
</doctrine-mongo-mapping>