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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use current instance schema controller #3454

Merged
merged 4 commits into from
Nov 20, 2021

Conversation

metcoder95
Copy link
Member

While still working on #3121 noticed when adding Schemas to child instances, these schemas were lost in the middle, causing any nested validation schema to fail the compilation step due to missing the schema(s) reference.

Note:

  • This only happens when registering plugins (for instance not creating a new context)
  • I'm not 100% why of the decision of using the parent of the SchemaController on each new registration instead of the one that belongs to the current instance, so please feel free to correct me if the approach is wrong or you think that can cause any side effect that I'm not considering within this PR 馃檪

Checklist

@mcollina mcollina requested review from Eomm and zekth November 17, 2021 07:23
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

fastify.js Outdated
@@ -631,7 +631,7 @@ function fastify (options) {
function setSchemaController (schemaControllerOpts) {
throwIfAlreadyStarted('Cannot call "setSchemaController" when fastify instance is already started!')
const old = this[kSchemaController]
const parent = old.parent || old
const parent = old
Copy link
Member

Choose a reason for hiding this comment

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

If the issue is the plugin registration with a null ref, shouldn't this be smth like this?

Suggested change
const parent = old
const parent = old ? old.parent : old

Copy link
Member Author

Choose a reason for hiding this comment

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

Not exactly, the issue appears on every new registration of a SchemaController, if you register a new one within a new encapsulated context, as the current context where the controller is being registered as a parent, any added schema within the current context is missed, causing to (in this case Ajv) totally missed such registered schemas.

By following the suggestion, see the result:
image

Having in mind that:

  • some was registered in the root instance
  • another was registered in the child instance

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a test without a plugin for better explanation 馃檪
4877f81

Copy link
Member

Choose a reason for hiding this comment

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

I would remove the parent variable since it is no more useful.

To help understand what is going on here, be aware that the compilersFactory is used only when necessary.

In this case, the root context does not use any schemas because it does not contain any route.
For this reason, the "main" compilers is not built.

As you can see, the plugin context instead uses a route and try to build the compilers, triggering the error (old is null)

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed in 79213ce 馃憤

@mcollina mcollina merged commit 1c8e4ed into fastify:main Nov 20, 2021
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants