From 1481c13b61b729557960e99049fb6747f3713919 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Mon, 31 Jul 2023 11:04:13 +0200 Subject: [PATCH] openapi: enable strict schema validation by default and fix (#4355) Enable strict schema validation by default. It can still be overridden by explicitly setting it to false. I've also fixed the validation errors that appeared when turning it on. I've opted for the simplest route and changed the schemas to comply with the tests. --- .../token-user-schema.test.ts.snap | 9 -------- .../spec/public-signup-token-schema.ts | 1 + src/lib/openapi/spec/token-user-schema.ts | 2 +- src/lib/openapi/spec/user-schema.ts | 1 + src/lib/routes/admin-api/tag-type.ts | 22 ++++++++++++------- src/test/e2e/helpers/test-helper.ts | 11 +++++++++- 6 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/lib/openapi/spec/__snapshots__/token-user-schema.test.ts.snap b/src/lib/openapi/spec/__snapshots__/token-user-schema.test.ts.snap index 553e7ce0c4..e0a4f10e1b 100644 --- a/src/lib/openapi/spec/__snapshots__/token-user-schema.test.ts.snap +++ b/src/lib/openapi/spec/__snapshots__/token-user-schema.test.ts.snap @@ -12,15 +12,6 @@ exports[`tokenUserSchema 1`] = ` }, "schemaPath": "#/required", }, - { - "instancePath": "", - "keyword": "required", - "message": "must have required property 'name'", - "params": { - "missingProperty": "name", - }, - "schemaPath": "#/required", - }, { "instancePath": "", "keyword": "required", diff --git a/src/lib/openapi/spec/public-signup-token-schema.ts b/src/lib/openapi/spec/public-signup-token-schema.ts index 1561d276a3..e24fa1bc28 100644 --- a/src/lib/openapi/spec/public-signup-token-schema.ts +++ b/src/lib/openapi/spec/public-signup-token-schema.ts @@ -29,6 +29,7 @@ export const publicSignupTokenSchema = { description: 'The public signup link for the token. Users who follow this link will be taken to a signup page where they can create an Unleash user.', type: 'string', + nullable: true, example: 'https://sandbox.getunleash.io/enterprise/new-user?invite=a3c84b25409ea8ca1782ef17f94a42fc', }, diff --git a/src/lib/openapi/spec/token-user-schema.ts b/src/lib/openapi/spec/token-user-schema.ts index 48cda4d22c..9b2460fe53 100644 --- a/src/lib/openapi/spec/token-user-schema.ts +++ b/src/lib/openapi/spec/token-user-schema.ts @@ -6,7 +6,7 @@ export const tokenUserSchema = { type: 'object', additionalProperties: false, description: 'A user identified by a token', - required: ['id', 'name', 'email', 'token', 'createdBy', 'role'], + required: ['id', 'email', 'token', 'createdBy', 'role'], properties: { id: { type: 'integer', diff --git a/src/lib/openapi/spec/user-schema.ts b/src/lib/openapi/spec/user-schema.ts index 005d13bbcc..8a1cd3f230 100644 --- a/src/lib/openapi/spec/user-schema.ts +++ b/src/lib/openapi/spec/user-schema.ts @@ -25,6 +25,7 @@ export const userSchema = { description: 'Name of the user', type: 'string', example: 'User', + nullable: true, }, email: { description: 'Email of the user', diff --git a/src/lib/routes/admin-api/tag-type.ts b/src/lib/routes/admin-api/tag-type.ts index 5ebbbf5659..65e9c9c049 100644 --- a/src/lib/routes/admin-api/tag-type.ts +++ b/src/lib/routes/admin-api/tag-type.ts @@ -18,11 +18,11 @@ import { resourceCreatedResponseSchema, } from '../../openapi/util/create-response-schema'; import { TagTypesSchema } from '../../openapi/spec/tag-types-schema'; -import { ValidateTagTypeSchema } from '../../openapi/spec/validate-tag-type-schema'; import { - tagTypeSchema, - TagTypeSchema, -} from '../../openapi/spec/tag-type-schema'; + validateTagTypeSchema, + ValidateTagTypeSchema, +} from '../../openapi/spec/validate-tag-type-schema'; +import { TagTypeSchema } from '../../openapi/spec/tag-type-schema'; import { UpdateTagTypeSchema } from '../../openapi/spec/update-tag-type-schema'; import { OpenApiService } from '../../services/openapi-service'; import { @@ -180,10 +180,16 @@ class TagTypeController extends Controller { res: Response, ): Promise { await this.tagTypeService.validate(req.body); - this.openApiService.respondWithValidation(200, res, tagTypeSchema.$id, { - valid: true, - tagType: req.body, - }); + + this.openApiService.respondWithValidation( + 200, + res, + validateTagTypeSchema.$id, + { + valid: true, + tagType: req.body, + }, + ); } async createTagType( diff --git a/src/test/e2e/helpers/test-helper.ts b/src/test/e2e/helpers/test-helper.ts index 7d8aa6899d..7fbf6ee7c3 100644 --- a/src/test/e2e/helpers/test-helper.ts +++ b/src/test/e2e/helpers/test-helper.ts @@ -179,7 +179,16 @@ async function createApp( server: { unleashUrl: 'http://localhost:4242', }, - ...customOptions, + ...{ + ...customOptions, + experimental: { + ...(customOptions?.experimental ?? {}), + flags: { + strictSchemaValidation: true, + ...(customOptions?.experimental?.flags ?? {}), + }, + }, + }, }); const services = createServices(stores, config, db); const unleashSession = sessionDb(config, undefined);