doctrine / mongodb-odm

The Official PHP MongoDB ORM/ODM
https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/
MIT License
1.09k stars 502 forks source link

PartialFilterExpression with value set to null creates an index with value set to the string "null" #2254

Closed GregRbs92 closed 3 years ago

GregRbs92 commented 3 years ago

Bug Report

Q A
BC Break yes/no
Version any

Summary

I wanted to create an index with a partial filter expression to index only the documents having a specific field set to null. Unfortunately, when using the XML mapping, the values are cast as string, therefore the index is created for the documents having the specific field set to the string "null".

Current behavior

Currently, only booleans and numbers are handled as specific types. https://github.com/doctrine/mongodb-odm/blob/3bdfe97be472160ebbc924d5882ce61bd640a515/lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php#L525-L531

How to reproduce

<index>
    <key name="parent" />
    <partial-filter-expression>
        <field name="parent" operator="eq" value="null" />
    </partial-filter-expression>
</index>

Expected behavior

Adding an exception for null values might be useful.

if ($value === 'true') { 
    $value = true; 
} elseif ($value === 'false') { 
    $value = false; 
} elseif (is_numeric($value)) { 
    $value = preg_match('/^[-]?\d+$/', $value) ? (int) $value : (float) $value; 
} elseif (strtolower($value) === 'null') {
    $value = null;
}
malarzm commented 3 years ago

This sounds like a reasonable thing to do. What you like to implement the change @GregRbs92?

malarzm commented 3 years ago

Although to be on par with true/false handling I'd give up on the strtolower call

GregRbs92 commented 3 years ago

@malarzm With pleasure, do you have any contribution guide I can refer to for the development?

malarzm commented 3 years ago

@GregRbs92 depends what you're looking for :) but bare minimum is to add a test along with the change, so we won't break added behaviour accidentally. If you don't have MongoDB installed locally you can count on running tests through GitHub actions, that's happening for each commit. Also we have automated checks for coding standards and static analysis, so if there's anything off with our standards, the checks will let you know. So summing up: