-
-
Notifications
You must be signed in to change notification settings - Fork 38
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 13 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 |
|---|---|---|
|
|
@@ -109,11 +109,27 @@ export const isSchema = <T extends z.core.$ZodType = z.core.$ZodType>( | |
| "_zod" in subject && | ||
| (type ? R.path(["_zod", "def", "type"], subject) === type : true); | ||
|
|
||
| /** @todo set to 20 in v28 to avoid too many combinations */ | ||
| export const defaultMaxCombinations = Infinity; | ||
|
Comment on lines
+112
to
+113
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. Shipping the limit feature with |
||
|
|
||
| /** Configurable replacement for R.xprod(), but it also handles empty arrays */ | ||
| export const combinations = <T>( | ||
| a: T[], | ||
| b: T[], | ||
| merge: (pair: [T, T]) => T, | ||
| ): T[] => (a.length && b.length ? R.xprod(a, b).map(merge) : a.concat(b)); | ||
| left: T[], | ||
| right: T[], | ||
| /** @desc The function that combines elements */ | ||
| merge: (a: T, b: T) => T, | ||
| /** @desc Maximum number of combinations */ | ||
| limit = defaultMaxCombinations, | ||
| ): T[] => { | ||
| if (!(limit > 0)) return []; | ||
| if (!left.length || !right.length) return left.concat(right).slice(0, limit); | ||
| const result: T[] = []; | ||
| for (let idxL = 0; idxL < left.length && result.length < limit; idxL++) { | ||
| for (let idxR = 0; idxR < right.length && result.length < limit; idxR++) | ||
| result.push(merge(left[idxL], right[idxR])); | ||
| } | ||
| return result; | ||
| }; | ||
|
|
||
| export const ucFirst = (subject: string) => | ||
| subject.charAt(0).toUpperCase() + subject.slice(1).toLowerCase(); | ||
|
|
||
| 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,20 @@ interface DocumentationParams { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @example { users: "About users", files: { description: "About files", url: "https://example.com" } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tags?: Parameters<typeof depictTags>[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| limits?: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @desc Limits the number of examples | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @default defaultMaxCombinations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @see defaultMaxCombinations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| examples?: number; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @desc Limits the number of security schemas combinations. Must be at least 1. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @default defaultMaxCombinations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @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 +176,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 +189,7 @@ export class Documentation extends OpenApiBuilder { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| endpoint, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| composition, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| brandHandling, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| maxExamples, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| makeRef: this.#makeRef.bind(this), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { description, shortDescription, scopes, inputSchema } = endpoint; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -189,12 +206,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 +222,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 +260,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() | ||||||||||||||||||
|
|
||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.