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

Cache #31

Merged
merged 2 commits into from
Dec 7, 2020
Merged

Cache #31

merged 2 commits into from
Dec 7, 2020

Conversation

simoneb
Copy link
Contributor

@simoneb simoneb commented Dec 2, 2020

Fixes #30

Not an extremely elegant solution, but minimal code changes to make the cache work.

Checklist

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.

Can you please add a test?

@simoneb
Copy link
Contributor Author

simoneb commented Dec 3, 2020

Can you please add a test?

I considered that, but then I figured that if we have 100 on tap it means that the cache is already tested.

If we want to test it explicitly instead, we may need to extract it so that it can be provided as a plugin option (can't think of any other way to test it directly). It's probably easy to do but it modifies the plugin interface so it also has to be documented.

wdyt?

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

@mcollina
Copy link
Member

mcollina commented Dec 3, 2020

this is ok, no worries

@climba03003
Copy link
Member

climba03003 commented Dec 3, 2020

here is my concern about your cache implementation.
#30 (comment)

You can test the caches by specify two different route level serializer with same type but different behavior. and compare the expected result. if the serialized results are the same the test should fail because it break the route level serializer usage.

Another test that needed is, specify a route level serializer for an accepted type that global serializer do not have. then trigger the route once and trigger another route that do not have the route level serializer. if it do not throw an error, it means the caches is polluted by the route level config.

@Eomm
Copy link
Member

Eomm commented Dec 3, 2020

I would agree with @climba03003 , but... the custom configuration per route is documented but not implemented!

This snippet doesn't work:

const fastify = require('fastify')()

fastify.register(require('./index'), {
  default: 'application/json'
})

fastify.get('/', {
  config: {
    serializer: {
      serializers: [{
        regex: /^application\/yaml$/,
        serializer: body => 'yaml-custom-no-global-one'
      }]
    }
  }
},
(req, reply) => { reply.send({ foo: 'bar' }) })

fastify.inject({
  url: '/',
  headers: {
    Accept: 'application/yaml'
  }
}, (_, res) => {
  console.log(res.statusCode)
  console.log(res.payload) // expected yaml-custom-no-global-one it is a json: {"foo":"bar"}
})

While, registering the plugin like this works as expected:

fastify.register(require('./index'), {
  serializers: [
    {
      regex: /^application\/yaml$/,
      serializer: body => 'yaml-global'
    }
  ],
  default: 'application/json'
})

The tests confirm this:

test('serializer per route', t => {

Opening a new issue about it

@simoneb
Copy link
Contributor Author

simoneb commented Dec 7, 2020

@Eomm @climba03003 does this PR need further work?

Copy link
Member

@Eomm Eomm left a comment

Choose a reason for hiding this comment

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

LGTM

@Eomm Eomm merged commit c66a62f into fastify:master Dec 7, 2020
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.

Cache is not triggered
4 participants