Skip to content

Circular reference types when used with @fastify/multipart #57

@sinedied

Description

@sinedied

Prerequisites

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

Fastify version

4.22.0

Plugin version

2.2.2

Node.js version

18.16.0

Operating system

macOS

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

12.6.8

Description

Using FastifyPluginAsyncJsonSchemaToTs along with the shared schema from @fastify/multipart generates a circular type error:

[TypeScript] src/routes/indexes/index.ts:104:40 - error TS2615: Type of property 'file' circularly references itself in mapped type '{ file: Never; }'.
[TypeScript] 
[TypeScript] 104       const { file } = request.body;
[TypeScript]                            ~~~~~~~~~~~~

Steps to Reproduce

  1. Configure @fastify/multipart in a test project with these options:
import fp from 'fastify-plugin';
import multipart from '@fastify/multipart';

export default fp(async (fastify) => {
  fastify.register(multipart, {
    attachFieldsToBody: true,
    sharedSchemaId: '#multipartField',
  });
});
  1. Create a route that references the shared schema:
import { FastifyPluginAsyncJsonSchemaToTs } from '@fastify/type-provider-json-schema-to-ts';

const root: FastifyPluginAsyncJsonSchemaToTs = async (fastify, opts): Promise<void> => {

  fastify.post('/upload', {
    schema: {
      body: {
        type: 'object',
        properties: {
          file: { $ref: '#multipartField' },
        },
        required: ['file'],
      },
    } as const,
    handler: async function (request, reply) {
      const { file } = request.body;
    }
  });
});

Expected Behavior

No error

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions