question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Model.translateAlias doesn't iterate through sub-Schemas if the key wasn't an alias

See original GitHub issue

Do you want to request a feature or report a bug? Bug

What is the current behavior? When using Model.translateAlias, if you have a not aliased key that refers to a sub-schema, it will not be picked up to translate keys further along in the key

If the current behavior is a bug, please provide the steps to reproduce.

  • Create a FirstSchema, where the top level field does not have an alias and refers to a SubSchema
  • Inside the SubSchema, use aliases
  • Perform a model(FirstSchema).translateAlias() and see that the SubSchema keys are not translated

The current translate function uses this iterative loop

// in model.js, Model.translateAlias()
for (const i in fieldKeys) {
      const name = fieldKeys[i];
      if (currentSchema && currentSchema.aliases[name]) {
        alias = currentSchema.aliases[name];
        // Alias found,
        translated.push(alias);
      } else {
        // Alias not found, so treat as un-aliased key
        // This is where this problem is, since we do not push name to alias
        translated.push(name);
      }

      // Check if aliased path is a schema
      // If the key wasn't aliased, we have no way to find the subschema
      if (currentSchema && currentSchema.paths[alias]) {
        currentSchema = currentSchema.paths[alias].schema;
      }
      else
        currentSchema = null;
    }

What is the expected behavior? I propose a one line fix

for (const i in fieldKeys) {
      const name = fieldKeys[i];
      if (currentSchema && currentSchema.aliases[name]) {
        alias = currentSchema.aliases[name];
        // Alias found,
        translated.push(alias);
      } else {
        // Alias not found, so treat as un-aliased key
        // Here
        alias = name;
        translated.push(name);
      }

      // Check if aliased path is a schema
      if (currentSchema && currentSchema.paths[alias]) {
        currentSchema = currentSchema.paths[alias].schema;
      }
      else
        currentSchema = null;
    }

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
IslandRhythmscommented, Sep 23, 2021
const mongoose = require('mongoose');

const Syntax = new mongoose.Schema({
    s: { type: String, alias: 'syntax' }
  });

  const Character = mongoose.model('Character', new mongoose.Schema({
    dot: { type: Syntax },
    name: { type: String, alias: '名' },
    bio: {
      age: { type: Number, alias: '年齢' }
    }
  }));

  async function run() {
    await mongoose.connect('mongodb://localhost:27017/', {useNewUrlParser: true,
    useUnifiedTopology: true,});
    await mongoose.connection.dropDatabase();

    let test = await Character.translateAliases({
        _id: '1',
        名: 'Stark',
        年齢: 30,
        'dot.syntax': 'DotSyntax',
        $and: [{ $or: [{ 名: 'Stark' }, { 年齢: 30 }] }, { 'dot.syntax': 'DotSyntax' }]
    });
    console.log(test);
    console.log(test.$and[0].$or);
  }

  run();
0reactions
frisbee09commented, Sep 23, 2021

So to clarify, the dot.syntax should be dot.s? I ran the script and everything else looks fine

Yeah, the Syntax schema has an alias on it that should be observed regardless of where it is on a parent schema’s definition. The test case I gave is a direct edit of the test case that already exists in model.translateAliases.test.js to remove the alias of d to dot. This then prevents the syntax > s alias from resolving.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Solved: SQL Server - "unable to translate alias" when I sa...
I checked the existing data connections and found one in the list that matched the server\database my locally created alias was using, so...
Read more >
Is there an inverse of the mongoose model.translateAliases ...
In my case aggregate project would not work as I am dynamically ... toObject({ virtuals: true }) which should return the alias keys...
Read more >
draft-ietf-ldapbis-protocol-31.txt
Protocol Model The general model adopted by this protocol is one of clients ... to locate the named object, it SHALL NOT perform...
Read more >
common information model (cim) infrastructure specification
The DMTF Common Information Model (CIM) Infrastructure is an approach to the management of systems and networks that applies the basic ...
Read more >
History - Apache Calcite
New behavior: The Project operator is not transformed and the rule becomes NOOP. Compatibility: This release is tested on Linux, macOS, Microsoft Windows; ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found