1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-09-05 17:53:12 +02:00

Fix: remove recursive schema inclusion

This commit is contained in:
Thomas Heartman 2022-06-29 11:03:09 +02:00
parent aa6708f7e8
commit 69c878a40f
5 changed files with 49 additions and 40 deletions

View File

@ -1,15 +0,0 @@
import { includeSchemasRecursively } from './nested-schemas';
test('includeSchemasRecursively', () => {
const schemaFour = { components: {} };
const schemaThree = { components: { schemas: { schemaFour } } };
const schemaOne = {
components: { schemas: { schemaTwo: schemaFour, schemaThree } },
};
expect(includeSchemasRecursively({ schemaOne })).toEqual({
schemaOne,
schemaTwo: schemaFour,
schemaThree,
schemaFour,
});
});

View File

@ -1,19 +0,0 @@
export interface ISchemaObject {
[k: string]: IComponentSchema;
}
interface IComponentSchema {
components: { schemas?: ISchemaObject };
}
export const includeSchemasRecursively = (
schemas: ISchemaObject,
): ISchemaObject =>
Object.entries(schemas).reduce<ISchemaObject>(
(acc: ISchemaObject, [key, value]) => ({
...acc,
[key]: value,
...includeSchemasRecursively(value.components.schemas ?? {}),
}),
{},
);

View File

@ -34,3 +34,44 @@ test('eventsSchema', () => {
validateSchema('#/components/schemas/eventsSchema', data), validateSchema('#/components/schemas/eventsSchema', data),
).toBeUndefined(); ).toBeUndefined();
}); });
test('eventsSchema types', () => {
const data: EventsSchema = {
version: 1,
events: [
{
// @ts-expect-error
id: '1',
type: 'feature-created',
createdBy: 'user@company.com',
createdAt: '2022-05-31T13:32:20.560Z',
data: {
name: 'new-feature',
description: 'Toggle description',
type: 'release',
project: 'my-project',
stale: false,
variants: [],
createdAt: '2022-05-31T13:32:20.547Z',
lastSeenAt: null,
impressionData: true,
},
preData: null,
tags: [
{
type: '',
// @ts-expect-error
value: 1,
},
],
featureName: 'new-feature',
project: 'my-project',
environment: null,
},
],
};
expect(
validateSchema('#/components/schemas/eventsSchema', data),
).not.toBeUndefined();
});

View File

@ -1,6 +1,6 @@
import { FromSchema } from 'json-schema-to-ts'; import { FromSchema } from 'json-schema-to-ts';
import { eventSchema } from './event-schema'; import { eventSchema } from './event-schema';
import { includeSchemasRecursively } from '../nested-schemas'; import { tagSchema } from './tag-schema';
export const eventsSchema = { export const eventsSchema = {
$id: '#/components/schemas/eventsSchema', $id: '#/components/schemas/eventsSchema',
@ -18,9 +18,10 @@ export const eventsSchema = {
}, },
}, },
components: { components: {
schemas: includeSchemasRecursively({ schemas: {
eventSchema, eventSchema,
}), tagSchema,
},
}, },
} as const; } as const;

View File

@ -1,6 +1,6 @@
import { FromSchema } from 'json-schema-to-ts'; import { FromSchema } from 'json-schema-to-ts';
import { includeSchemasRecursively } from '../nested-schemas';
import { eventSchema } from './event-schema'; import { eventSchema } from './event-schema';
import { tagSchema } from './tag-schema';
export const featureEventsSchema = { export const featureEventsSchema = {
$id: '#/components/schemas/featureEventsSchema', $id: '#/components/schemas/featureEventsSchema',
@ -17,9 +17,10 @@ export const featureEventsSchema = {
}, },
}, },
components: { components: {
schemas: includeSchemasRecursively({ schemas: {
eventSchema, eventSchema,
}), tagSchema,
},
}, },
} as const; } as const;