Skip to content

Conversation

@jsumners
Copy link
Member

@jsumners jsumners commented Jul 31, 2019

The issue I was having in #39 was due to object composition and cloning. I have a wrapper around Fastify that reads a list of routes from config/routes.js and then the corresponding handlers from lib/handlers/. For example:

config/routes.js:

module.exports = [{
  path: '/foo',
  method: 'GET',
  handler: 'foo'
}]

lib/handlers/foo.js:

module.exports = {
  schema: {
    query: { fluent.object().prop('foo', fluent.string()) }
  },
  handler(req, res) {
    res.send(req.query)
  }
}

So these two files are read in and the objects are merged to build up the full route definition before being passed to fastify.route. If any of the schemas are fluent-schema instances then the Symbol.for('fluent-schema-object') will be lost during this process because symbols do not get copied in any current object merging method (by design). But if we don't retain the ability to determine if the schemas are instances of fluent-schema then we can't work with these schemas during the schema compilation process. Thus, this PR adds an isFluentSchema boolean to use instead of the symbol.

@mcollina
Copy link
Member

Would you mind adding that to the docs?

@jsumners
Copy link
Member Author

jsumners commented Jul 31, 2019

I don't see anything in docs/API.md for the symbol so I didn't add it for this. The only mention of this type of stuff is in the README.md.

@mcollina
Copy link
Member

mcollina commented Jul 31, 2019 via email

@jsumners
Copy link
Member Author

I did add it to the readme.

@jsumners
Copy link
Member Author

I have updated API.md with bits for both properties. I think the symbol is extraneous, though, and should be removed.

@mcollina mcollina requested review from aboutlo and delvedor August 1, 2019 07:32
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

Copy link
Member

@delvedor delvedor left a comment

Choose a reason for hiding this comment

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

LGTM

At this point why keep the symbol at all?

@mcollina
Copy link
Member

mcollina commented Aug 1, 2019 via email

@jsumners
Copy link
Member Author

jsumners commented Aug 1, 2019

Merge release and I’ll submit a PR to Fastify.

@mcollina
Copy link
Member

mcollina commented Aug 1, 2019

I'd prefer to wait for @aboutlo opinion.

@aboutlo
Copy link
Collaborator

aboutlo commented Aug 1, 2019 via email

@aboutlo
Copy link
Collaborator

aboutlo commented Aug 3, 2019

hey, @jsumners thank you for opening the PR!

A couple of things:

  • API.md is a file autogenerated from doc. I would add the explanation just in the README
  • Let's just put the new method only in the README, but we keep the old one as well under the hood so no breaking changes

So these two files are read in, and the objects are merged to build up the full route definition before being passed to fastify.route

Could you add a test to reproduce your cloning issue? Maybe in the Integration tests

I would expect to have similar issues given I do a lot of deconstructing internally at FluentSchema, but maybe I have been lucky.

I'm worried that Fluent Schema has an internal util method https://github.com/fastify/fluent-schema/blob/5382d9bfce1d630356878289e1abb06a57057882/src/utils.js#L3 that has been changed to use the symbol.

So if we can reproduce the issue I would expect we need to move to the boolean implementation in order to fix it

@jsumners
Copy link
Member Author

jsumners commented Aug 3, 2019

Good call on the integration test. It seems I was wrong about Object.assign and {...foo} in regard to symbols. They actually do copy such properties over. The actual issue is my usage of https://www.npmjs.com/package/lodash.merge which "... recursively merges own and inherited enumerable string keyed properties of source objects into the destination object."

I see that I can probably rework how I'm cloning my objects in my project (possibly), but I also think the usage of a Symbol here is overkill and just being crafty for the sake of being crafty. So I still think migrating to a plain boolean is best.

@aboutlo
Copy link
Collaborator

aboutlo commented Aug 3, 2019

I think the use case to detect if an object is a FluentSchema makes sense but not sure about the best implementation between Symbol vs Boolean.

We should only have one approach and if Symbol works I would keep it rather than changing the API.

@delvedor, you have originally proposed the Symbol approach do you have any suggestion?

@jsumners
Copy link
Member Author

jsumners commented Aug 3, 2019

Symbols are useful when the property doesn’t need to be exposed during serialization or to keep the property “private”. Neither situation applies here. The regular property is less mysterious and easier to use.

@mcollina
Copy link
Member

mcollina commented Aug 3, 2019 via email

@jsumners
Copy link
Member Author

jsumners commented Aug 7, 2019

@delvedor are you +1 for using a plain property?

@aboutlo
Copy link
Collaborator

aboutlo commented Aug 8, 2019

OK, given there is a general consensus if no news hopefully this evening I will move forward with the boolean approach

@aboutlo aboutlo merged commit dfd65d8 into fastify:master Aug 8, 2019
@jsumners jsumners deleted the non-symbol branch August 8, 2019 23:41
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.

4 participants