Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lists of Security Requirement Objects on an operation are not supported #816

Closed
2 tasks done
beryxz opened this issue Aug 21, 2024 · 3 comments
Closed
2 tasks done
Labels
bug Confirmed bug good first issue Good for newcomers typescript TypeScript related

Comments

@beryxz
Copy link

beryxz commented Aug 21, 2024

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.28.1

Plugin version

8.15.0

Node.js version

22.6

Operating system

Linux

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

24.04

Description

It seems that the security property definition inside FastifySchema doesn't cover the use case of multiple different security objects on a route schema (spec. ref):

interface FastifySchema {
    ...
    security?: ReadonlyArray<{ [securityLabel: string]: readonly string[] }>;
}

The relevant part would be:

When a list of Security Requirement Objects is defined on the OpenAPI Object or Operation Object, only one of the Security Requirement Objects in the list needs to be satisfied to authorize the request.

It should then be possible to use both the following:

{
  // api_key AND bearer_token
  security: [{ api_key: [], bearer_token: [] }]
}
{
  // api_key OR bearer_token
  security: [{ api_key: [] }, { bearer_token: [] }]
}

but the "OR" solution doesn't work and throws the error:

Types of property 'security' are incompatible.
    Type '({ bearer_token: never[]; api_key?: undefined; } | { api_key: never[]; bearer_token?: undefined; })[]' is not assignable to type 'readonly { [securityLabel: string]: readonly string[]; }[]'.
      Type '{ bearer_token: never[]; api_key?: undefined; } | { api_key: never[]; bearer_token?: undefined; }' is not assignable to type '{ [securityLabel: string]: readonly string[]; }'.
        Type '{ bearer_token: never[]; api_key?: undefined; }' is not assignable to type '{ [securityLabel: string]: readonly string[]; }'.
          Property 'api_key' is incompatible with index signature.
            Type 'undefined' is not assignable to type 'readonly string[]'.

Link to code that reproduces the bug

No response

Expected Behavior

It should be possible to use both solutions in the security property.

@mcollina mcollina added bug Confirmed bug good first issue Good for newcomers typescript TypeScript related labels Aug 22, 2024
@mcollina
Copy link
Member

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests. We use tsd for type tests.

@beryxz
Copy link
Author

beryxz commented Aug 23, 2024

Sure, here it is; I hope everything is in order.

The new tests explicitly use undefined as errors weren't raised without it. The initial stack trace was probably a consequence of using Fastify with the TypeBoxTypeProvider.

@climba03003
Copy link
Member

climba03003 commented Sep 2, 2024

As per my comment here.
#817 (comment)

It is working intended because of how TypeScript reflect the array types.
You can either place all together which allows proper infer or use as const to provides actual types.

@climba03003 climba03003 closed this as not planned Won't fix, can't repro, duplicate, stale Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed bug good first issue Good for newcomers typescript TypeScript related
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants