Skip to content

Commit 4cc52c2

Browse files
ikallaliGromNaN
andauthored
Fix: Cast numeric write-concern values from XML mapping to integers (#2750)
When using write-concern="1" in XML mapping, the value was being cast to string, causing MongoDB to interpret it as a named write concern mode instead of a numeric level. This led to errors like: "No write concern mode named '1' found in replica set configuration". This commit ensures that numeric write concern values are properly cast to integers, while still allowing named modes such as "majority" to be used as strings. --------- Co-authored-by: Jérôme Tamarelle <[email protected]>
1 parent 44a611c commit 4cc52c2

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,12 @@ public function loadMetadataForClass($className, \Doctrine\Persistence\Mapping\C
158158
}
159159

160160
if (isset($xmlRoot['write-concern'])) {
161-
$metadata->setWriteConcern((string) $xmlRoot['write-concern']);
161+
$writeConcern = (string) $xmlRoot['write-concern'];
162+
if (is_numeric($writeConcern)) {
163+
$writeConcern = (int) $writeConcern;
164+
}
165+
166+
$metadata->setWriteConcern($writeConcern);
162167
}
163168

164169
if (isset($xmlRoot['inheritance-type'])) {

tests/Doctrine/ODM/MongoDB/Tests/Mapping/AbstractAnnotationDriverTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function testEmbeddedClassCantHaveShardKey(): void
148148
public function testDocumentAnnotationCanSpecifyWriteConcern(): void
149149
{
150150
$cm = $this->dm->getClassMetadata(AnnotationDriverTestWriteConcernMajority::class);
151-
self::assertEquals('majority', $cm->writeConcern);
151+
self::assertSame('majority', $cm->writeConcern);
152152

153153
$cm = $this->dm->getClassMetadata(AnnotationDriverTestWriteConcernUnacknowledged::class);
154154
self::assertSame(0, $cm->writeConcern);

tests/Doctrine/ODM/MongoDB/Tests/Mapping/AbstractMappingDriverTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function testDocumentLevelReadPreference(ClassMetadata $class): ClassMeta
101101
#[Depends('testDocumentCollectionNameAndInheritance')]
102102
public function testDocumentLevelWriteConcern(ClassMetadata $class): ClassMetadata
103103
{
104-
self::assertEquals(1, $class->getWriteConcern());
104+
self::assertSame(1, $class->getWriteConcern());
105105

106106
return $class;
107107
}

0 commit comments

Comments
 (0)