diff --git a/src/lib/openapi/nested-schemas.ts b/src/lib/openapi/nested-schemas.ts new file mode 100644 index 0000000000..4769caeb23 --- /dev/null +++ b/src/lib/openapi/nested-schemas.ts @@ -0,0 +1,16 @@ +interface ISchemaObject { + [k: string]: IComponentSchema; +} + +interface IComponentSchema { + components: { schemas?: ISchemaObject }; +} + +export const includeSchemasRecursively = ( + schemas: ISchemaObject, +): ISchemaObject => + Object.entries(schemas).reduce(([key, value], acc) => ({ + ...acc, + [key]: value, + ...includeSchemasRecursively(value.components.schemas), + })); diff --git a/src/lib/openapi/spec/events-schema.ts b/src/lib/openapi/spec/events-schema.ts index 0fc6a1097d..c1ad6a59bb 100644 --- a/src/lib/openapi/spec/events-schema.ts +++ b/src/lib/openapi/spec/events-schema.ts @@ -1,5 +1,6 @@ import { FromSchema } from 'json-schema-to-ts'; import { eventSchema } from './event-schema'; +import { includeSchemasRecursively } from '../nested-schemas'; export const eventsSchema = { $id: '#/components/schemas/eventsSchema', @@ -17,9 +18,9 @@ export const eventsSchema = { }, }, components: { - schemas: { + schemas: includeSchemasRecursively({ eventSchema, - }, + }), }, } as const; diff --git a/src/lib/openapi/spec/feature-events-schema.ts b/src/lib/openapi/spec/feature-events-schema.ts new file mode 100644 index 0000000000..c60bae58eb --- /dev/null +++ b/src/lib/openapi/spec/feature-events-schema.ts @@ -0,0 +1,26 @@ +import { FromSchema } from 'json-schema-to-ts'; +import { eventSchema } from './event-schema'; + +export const featureEventsSchema = { + $id: '#/components/schemas/featureEventsSchema', + type: 'object', + additionalProperties: false, + required: ['toggleName', 'events'], + properties: { + toggleName: { + type: 'string', + }, + events: { + type: 'array', + items: { $ref: eventSchema.$id }, + }, + }, + components: { + schemas: { + eventSchema, + ...eventSchema.components.schemas, + }, + }, +} as const; + +export type FeatureEventsSchema = FromSchema; diff --git a/src/lib/routes/admin-api/event.ts b/src/lib/routes/admin-api/event.ts index 51149aba58..94d86f03b5 100644 --- a/src/lib/routes/admin-api/event.ts +++ b/src/lib/routes/admin-api/event.ts @@ -7,8 +7,12 @@ import { IEvent } from '../../types/events'; import Controller from '../controller'; import { anonymise } from '../../util/anonymise'; import { OpenApiService } from '../../services/openapi-service'; -import { createResponseSchema } from 'lib/openapi'; -import { eventsSchema, EventsSchema } from 'lib/openapi/spec/events-schema'; +import { createResponseSchema } from '../../../lib/openapi'; +import { + eventsSchema, + EventsSchema, +} from '../../../lib/openapi/spec/events-schema'; +import { serializeDates } from '../../../lib/types/serialize-dates'; const version = 1; export default class EventController extends Controller { @@ -73,7 +77,7 @@ export default class EventController extends Controller { const response: EventsSchema = { version, - events: this.fixEvents(events), + events: serializeDates(this.fixEvents(events)), }; this.openApiService.respondWithValidation( 200,