Skip to content

Commit

Permalink
openapi: enable strict schema validation by default and fix (#4355)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thomasheartman committed Jul 31, 2023
1 parent 799387e commit 1481c13
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 19 deletions.
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/lib/openapi/spec/public-signup-token-schema.ts
Expand Up @@ -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',
},
Expand Down
2 changes: 1 addition & 1 deletion src/lib/openapi/spec/token-user-schema.ts
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions src/lib/openapi/spec/user-schema.ts
Expand Up @@ -25,6 +25,7 @@ export const userSchema = {
description: 'Name of the user',
type: 'string',
example: 'User',
nullable: true,
},
email: {
description: 'Email of the user',
Expand Down
22 changes: 14 additions & 8 deletions src/lib/routes/admin-api/tag-type.ts
Expand Up @@ -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 {
Expand Down Expand Up @@ -180,10 +180,16 @@ class TagTypeController extends Controller {
res: Response<ValidateTagTypeSchema>,
): Promise<void> {
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(
Expand Down
11 changes: 10 additions & 1 deletion src/test/e2e/helpers/test-helper.ts
Expand Up @@ -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);
Expand Down

0 comments on commit 1481c13

Please sign in to comment.