Skip to content
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

setGlobalOptions must be called before usages of @modelOptions() #939

Open
richard-jfc opened this issue May 10, 2024 · 1 comment
Open
Labels
breaking This PR is a breaking change | This Issue to fix would be a breaking change bug Something isn't working
Milestone

Comments

@richard-jfc
Copy link

Versions

  • System: linux
  • NodeJS: 18.19.0
  • Typescript: 5.3.3
  • Compiler / Transpiler: ts-jest
  • Typegoose(NPM): 12.4.0
  • mongoose: 8.3.4
  • mongodb: 6.0.14

What is the Problem?

Classes that use @modelOptions() are not respecting setGlobalOptions. This is particular unobvious when the class only uses @modelOptions because it extends TimeStamps.

Code Example

import {
  buildSchema,
  prop,
  setGlobalOptions,
  Severity,
} from '@typegoose/typegoose';
import { TimeStamps } from '@typegoose/typegoose/lib/defaultClasses';

setGlobalOptions({ options: { allowMixed: Severity.ERROR } });

class User extends TimeStamps {
  @prop()
  username: string | undefined; // This will result in a Mixed type
}

buildSchema(User); // Only logs a warning, should throw an error

Do you know why it happens?

@modelOptions() attempts to apply the global options immediately, it doesn't wait for buildSchema to be called. Importing TimeStamps calls @modelOptions(), which happens before the setGlobalOptions call.

Expected Behavior

global options should be applied when buildSchema is called, not when @modelOptions is called.


Work around

Relies on imports loading in a specific order

// globalTypegooseOptions.ts
import { setGlobalOptions, Severity } from '@typegoose/typegoose';

setGlobalOptions({ options: { allowMixed: Severity.ERROR } });

//---
// main.ts
import './globalTypegooseOptions';
import { buildSchema, prop } from '@typegoose/typegoose';
import { TimeStamps } from '@typegoose/typegoose/lib/defaultClasses';

class User extends TimeStamps {
  @prop()
  username: string | undefined; // This will result in a Mixed type
}

buildSchema(User); // Correctly throws an error
@richard-jfc richard-jfc added the bug Something isn't working label May 10, 2024
@hasezoey hasezoey added the breaking This PR is a breaking change | This Issue to fix would be a breaking change label May 11, 2024
@hasezoey
Copy link
Member

hasezoey commented May 11, 2024

yes, @modelOptions currently directly merges on decorator call (and on first call applies global options).

the only fix to this i can image is re-working how modelOptions works by storing all arguments in a array and on buildSchema apply / merge them in-order, which i would consider it being a breaking change to how it currently works.

workaround for now as pointed out is to put the setGlobalOptions before any class-related imports.

@hasezoey hasezoey added this to the 13.0 milestone May 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking This PR is a breaking change | This Issue to fix would be a breaking change bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants