[OpenAPI Type Generator] Exit with error if a $ref contains a URL (#182175)

## Summary

Closes https://github.com/elastic/kibana/issues/181948

We do not support URLs in `$ref` fields but we were previously exiting
with success and generating an invalid .ts file. This makes the error
more explicit.

I have also removed the reference which drew our attention to this bug.

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Pablo Machado <pablo.nevesmachado@elastic.co>
This commit is contained in:
Mark Hopkin 2024-05-02 10:42:31 +01:00 committed by GitHub
parent b40b6d81f2
commit c49cb1fa9e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 16 additions and 6 deletions

View file

@ -20,9 +20,21 @@ const hasRef = (obj: unknown): obj is NormalizedReferenceObject => {
return typeof obj === 'object' && obj !== null && '$ref' in obj;
};
const stringIsUrl = (str: string) => {
try {
new URL(str);
return true;
} catch {
return false;
}
};
export function normalizeSchema(schema: OpenAPIV3.Document) {
traverseObject(schema, (element) => {
if (hasRef(element)) {
if (stringIsUrl(element.$ref)) {
throw new Error(`URL references are not supported: ${element.$ref}`);
}
const referenceName = element.$ref.split('/').pop();
if (!referenceName) {
throw new Error(`Cannot parse reference name: ${element.$ref}`);

View file

@ -29,10 +29,10 @@ export type DataViewId = z.infer<typeof DataViewId>;
export const DataViewId = z.string();
/**
* An elasticsearch DSL filter object. Used to filter the risk inputs involved, which implicitly filters the risk scores themselves.
* An elasticsearch DSL filter object. Used to filter the risk inputs involved, which implicitly filters the risk scores themselves. See https://cloud.elastic.co/api/v1/api-docs/spec.json#/definitions/QueryContainer
*/
export type Filter = z.infer<typeof Filter>;
export const Filter = z.unknown();
export const Filter = z.object({});
/**
* Specifies how many scores will be involved in a given calculation. Note that this value is per `identifier_type`, i.e. a value of 10 will calculate 10 host scores and 10 user scores, if available. To avoid missed data, keep this value consistent while paginating through scores.

View file

@ -32,10 +32,8 @@ components:
type: string
Filter:
description: An elasticsearch DSL filter object. Used to filter the risk inputs involved, which implicitly filters the risk scores themselves.
# TODO Fix the following line reference. Issue: https://github.com/elastic/kibana/issues/181948
# $ref: 'https://cloud.elastic.co/api/v1/api-docs/spec.json#/definitions/QueryContainer'
type: object
description: An elasticsearch DSL filter object. Used to filter the risk inputs involved, which implicitly filters the risk scores themselves. See https://cloud.elastic.co/api/v1/api-docs/spec.json#/definitions/QueryContainer
PageSize:
description: Specifies how many scores will be involved in a given calculation. Note that this value is per `identifier_type`, i.e. a value of 10 will calculate 10 host scores and 10 user scores, if available. To avoid missed data, keep this value consistent while paginating through scores.