Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-4481

Filter / UpdateFilter does not check nested properties in optional objects

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 4.9.0
    • Affects Version/s: 4.8.1
    • Component/s: TypeScript
    • Labels:
    • 0
    • Not Needed

      What problem are you facing?

      Both `Filter` and `UpdateFilter` types check top-level optional (nullable) properties, but they don't check nested properties inside a top-level optional (nullable) object property.

      What driver and relevant dependency versions are you using?

      • mongodb v4.8.1
      • typescript v4.7.4

      Steps to reproduce?

      Unable to find source-code formatter for language: typescript. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      import { Filter, UpdateFilter } from 'mongodb';
      
      interface Foo {
        optional?: string;
        optionalNested?: {
          path: string;
        };
        required: string;
        requiredNested: {
          path: string;
        };
      }
      
      const queryOptionalOK: Filter<Foo> = { optional: "foo", "optionalNested.path": "foo" };
      // ^ OK
      const queryOptionalFail: Filter<Foo> = { optional: 1, "optionalNested.path": 1 };
      // ^ Fail - TS flags only `optional`, but `optionalNested.path` should also be flagged
      const queryRequiredOK: Filter<Foo> = { required: "foo", "requiredNested.path": "foo" };
      // ^ OK
      const queryRequiredFail: Filter<Foo> = { required: 1, "requiredNested.path": 1 };
      // ^ OK - Both fields are flagged
      
      const updateOptionalOK: UpdateFilter<Foo> = { $set: { optional: "foo", "optionalNested.path": "foo" } };
      // ^ OK - Expected
      const updateOptionalFail: UpdateFilter<Foo> = { $set: { optional: 1, "optionalNested.path": 1 } };
      // ^ Fail - TS flags only `optional`, but `optionalNested.path` should also be flagged
      const updateRequiredOK: UpdateFilter<Foo> = { $set: { required: "foo", "requiredNested.path": "foo" } };
      // ^ OK - Expected
      const updateRequiredFail: UpdateFilter<Foo> = { $set: { required: 1, "requiredNested.path": 1 } };
      // ^ OK - Both fields are flagged
      

      TS Playground

            Assignee:
            neal.beeken@mongodb.com Neal Beeken
            Reporter:
            avaly@plexapp.com Valentin Agachi
            Votes:
            2 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: