Skip to content

Discriminator validation issue#10655

Merged
dhmlau merged 7 commits intoloopbackio:masterfrom
pnoebauer:discriminator-validation-issue
Jun 13, 2025
Merged

Discriminator validation issue#10655
dhmlau merged 7 commits intoloopbackio:masterfrom
pnoebauer:discriminator-validation-issue

Conversation

@pnoebauer
Copy link
Contributor

High-Level Description

This pull request illustrates a bug in the LoopBack framework causing discriminators to not work. The issue is due to LoopBack-Rest invoking the openapi-schema-to-json-schema library function incorrectly under loopback-next/packages/rest/src/validation/request-body.validator.ts, which results in the removal of OpenAPI properties such as discriminator.

Changes Made

Bug Illustration:
The bug is located in loopback-next/packages/rest/src/validation/request-body.validator.ts#L87.
Added comments and TODO notes in the pr to highlight the issue.
Demonstrated the bug in the /examples/validation-app by adding a controller, models, and tests.
Code Changes:
In loopback-next/packages/rest/src/validation/request-body.validator.ts:

function convertToJsonSchema(openapiSchema: SchemaObject) {
  /*
    loopback does not pass the options arg to function toJsonSchema(schema, options?),
    which is part of the openapi-schema-to-json-schema library;
    by default toJsonSchema() removes discriminators, which is why they do not work
    in loopback
  */
  // TODO: fix below bug; toJsonSchema requires the options object to enable discriminators
  const jsonSchema = toJsonSchema(openapiSchema);
  // NOTE: replacing above line with below fixes discriminators not working in loopback and all tests pass
  // const jsonSchema = toJsonSchema(openapiSchema, {
  //   keepNotSupported: ['discriminator'],
  // });
}

Example Application:
Created an example application under /examples/validation-app to reproduce the issue.
Added a controller and models to showcase the problem.
Included tests that demonstrate the expected error messages if discriminators worked correctly.

…t-body-validator

Signed-off-by: Philip Noebauer <philip.noebauer@gmail.com>
Signed-off-by: Philip Noebauer <philip.noebauer@gmail.com>
@aaqilniz
Copy link
Contributor

Hi, @pnoebauer. Can you please uncomment the actual change we need to fix the issue?

Signed-off-by: Philip Noebauer <philip.noebauer@gmail.com>
@pnoebauer
Copy link
Contributor Author

Hi, @pnoebauer. Can you please uncomment the actual change we need to fix the issue?

Hi, @pnoebauer. Can you please uncomment the actual change we need to fix the issue?

Hi, sure! I have just pushed another commit with the comments removed and a possible bug fix.

@pnoebauer
Copy link
Contributor Author

@aaqilniz @rmg @klassicd @dhmlau could you please review this pr

please advise if any changes are needed

Copy link
Member

@dhmlau dhmlau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pnoebauer, thanks for the PR. Sorry about the delay. I have a few minor comments. Once it's addressed, it would be good to go. Thanks!

@dhmlau
Copy link
Member

dhmlau commented Jun 12, 2025

@pnoebauer, after you've incorporated the feedback, could you please merge the commits? Thanks.

Signed-off-by: Philip Noebauer <philip.noebauer@gmail.com>
@pnoebauer
Copy link
Contributor Author

@pnoebauer, thanks for the PR. Sorry about the delay. I have a few minor comments. Once it's addressed, it would be good to go. Thanks!

@dhmlau I made requested changes
could I please get your approval?

@dhmlau dhmlau merged commit fe2acbf into loopbackio:master Jun 13, 2025
13 checks passed
@dhmlau
Copy link
Member

dhmlau commented Jun 13, 2025

@pnoebauer, thanks again for your contribution. Your PR has landed! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants