Git Product home page Git Product logo

Comments (5)

weiliddat avatar weiliddat commented on June 1, 2024

The line that adds some default options for the OPTIONS response seem to be affecting it - perhaps because it sets a default schema that isn't merged?
I'm just guessing because commenting out line 36 (snippet below) in fastify-cors' index.js seems to avoid this issue, and was introduced in 2.2.0.

  if (preflight === true) {
    fastify.options('*', { schema: { hide: hideOptionsRoute } }, (req, reply) => reply.send())
  }

from fastify-cors.

Eomm avatar Eomm commented on June 1, 2024

We should investigate, but if you switch

    fastify.register(sharedSchemas);
    fastify.register(cors);

it seems to work

EDIT:
the register() function doesn't return a promise, you don't need to await it

from fastify-cors.

weiliddat avatar weiliddat commented on June 1, 2024

Hmm OK, after some debugging I'm not sure if it's just my misunderstanding on the conceptual/documentation thing on fastify's schema, route, and plugin encapsulation.
Maybe it makes sense to move this to the fastify repo instead because it's not really a cors plugin issue.

@Eomm you seem to be also doing some work on the schema compilation/registration so I guess you're the best person to ask about this.

Some things I found out:

  1. Registering a route with a schema option before registering the schema in a plugin, then using the schema. This causes the schema compiler to run twice, but on the second run it doesn't have the same list of schemas to compile, causing it to throw a reference error.
const fastify = require('fastify')({ logger: true });
const fp = require('fastify-plugin');

fastify.get('/', { schema: {} }, (req, reply) => reply.send());

fastify.register(fp(async f => f.addSchema({
    $id: 'https://example.com/bson/objectId',
    type: 'string',
    pattern: '\\b[0-9A-Fa-f]{24}\\b',
})));

fastify.get('/:id', {
    schema: {
        params: {
            type: 'object',
            properties: {
                id: { $ref: 'https://example.com/bson/objectId#' },
            },
        },
    },
    handler: async (req) => ({ hello: req.params.id }),
});

fastify.listen('1337', '127.0.0.1');
  1. Moving the schema registration out of the plugin makes it fine then. I'm not sure why? I thought running the plugin through fastify-plugin should also avail the schemas to whatever is in the registration scope, but it seems I'm wrong about this.

The server below runs fine, and schemas compile and throw the correct error if the routes do not fulfill the pattern.

const fastify = require('fastify')({ logger: true });

fastify.get('/', { schema: {} }, (req, reply) => reply.send());

fastify.addSchema({
    $id: 'https://example.com/bson/objectId',
    type: 'string',
    pattern: '\\b[0-9A-Fa-f]{24}\\b',
});

fastify.get('/:id', {
    schema: {
        params: {
            type: 'object',
            properties: {
                id: { $ref: 'https://example.com/bson/objectId#' },
            },
        },
    },
    handler: async (req) => ({ hello: req.params.id }),
});

fastify.listen('1337', '127.0.0.1');
  1. Not having any routes that do not use schema options before the schema is registered in a plugin also makes it work.
const fastify = require('fastify')({ logger: true });
const fp = require('fastify-plugin');

fastify.get('/', (req, reply) => reply.send());

fastify.register(fp(async f => f.addSchema({
    $id: 'https://example.com/bson/objectId',
    type: 'string',
    pattern: '\\b[0-9A-Fa-f]{24}\\b',
})));

fastify.get('/:id', {
    schema: {
        params: {
            type: 'object',
            properties: {
                id: { $ref: 'https://example.com/bson/objectId#' },
            },
        },
    },
    handler: async (req) => ({ hello: req.params.id }),
});

fastify.listen('1337', '127.0.0.1');

from fastify-cors.

weiliddat avatar weiliddat commented on June 1, 2024

I'm closing this and opening an issue in fastify instead.

from fastify-cors.

Eomm avatar Eomm commented on June 1, 2024

but it seems I'm wrong about this.

No, you are right.

This causes the schema compiler to run twice

I think this is the error, my thought is that the schemas are consumed and the $id is removed.

We will check in the other issue πŸ‘

from fastify-cors.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.