Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
# Backport This will backport the following commits from `main` to `8.14`: - [[HTTP/OAS] Lazy response schemas (#181622)](#181622) <!--- Backport version: 9.4.3 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Jean-Louis Leysens","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-04-29T09:22:44Z","message":"[HTTP/OAS] Lazy response schemas (#181622)\n\n## Summary\r\n\r\nBased on the introduction of new response schemas for OAS generation we\r\nare going to start the long tail of introducing missing response (`joi`)\r\nschemas. We have roughly 520 known public APIs, most of which do not\r\nhave response schemas defined. We expected a fairly large increase in\r\n`@kbn/config-schema` definitions in the coming weeks/months. Regardless\r\nof actual outcome and given how slow schema instantiation is, this\r\npresents a slight concern for startup time.\r\n\r\n## Proposed changes\r\n\r\nGive consumers guidance and a way to pass in validation lazily. Under\r\nthe hood we make sure that the lazy schemas only get called once.\r\n\r\n```ts\r\n\r\n/**\r\n * A validation schema factory.\r\n *\r\n * @note Used to lazily create schemas that are otherwise not needed\r\n * @note Assume this function will only be called once\r\n *\r\n * @return A @kbn/config-schema schema\r\n * @public\r\n */\r\nexport type LazyValidator = () => Type<unknown>;\r\n\r\n/** @public */\r\nexport interface VersionedRouteCustomResponseBodyValidation {\r\n /** A custom validation function */\r\n custom: RouteValidationFunction<unknown>;\r\n}\r\n\r\n/** @public */\r\nexport type VersionedResponseBodyValidation =\r\n | LazyValidator\r\n | VersionedRouteCustomResponseBodyValidation;\r\n\r\n/**\r\n * Map of response status codes to response schemas\r\n *\r\n * @note Instantiating response schemas is expensive, especially when it is\r\n * not needed in most cases. See example below to ensure this is lazily\r\n * provided.\r\n *\r\n * @note The {@link TypeOf} type utility from @kbn/config-schema can extract\r\n * types from lazily created schemas\r\n *\r\n * @example\r\n * ```ts\r\n * // Avoid this:\r\n * const badResponseSchema = schema.object({ foo: foo.string() });\r\n * // Do this:\r\n * const goodResponseSchema = () => schema.object({ foo: foo.string() });\r\n *\r\n * type ResponseType = TypeOf<typeof goodResponseSchema>;\r\n * ...\r\n * .addVersion(\r\n * { ... validation: { response: { 200: { body: goodResponseSchema } } } },\r\n * handlerFn\r\n * )\r\n * ...\r\n * ```\r\n * @public\r\n */\r\nexport interface VersionedRouteResponseValidation {\r\n [statusCode: number]: {\r\n body: VersionedResponseBodyValidation;\r\n };\r\n unsafe?: { body?: boolean };\r\n}\r\n```\r\n\r\n## Notes\r\n\r\n* Expected (worst case) in low resource environments is an additional\r\n1.5 seconds to start up time and additional ~70MB to memory pressure\r\nwhich is not a great trade-off for functionality that is only used when\r\nOAS generation is on.\r\n\r\nRelated https://github.com/elastic/kibana/pull/181277","sha":"97e1d9f4b8b816d45b31a9caa0feca9bbb9c3bd8","branchLabelMapping":{"^v8.15.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:http","Team:Core","release_note:skip","ci:project-deploy-observability","v8.14.0","v8.15.0"],"title":"[HTTP/OAS] Lazy response schemas","number":181622,"url":"https://github.com/elastic/kibana/pull/181622","mergeCommit":{"message":"[HTTP/OAS] Lazy response schemas (#181622)\n\n## Summary\r\n\r\nBased on the introduction of new response schemas for OAS generation we\r\nare going to start the long tail of introducing missing response (`joi`)\r\nschemas. We have roughly 520 known public APIs, most of which do not\r\nhave response schemas defined. We expected a fairly large increase in\r\n`@kbn/config-schema` definitions in the coming weeks/months. Regardless\r\nof actual outcome and given how slow schema instantiation is, this\r\npresents a slight concern for startup time.\r\n\r\n## Proposed changes\r\n\r\nGive consumers guidance and a way to pass in validation lazily. Under\r\nthe hood we make sure that the lazy schemas only get called once.\r\n\r\n```ts\r\n\r\n/**\r\n * A validation schema factory.\r\n *\r\n * @note Used to lazily create schemas that are otherwise not needed\r\n * @note Assume this function will only be called once\r\n *\r\n * @return A @kbn/config-schema schema\r\n * @public\r\n */\r\nexport type LazyValidator = () => Type<unknown>;\r\n\r\n/** @public */\r\nexport interface VersionedRouteCustomResponseBodyValidation {\r\n /** A custom validation function */\r\n custom: RouteValidationFunction<unknown>;\r\n}\r\n\r\n/** @public */\r\nexport type VersionedResponseBodyValidation =\r\n | LazyValidator\r\n | VersionedRouteCustomResponseBodyValidation;\r\n\r\n/**\r\n * Map of response status codes to response schemas\r\n *\r\n * @note Instantiating response schemas is expensive, especially when it is\r\n * not needed in most cases. See example below to ensure this is lazily\r\n * provided.\r\n *\r\n * @note The {@link TypeOf} type utility from @kbn/config-schema can extract\r\n * types from lazily created schemas\r\n *\r\n * @example\r\n * ```ts\r\n * // Avoid this:\r\n * const badResponseSchema = schema.object({ foo: foo.string() });\r\n * // Do this:\r\n * const goodResponseSchema = () => schema.object({ foo: foo.string() });\r\n *\r\n * type ResponseType = TypeOf<typeof goodResponseSchema>;\r\n * ...\r\n * .addVersion(\r\n * { ... validation: { response: { 200: { body: goodResponseSchema } } } },\r\n * handlerFn\r\n * )\r\n * ...\r\n * ```\r\n * @public\r\n */\r\nexport interface VersionedRouteResponseValidation {\r\n [statusCode: number]: {\r\n body: VersionedResponseBodyValidation;\r\n };\r\n unsafe?: { body?: boolean };\r\n}\r\n```\r\n\r\n## Notes\r\n\r\n* Expected (worst case) in low resource environments is an additional\r\n1.5 seconds to start up time and additional ~70MB to memory pressure\r\nwhich is not a great trade-off for functionality that is only used when\r\nOAS generation is on.\r\n\r\nRelated https://github.com/elastic/kibana/pull/181277","sha":"97e1d9f4b8b816d45b31a9caa0feca9bbb9c3bd8"}},"sourceBranch":"main","suggestedTargetBranches":["8.14"],"targetPullRequestStates":[{"branch":"8.14","label":"v8.14.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.15.0","branchLabelMappingKey":"^v8.15.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/181622","number":181622,"mergeCommit":{"message":"[HTTP/OAS] Lazy response schemas (#181622)\n\n## Summary\r\n\r\nBased on the introduction of new response schemas for OAS generation we\r\nare going to start the long tail of introducing missing response (`joi`)\r\nschemas. We have roughly 520 known public APIs, most of which do not\r\nhave response schemas defined. We expected a fairly large increase in\r\n`@kbn/config-schema` definitions in the coming weeks/months. Regardless\r\nof actual outcome and given how slow schema instantiation is, this\r\npresents a slight concern for startup time.\r\n\r\n## Proposed changes\r\n\r\nGive consumers guidance and a way to pass in validation lazily. Under\r\nthe hood we make sure that the lazy schemas only get called once.\r\n\r\n```ts\r\n\r\n/**\r\n * A validation schema factory.\r\n *\r\n * @note Used to lazily create schemas that are otherwise not needed\r\n * @note Assume this function will only be called once\r\n *\r\n * @return A @kbn/config-schema schema\r\n * @public\r\n */\r\nexport type LazyValidator = () => Type<unknown>;\r\n\r\n/** @public */\r\nexport interface VersionedRouteCustomResponseBodyValidation {\r\n /** A custom validation function */\r\n custom: RouteValidationFunction<unknown>;\r\n}\r\n\r\n/** @public */\r\nexport type VersionedResponseBodyValidation =\r\n | LazyValidator\r\n | VersionedRouteCustomResponseBodyValidation;\r\n\r\n/**\r\n * Map of response status codes to response schemas\r\n *\r\n * @note Instantiating response schemas is expensive, especially when it is\r\n * not needed in most cases. See example below to ensure this is lazily\r\n * provided.\r\n *\r\n * @note The {@link TypeOf} type utility from @kbn/config-schema can extract\r\n * types from lazily created schemas\r\n *\r\n * @example\r\n * ```ts\r\n * // Avoid this:\r\n * const badResponseSchema = schema.object({ foo: foo.string() });\r\n * // Do this:\r\n * const goodResponseSchema = () => schema.object({ foo: foo.string() });\r\n *\r\n * type ResponseType = TypeOf<typeof goodResponseSchema>;\r\n * ...\r\n * .addVersion(\r\n * { ... validation: { response: { 200: { body: goodResponseSchema } } } },\r\n * handlerFn\r\n * )\r\n * ...\r\n * ```\r\n * @public\r\n */\r\nexport interface VersionedRouteResponseValidation {\r\n [statusCode: number]: {\r\n body: VersionedResponseBodyValidation;\r\n };\r\n unsafe?: { body?: boolean };\r\n}\r\n```\r\n\r\n## Notes\r\n\r\n* Expected (worst case) in low resource environments is an additional\r\n1.5 seconds to start up time and additional ~70MB to memory pressure\r\nwhich is not a great trade-off for functionality that is only used when\r\nOAS generation is on.\r\n\r\nRelated https://github.com/elastic/kibana/pull/181277","sha":"97e1d9f4b8b816d45b31a9caa0feca9bbb9c3bd8"}}]}] BACKPORT--> Co-authored-by: Jean-Louis Leysens <[email protected]>
- Loading branch information