ts-spec / tspec

Type-driven API Documentation library. Auto-generating REST API document based on TypeScript types.
https://ts-spec.github.io/tspec/
MIT License
127 stars 5 forks source link

Void in 204 responses throws "Not supported: root type undefined" in typescript-json-schema #43

Closed Jukkay closed 9 months ago

Jukkay commented 9 months ago

I'm having problems with adding response types for 204 responses.

'/{id}/data/': {
      get: {
        summary: 'Get data by id',
        path: { id: number },
        description: 'Returns data as an object.',
        responses: {
          200: responseDataType,
          204: void,
        },
      },
    },

As per documentation, this should be the way, right? But I'm getting this error:

[1] /projecti/node_modules/typescript-json-schema/typescript-json-schema.ts:689
[1]                     throw new Error("Not supported: root type undefined");
[1]                           ^
[1] Error: Not supported: root type undefined
[1]     at JsonSchemaGenerator.getDefinitionForRootType (/dsm_api/node_modules/typescript-json-schema/typescript-json-schema.ts:689:27)
[1]     at JsonSchemaGenerator.getTypeDefinition (/dsm_api/node_modules/typescript-json-schema/typescript-json-schema.ts:1464:26)
[1]     at JsonSchemaGenerator.getDefinitionForProperty (/dsm_api/node_modules/typescript-json-schema/typescript-json-schema.ts:821:33)
[1]     at /dsm_api/node_modules/typescript-json-schema/typescript-json-schema.ts:1178:38
[1]     at Array.reduce (<anonymous>)
[1]     at JsonSchemaGenerator.getClassDefinition (/dsm_api/node_modules/typescript-json-schema/typescript-json-schema.ts:1176:47)
[1]     at JsonSchemaGenerator.getTypeDefinition (/dsm_api/node_modules/typescript-json-schema/typescript-json-schema.ts:1480:26)
[1]     at JsonSchemaGenerator.getDefinitionForProperty (/dsm_api/node_modules/typescript-json-schema/typescript-json-schema.ts:821:33)
[1]     at /dsm_api/node_modules/typescript-json-schema/typescript-json-schema.ts:1178:38
[1]     at Array.reduce (<anonymous>)

Any other type works without errors. I'm on "tspec": "^0.1.110" and Node version 20.10.0.

My config:

export const optionsV1: Tspec.GenerateParams = {
  specPathGlobs: ['src/**/*.ts'],
  tsconfigPath: './tsconfig.json',
  outputPath: './generate/openapi.json',
  specVersion: 3,
  openapi: {
    title: 'API name,
    version: '1.0.0',
    servers: [
      {
        url: 'url',
        description: 'description',
      },
    ]
  },
  debug: false,
  ignoreErrors: true,
};

Not sure if this is an issue with Tspec or typescript-json-schema but any help would be appreciated.

hyeonss0417 commented 9 months ago

Thank you for your reporting👍 It seems to be an issue caused by the lack of support for void type in typescript-json-schema. We'll work on this issue quickly in the near future and let you know when it is resolved.

hyeonss0417 commented 9 months ago

After identifying the cause of the problem, we realized that the error was occurring in the typescript-json-schema library when a particular type was defined as only void instead of a union type. Rather than fixing the typescript-json-schema library directly, we wrote a fix PR to allow the response body to indicate that it is empty in a way other than void. (#46)

If your existing problematic code looked like this

'/{id}/data/': {
      get: {
        summary: 'Get data by id',
        path: { id: number },
        description: 'Returns data as an object.',
        responses: {
          200: responseDataType,
          204: void,
        },
      },
    },

As of tspec 0.1.111, you can fix this by modifying it like this (or by putting in an empty string)

'/{id}/data/': {
      get: {
        summary: 'Get data by id',
        path: { id: number },
        description: 'Returns data as an object.',
        responses: {
          200: responseDataType,
          204: Tspec.NoContent,
        },
      },
    },
Jukkay commented 9 months ago

Alright, this seems to work. Thank you, excellent work!