SOFA icon indicating copy to clipboard operation
SOFA copied to clipboard

OpenAPI's "security" property is not typescript friendly

Open GoranD1 opened this issue 1 year ago • 2 comments

I tried to configure the authentication part of the openAPI documentation with the following config:

  app.use(
    '/api',
    useSofa({
      schema,
      basePath: '/api',
      openAPI: {
        components: {
          securitySchemes: {
            ApiKeyAuth: {
              type: 'apiKey',
              in: 'header',
              name: 'authorization',
            },
          },
        },
        security: [
          {
            ApiKeyAuth: [],
          },
        ],
      },
    })
  );

But typescript gives the following error:

Type '{ components: { securitySchemes: { ApiKeyAuth: { type: string; in: string; name: string; }; }; }; security: { ApiKeyAuth: never[]; }[]; }' is not assignable to type 'RouterOpenAPIOptions<any>'.
  Object literal may only specify known properties, and 'security' does not exist in type 'RouterOpenAPIOptions<any>'.ts(2322)
sofa.d.ts(37, 5): The expected type comes from property 'openAPI' which is declared here on type 'SofaConfig'

The security parameter works fine if I do some "hacking" and suppress typescript errors, so then I guess this is just the issue with types. I am on v0.18.0.

GoranD1 avatar Aug 28 '23 10:08 GoranD1

@GoranD1 I'm affected by this too - what was your workaround?

C-D-Lewis avatar Feb 23 '24 15:02 C-D-Lewis

@C-D-Lewis Adding //@ts-ignore should do the trick and if @typescript/eslint is giving you troubles for it add the //eslint-disable-next-line @typescript-eslint/ban-ts-comment as well.

So in the end it would be something like this:

  app.use(
    '/api',
    useSofa({
      schema,
      basePath: '/api',
      openAPI: {
        components: {
          securitySchemes: {
            ApiKeyAuth: {
              type: 'apiKey',
              in: 'header',
              name: 'authorization',
            },
          },
        },
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        security: [
          {
            ApiKeyAuth: [],
          },
        ],
      },
    })
  );

GoranD1 avatar Feb 26 '24 10:02 GoranD1