-
Notifications
You must be signed in to change notification settings - Fork 8.1k
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
[OAS] Support lazy runtime types #184000
[OAS] Support lazy runtime types #184000
Conversation
/ci |
const id = 'recursive'; | ||
const object: Type<RecursiveType> = schema.object( | ||
{ | ||
name: schema.string(), | ||
self: lazy<RecursiveType>(id), | ||
}, | ||
{ meta: { id } } | ||
); |
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's a bit verbose, I would have liked to do something like Zod but with the way Joi.link
works this just seemed simplest.
/ci |
.object() | ||
.keys(schemaKeys) | ||
.default() | ||
.optional() | ||
.unknown(unknowns === 'allow') | ||
.options({ stripUnknown: { objects: unknowns === 'ignore' } }); | ||
|
||
if (options.meta?.id) { | ||
schema = schema.id(options.meta.id); |
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.
Note:
- To keep things simple,
id
is only supported for object types, for now - Since using the
joi
's built-in.id()
mechanism we can actually delete quite a bit of code from the downstream OAS generation becausejoi-to-json
understands it and correctly extracts references for us
/ci |
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.
Added a test/snapshot, but mostly I was able to delete code from kbn-router-to-openapispec
it('can convert and record refs', () => { | ||
const ctx = createCtx({ refs: true }); | ||
const obj = schema.object({}, { meta: { id: 'foo' } }); | ||
const parsed = joi2JsonInternal(obj.getSchema()); | ||
const result = ctx.processRef(parsed); | ||
expect(result).toEqual({ $ref: '#/components/schemas/foo' }); | ||
expect(ctx.sharedSchemas.get('foo')).toMatchObject({ type: 'object', properties: {} }); | ||
expect(metaFields.META_FIELD_X_OAS_REF_ID in ctx.sharedSchemas.get('foo')!).toBe(false); | ||
}); |
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.
This is handled by joi-to-json
now.
Pinging @elastic/kibana-core (Team:Core) |
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.
LGTM, great to finally have recursive types/schemas being supported by config-schema!
packages/kbn-router-to-openapispec/src/oas_converter/kbn_config_schema/parse.ts
Outdated
Show resolved
Hide resolved
|
||
describe('lazy', () => { | ||
const id = 'recursive'; | ||
const object: Type<RecursiveType> = schema.object( |
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.
As discussed: the explicit Type<RecursiveType>
typing can be removed 🚀
💚 Build Succeeded
Metrics [docs]Module Count
Public APIs missing comments
Async chunks
Public APIs missing exports
History
To update your PR or re-run it, just comment with: cc @jloleysens |
## Summary Close elastic#182910 Add the ability to declare recursive schemas. Updates `@kbn/config-schema` to support recursive types. This design follows the underlying pattern provided by Joi: https://joi.dev/api/?v=17.13.0#linkref: ```ts const id = 'recursive'; const recursiveSchema: Type<RecursiveType> = schema.object( { name: schema.string(), self: schema.lazy<RecursiveType>(id), }, { meta: { id } } ); ``` Since using the `.link` API we are also using `.id` which enables us to leverage this mechanism OOTB with `joi-to-json` for OAS generation (thus we could delete a lot of code there). I chose to avoid using `id` originally because I thought it would be simpler if we control more of the conversion in config-schema's case but for recursive schemas and references I think this is a favourable trade off. Open to other ideas! --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
## Summary Per the title. Related elastic#184000
Summary
Close #182910
Add the ability to declare recursive schemas. Updates
@kbn/config-schema
to support recursive types. This design follows the underlying pattern provided by Joi: https://joi.dev/api/?v=17.13.0#linkref:Since using the
.link
API we are also using.id
which enables us to leverage this mechanism OOTB withjoi-to-json
for OAS generation (thus we could delete a lot of code there).I chose to avoid using
id
originally because I thought it would be simpler if we control more of the conversion in config-schema's case but for recursive schemas and references I think this is a favourable trade off. Open to other ideas!