Skip to content

global options are not taked into account when route options are defined partially #340

@blephy

Description

@blephy

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

5.2.0

Plugin version

8.0.1

Node.js version

22.12.0

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

15.1.1

Description

Given this plugin registered has follow :

function onInvalidRequestPayload(
  ...parameters: Parameters<NonNullable<FastifyCompressOptions['onInvalidRequestPayload']>>
) {
  return {
    statusCode: HTTPStatusCodeEnum.BadRequest,
    code: 'Invalid Request Payload',
    name: 'InvalidRequestPayload',
    error: 'Invalid Request Payload',
    message: `Payload is not ${parameters[0]} encoded. ${parameters[2].message}`,
  };
}

function onUnsupportedRequestEncoding(
  ...parameters: Parameters<NonNullable<FastifyCompressOptions['onUnsupportedRequestEncoding']>>
) {
  return {
    statusCode: HTTPStatusCodeEnum.UnsupportedMediaType,
    code: 'Unsupported Media Type',
    name: 'UnsupportedMediaType',
    error: 'Unsupported Media Type',
    message: `${parameters[0]} encoding is not supported`,
  };
}
  
await fastify.register(compressPlugin, {
        global: true,
        onInvalidRequestPayload: onInvalidRequestPayload,
        removeContentLengthHeader: false,
        onUnsupportedRequestEncoding: onUnsupportedRequestEncoding,
      });

if a route is registered has follow :

fastifyInstance.withTypeProvider<FastifyZodOpenApiTypeProvider>().route({
    method: 'POST',
    url: '/',
    schema: {
      body: BODY,
      response: {
        [HTTPStatusCodeEnum.OK]: RESPONSE,
      },
    },
    decompress: {
      // eslint-disable-next-line @typescript-eslint/explicit-function-return-type, @typescript-eslint/promise-function-async
      onUnsupportedRequestEncoding: function (encoding, request, reply) {
        // we duplicate this here because our preParsing hook is not triggered in this context
        request.monitoringScanEvent =
          MonitoringScanEvent.CreateWithoutTraceId().setName('a');

        return onUnsupportedRequestEncoding(encoding, request, reply);
      },
    },
    bodyLimit: 14_680_064, // 14M
    preParsing: (request, _reply, _payload, done): void => {
      request.monitoringScanEvent =
        MonitoringScanEvent.CreateWithoutTraceId().setName('a');

      done();
    },
    errorHandler: async (error, request, reply): Promise<void> => {
      request.monitoringScanEvent.setStatus('error');
      request.monitoringScanEvent.mergeWithMessage({
        error: error.message,
      });

      fastifyInstance.errorHandler(error, request, reply);
    },
    onResponse: async (request): Promise<void> => {
      await fastifyInstance.httpClient
        .getInstance()
        .v1.monitoring.postScanEvent(
          request.monitoringScanEvent.toObject(),
          request.monitoringScanEvent.getTraceId(),
        );
    },
    handler: async (request, reply): Promise<ResponseType> => {
      const response = await handler(
        fastifyInstance,
        request.body,
        request.headers,
        request.monitoringScanEvent,
        request.log,
      );

      return await reply.status(HTTPStatusCodeEnum.OK).send(response);
    },
  });

This onInvalidRequestPayload hook provided in the global configuration at plugin registration is not taken into account, instead, the 'default' hook from the plugin is used.

Link to code that reproduces the bug

No response

Expected Behavior

This plugin should merge every options with :

  • route option first
  • global option at registration in second
  • fallback to default plugin option last

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions