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
src: change request.context to use a symbol #2989
Conversation
Many of the other properties in fastify use a Symbol as the property key in order to essentially hide these properties, preventing them from becoming a part of the published API. The `context` property on `request` was essentially an undocumented breaking change to the API. See: fastify#2506 This commit removes the `context` property from `Request` and replaces it with a `Symbol` to both revert this breaking change, and to remove the `context` property from the API surface area. This is potentially a breaking change, however, the `Request#context` property has never been documented, and is - based on my understanding of the intent in fastify#2506 - not meant to be a part of the public API.
I think Making it private will definitely be breaking for most folks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with @mcollina, context
is badly documented but there are real uses cases around.
An example if present in the docs.
The main issue is that context
contains both private and public info. We should discuss about refactoring it in v4.
Lines 7 to 29 in 0ae362b
function Context (schema, handler, Reply, Request, contentTypeParser, config, errorHandler, bodyLimit, logLevel, logSerializers, attachValidation, replySerializer, schemaErrorFormatter) { | |
this.schema = schema | |
this.handler = handler | |
this.Reply = Reply | |
this.Request = Request | |
this.contentTypeParser = contentTypeParser | |
this.onRequest = null | |
this.onSend = null | |
this.onError = null | |
this.onTimeout = null | |
this.preHandler = null | |
this.onResponse = null | |
this.config = config | |
this.errorHandler = errorHandler | |
this._middie = null | |
this._parserOptions = { limit: bodyLimit || null } | |
this.logLevel = logLevel | |
this.logSerializers = logSerializers | |
this[kFourOhFourContext] = null | |
this.attachValidation = attachValidation | |
this[kReplySerializerDefault] = replySerializer | |
this.schemaErrorFormatter = schemaErrorFormatter || defaultSchemaErrorFormatter | |
} |
It is used for sure, like accessing the route config eg: |
The examples referenced by @Eomm and @delvedor are both accessing the The only thing that this PR changes is how
I did not check for usage in all of the organization repos. But just to be clear, if they were written before #2506 landed then they were accessing |
+1000 |
@lance yes, we referenced examples with fastify.decorateRequest('getStuff', function () {
if (this.context.config.something) {
return 'stuff'
}
return 'not stuff'
}) |
@delvedor I know that. It was the addition of  |
For what it's worth, I understand if you don't want to land this PR. But I would say that if you don't it's very important that |
fastify.decorateRequest('getStuff', function () {
if (this.context.config.something) { For a user to do this, they would have to know that there is a context property on the request object. That is not documented. |
I'm sorry if we broke you while adding that property.. however that was 8 months ago: a bit late for a revert. I'm +1 in documenting the context property as "opaque". |
No apology needed. This only arose now because someone else recently complained about the situation in #2506 and was told "we accept PRs". I was just trying to do my part. 🤷 |
I don't want this to seem like we don't accept PRs. We do accept them after considered review. The review in this case has determined that the status quo is now set and a revert is not viable. It has also determined that some work around this should be done against the @lance would you be willing to spearhead that effort? |
Understood - and I submitted this PR knowing that it would likely be rejected. I just had to defend it as best as I could.
Can you help me understand what that looks like? Based on this conversation, it sounds like there should be a publicly documented API for |
Not really. I would just document that a |
Even for
|
I realize that I'm 7mo late to this conversation, but would the team be open to a PR that types https://github.com/fastify/fastify/blob/main/types/request.d.ts
While it didn't cause any major issues for us, my team was stashing away some request-scoped data in this field and were surprised to find a few weeks ago that it was already being used internally by Fastify. This was prior to the field being documented in #3201 and we didn't check via a debugger if it was already being used. But we got lucky and didn't clobber any of the existing fields. 😄 Let me know if y'all are open to that small PR and I'll submit it. |
You can update it to the same as Line 24 in 11c77b5
|
I thought about proposing that, but it seems to run counter to things said in this thread by @mcollina, @delvedor, and @jsumners that this field is internal/opaque and shouldn't be carried forward as-is into v4 and beyond. That was the main reason for proposing it be typed as I'm happy to submit a PR that goes either route depending on what the maintainers prefer. I just don't want others to get bit by this. |
The other property of |
@climba03003 - sounds good! Submitted a PR here that types |
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. |
Many of the other properties in fastify objects use a Symbol as the property key in order to hide these properties, preventing them from becoming a part of the public API. The
context
property onrequest
was essentially an undocumented breaking change to the API.See: #2506
This commit removes the
context
property fromRequest
and replaces it with aSymbol
to both revert this breaking change, and to remove thecontext
property from the API surface area.This is potentially a breaking change, however, the
Request#context
property has never been documented, and is - based on my understanding of the intent in #2506 - not meant to be a part of the public API.Checklist
npm run test
andnpm run benchmark
and the Code of conduct