-
-
Notifications
You must be signed in to change notification settings - Fork 27
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
Adding multiple items to the security array of the JSON Schema breaks all the types #40
Comments
cc @fastify/typescript |
Had the same issue, but when providing an additional Using the alternative syntax using |
@sinclairzx81 could you take a look? |
Hi @mcollina ! Just had a quick look at the original issue code, this seems to be working ok... General Recommendation:
Happy to look a bit deeper if there's a way to repro the inference issue, but this appears to be working alright :) Hope this helps! |
Thanks! |
Well, seems like my first assumption was not correct, my issue popped up in all ( The issue I had, which I do not think is related to OP's issue, was that the function which I wanted to override ...by enforcing these basic types, functionality breaks and my request components all resolves as Thank you guys @mcollina @sinclairzx81 for your time :) |
I ran into the same issues with multiple items in the security array making body https://github.com/andreaspalsson/typebox-schema-security Also ran it through GitHub Actions to make sure it wasn't something local and the types fails there as well https://github.com/andreaspalsson/typebox-schema-security/actions/runs/4232737696/jobs/7352833222 Versions:
|
@andreaspalsson Hey, thanks for the repro! Had another look at this and can reproduce the issue on the TSP. The issue seems to be related to the definitions inside // import '@fastify/swagger' // uncomment to introduce error IssueThe swagger package performs a declaration merge on the interface FastifySchema {
hide?: boolean;
deprecated?: boolean;
tags?: string[];
description?: string;
summary?: string;
consumes?: string[];
produces?: string[];
externalDocs?: OpenAPIV2.ExternalDocumentationObject | OpenAPIV3.ExternalDocumentationObject;
security?: Array<{ [securityLabel: string]: string[] }>; // <-- issue here !
/**
* OpenAPI operation unique identifier
*/
operationId?: string;
} TypeScript can have some difficulties dealing with arbitrary length array / property key definitions when mapping types, but its curious why it's causing issues here as this property is not mapped. My best guess is due to the declaration merge, this is causing the In situations like this though, it's common to turn to Possible FixThe following updates the // ------------------------------------------------------------------------------
// Fix: @fastify/swagger
//
// Here we mandate that the security array is readonly, and the caller must
// pass with a 'as const' assertion. This enables TS to propagate the type as
// fixed readonly data structure. Similar techniques can be seen in libraries
// like json-schema-to-ts which mandate 'as const' to infer object property
// keys encoded in JSON Schema.
// ------------------------------------------------------------------------------
declare module "fastify" {
// added: readonly is required for propagation of dictionary types.
export type SecurityArray = readonly {
[securityLabel: string]: readonly string[]
}[]
interface FastifySchema {
hide?: boolean;
deprecated?: boolean;
tags?: string[];
description?: string;
summary?: string;
consumes?: string[];
produces?: string[];
externalDocs?: OpenAPIV2.ExternalDocumentationObject | OpenAPIV3.ExternalDocumentationObject;
security?: SecurityArray; // Fix
/**
* OpenAPI operation unique identifier
*/
operationId?: string;
}
} ExampleHere's an example of things working in the TSP with the server.post(
"/double-security",
{
schema: {
body: Type.Object({
foo: Type.Number(),
bar: Type.String(),
}),
security: [{ apiKey: ['foo'] }, { authHeader: ['bar'] }] as const // mandate const readonly assertion
},
},
(request, reply) => {
const { foo, bar } = request.body; // ok
return { foo, bar };
}
);
}; FutureWhile in TS 4.0 the Hope this helps |
@andreaspalsson @Amphaal Oh, one other thing. In the interim, you should be able to get around the inference issues with an security: [{ apiKey: [] }, { authHeader: [] }] as any // replace with "as const" on fix Cheers |
@sinclairzx81 Thanks for a great explanation of the problem and a workaround. We only have a few endpoints that have multiple security items so this interim solution will work fine for us. Thanks for the quick help. |
Is this a problem i |
@mcollina Yeah, it might be worth opening an issue on |
Should we move this issue? |
Prerequisites
Fastify version
4.5.3
Plugin version
2.3.0
Node.js version
18.7.0
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
11.5.2
Description
We have a problem that our types are not automatically inferred when we add multiple items to the
security
property of the JSON schema that you can define on the route.You can see this in the example below:
Steps to Reproduce
Expected Behavior
We'd like that our types are inferred when adding multiple items to the security array.
The text was updated successfully, but these errors were encountered: