-
-
Notifications
You must be signed in to change notification settings - Fork 37
feat: ability to limit combinations #3372
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
Changes from all commits
dd5bc6a
e4f0441
83e0642
0e55963
ac657e0
07c81c0
9003613
13f4df4
a81e031
72fdf78
a9eacbb
4eb5891
330ccb3
ebec410
9941ce4
4ff2d0c
3efbec7
57ca58f
116e65a
4fd3b23
4b4847b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -13,6 +13,7 @@ import { contentTypes } from "./content-type"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { DocumentationError } from "./errors"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { getInputSources, makeCleanId } from "./common-helpers"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { CommonConfig } from "./config-type"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { getSecurityHeaders } from "./security"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { processContainers } from "./logical-container"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { ClientMethod } from "./method"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -86,6 +87,26 @@ interface DocumentationParams { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @example { users: "About users", files: { description: "About files", url: "https://example.com" } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tags?: Parameters<typeof depictTags>[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @desc Caps on the number of generated entities. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @default { examples: defaultMaxCombinations, security: defaultMaxCombinations } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @example { examples: 20, security: 10 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| limits?: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @desc Caps the number of generated examples (request/response examples from examples of individual properties). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @default defaultMaxCombinations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @see defaultMaxCombinations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| examples?: number; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @desc Caps the number of security schemas combinations. Must be at least 1. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @default defaultMaxCombinations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @todo decouple from defaultMaxCombinations, use higher but still fixed limit in v28 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @see Middleware | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| security?: number; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+96
to
+108
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two issues on the nested properties: (1) a
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export class Documentation extends OpenApiBuilder { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -161,6 +182,7 @@ export class Documentation extends OpenApiBuilder { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| hasSummaryFromDescription = true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| hasHeadMethod = true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| composition = "inline", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| limits: { examples: maxExamples, security: maxSecurity } = {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }: DocumentationParams) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| super(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.addInfo({ title, version }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -173,6 +195,7 @@ export class Documentation extends OpenApiBuilder { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| endpoint, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| composition, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| brandHandling, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| maxExamples, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| makeRef: this.#makeRef.bind(this), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { description, shortDescription, scopes, inputSchema } = endpoint; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -189,12 +212,12 @@ export class Documentation extends OpenApiBuilder { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const request = depictRequest({ ...commons, schema: inputSchema }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const security = processContainers(endpoint.security); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const securityHeaders = getSecurityHeaders(endpoint.security); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const depictedParams = depictRequestParams({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...commons, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| inputSources, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isHeader, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| security, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| securityHeaders, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
RobinTail marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| request, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: descriptions?.requestParameter?.call(null, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| method, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -205,7 +228,7 @@ export class Documentation extends OpenApiBuilder { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const responses: ResponsesObject = {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const variant of responseVariants) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const apiResponses = endpoint.getResponses(variant); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const apiResponses = endpoint.getResponses(variant, { maxExamples }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const { mimeTypes, schema, statusCodes } of apiResponses) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const statusCode of statusCodes) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| responses[statusCode] = depictResponse({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -243,7 +266,10 @@ export class Documentation extends OpenApiBuilder { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : undefined; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const securityRefs = depictSecurityRefs( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| depictSecurity(security, inputSources), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| depictSecurity( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| processContainers(endpoint.security, maxSecurity), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| inputSources, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| scopes, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (securitySchema) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const name = this.#ensureUniqSecuritySchemaName(securitySchema); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,6 +9,7 @@ import { | |||||||||||||||||
| getInput, | ||||||||||||||||||
| ensureError, | ||||||||||||||||||
| isSchema, | ||||||||||||||||||
| defaultMaxCombinations, | ||||||||||||||||||
| } from "./common-helpers"; | ||||||||||||||||||
| import { CommonConfig } from "./config-type"; | ||||||||||||||||||
| import { | ||||||||||||||||||
|
|
@@ -57,6 +58,7 @@ export abstract class AbstractEndpoint { | |||||||||||||||||
| /** @internal */ | ||||||||||||||||||
| public abstract getResponses( | ||||||||||||||||||
| variant: ResponseVariant, | ||||||||||||||||||
| params: { maxExamples?: number }, | ||||||||||||||||||
| ): ReadonlyArray<NormalizedResponse>; | ||||||||||||||||||
|
Comment on lines
59
to
62
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The options bag is required but its only field is optional, so every caller that doesn't want to cap must still write
Suggested change
|
||||||||||||||||||
| /** @internal */ | ||||||||||||||||||
| public abstract getOperationId(method: ClientMethod): string | undefined; | ||||||||||||||||||
|
|
@@ -90,16 +92,16 @@ export class Endpoint< | |||||||||||||||||
| readonly #def: ConstructorParameters<typeof Endpoint<IN, OUT, CTX>>[0]; | ||||||||||||||||||
|
|
||||||||||||||||||
| /** considered expensive operation, only required for generators */ | ||||||||||||||||||
| #ensureOutputExamples = R.once(() => { | ||||||||||||||||||
| #ensureOutputExamples(limit: number) { | ||||||||||||||||||
| if (globalRegistry.get(this.#def.outputSchema)?.examples?.length) return; // examples on output schema, or pull up: | ||||||||||||||||||
| if (!isSchema<z.core.$ZodObject>(this.#def.outputSchema, "object")) return; | ||||||||||||||||||
| const examples = pullResponseExamples(this.#def.outputSchema); | ||||||||||||||||||
| const examples = pullResponseExamples(this.#def.outputSchema, limit); | ||||||||||||||||||
| if (!examples.length) return; | ||||||||||||||||||
| const current = this.#def.outputSchema.meta(); | ||||||||||||||||||
| globalRegistry | ||||||||||||||||||
| .remove(this.#def.outputSchema) // reassign to avoid cloning | ||||||||||||||||||
| .add(this.#def.outputSchema, { ...current, examples }); | ||||||||||||||||||
| }); | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
94
to
+104
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dropping |
||||||||||||||||||
|
|
||||||||||||||||||
| constructor(def: { | ||||||||||||||||||
| deprecated?: boolean; | ||||||||||||||||||
|
|
@@ -172,8 +174,12 @@ export class Endpoint< | |||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /** @internal */ | ||||||||||||||||||
| public override getResponses(variant: ResponseVariant) { | ||||||||||||||||||
| if (variant === "positive") this.#ensureOutputExamples(); | ||||||||||||||||||
| public override getResponses( | ||||||||||||||||||
| variant: ResponseVariant, | ||||||||||||||||||
| { maxExamples = defaultMaxCombinations }: { maxExamples?: number }, | ||||||||||||||||||
| ) { | ||||||||||||||||||
| if (variant === "positive" && maxExamples > 0) | ||||||||||||||||||
| this.#ensureOutputExamples(maxExamples); | ||||||||||||||||||
| return Object.freeze( | ||||||||||||||||||
| variant === "negative" | ||||||||||||||||||
| ? this.#def.resultHandler.getNegativeResponse() | ||||||||||||||||||
|
|
||||||||||||||||||
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.
Shipping the limit feature with
Infinityas the default means v27.4.0 users still hit the combinatorial explosion the CHANGELOG warns about unless they discover and configurelimits. The@todoalready concedes the right default is lower — consider picking it now (e.g.20) and treating the cap-by-default as the breaking change it effectively is in v28, rather than landing a disarmed guardrail.