-
-
Notifications
You must be signed in to change notification settings - Fork 210
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
fix: multiple alternative security objects in route schema #817
Conversation
please add some unit tests |
Are these unit tests what you had in mind? |
To be honest, I dont see any code change in the lib folder. So this is only about typings? |
Yes, it's only about updating the type of the |
When this fix will be release?... |
@@ -40,7 +40,7 @@ declare module 'fastify' { | |||
consumes?: readonly string[]; | |||
produces?: readonly string[]; | |||
externalDocs?: OpenAPIV2.ExternalDocumentationObject | OpenAPIV3.ExternalDocumentationObject; | |||
security?: ReadonlyArray<{ [securityLabel: string]: readonly string[] }>; | |||
security?: ReadonlyArray<{ [securityLabel: string]: (readonly string[] | undefined) }>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is weird to allows undefined
here.
As @climba03003 rightly pointed out, I also think my changes are invalid, and this issue/PR should be closed. I looked into my problem more thoroughly, and it was not actually caused by Fastify or Typebox but by a lack of explicit typing on my end. To give a simplified context: const app = fastify().withTypeProvider<TypeBoxTypeProvider>();
// FastifyRequestTypebox & FastifyReplyTypebox defined as in the official README
export const SomeOperationSchema = {
summary: 'Do some operation',
security: [{ bearer_token: [] }, { api_key: [] }],
response: {
204: NoContentResponse
},
};
async function some_operation(
request: FastifyRequestTypebox<typeof SomeOperationSchema>,
reply: FastifyReplyTypebox<typeof SomeOperationSchema>,
) {
return reply.status(204).send();
}
app.get('/', { schema: SomeOperationSchema }, some_operation); This approach didn't work as the dynamic result of [{ bearer_token: [] }, { api_key: [] }] gets parsed as ({ bearer_token: never[]; test?: undefined; } | { test: never[]; bearer_token?: undefined; })[] thus, not matching the I tried fixing this by explicitly setting the type of the route schema: - export const SomeOperationSchema = {
+ export const SomeOperationSchema: FastifySchema = { but this seems to break the typing of everything else. What would be an appropriate solution? |
I am closing this PR because it is related to how TypeScript works and coding styles.
import Swagger from '@fastify/swagger'
import Fastify from 'fastify'
const fastify = Fastify()
await fastify.register(Swagger)
fastify.get('/', {
schema: {
security: [{ api_key: [] }, { bearer: [] }]
}
}, () => { })
import Swagger from '@fastify/swagger'
import Fastify from 'fastify'
const fastify = Fastify()
await fastify.register(Swagger)
const schema = {
security: [{ api_key: [] }, { bearer: [] }]
} as const
fastify.get('/', {
schema
}, () => { }) |
Fixes #816
Update the
security
property ofFastifySchema
to allow multiple security objects in a route schema.Checklist
npm run test
andnpm run benchmark
and the Code of conduct