1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-23 00:22:19 +01:00

refactor: use the same variant schema for the admin and client APIs (#1882)

This commit is contained in:
olav 2022-08-08 16:21:57 +02:00 committed by GitHub
parent df910cc20c
commit f846b8da18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 45 additions and 102 deletions

View File

@ -1,5 +1,4 @@
import { OpenAPIV3 } from 'openapi-types';
import { addonParameterSchema } from './spec/addon-parameter-schema';
import { addonSchema } from './spec/addon-schema';
import { addonsSchema } from './spec/addons-schema';
@ -15,7 +14,6 @@ import { clientFeatureSchema } from './spec/client-feature-schema';
import { clientFeaturesQuerySchema } from './spec/client-features-query-schema';
import { clientFeaturesSchema } from './spec/client-features-schema';
import { clientMetricsSchema } from './spec/client-metrics-schema';
import { clientVariantSchema } from './spec/client-variant-schema';
import { cloneFeatureSchema } from './spec/clone-feature-schema';
import { constraintSchema } from './spec/constraint-schema';
import { contextFieldSchema } from './spec/context-field-schema';
@ -101,7 +99,6 @@ import { validateTagTypeSchema } from './spec/validate-tag-type-schema';
import { variantSchema } from './spec/variant-schema';
import { variantsSchema } from './spec/variants-schema';
import { versionSchema } from './spec/version-schema';
import { IServerOption } from '../types';
import { URL } from 'url';
import { groupSchema } from './spec/group-schema';
@ -126,7 +123,6 @@ export const schemas = {
clientFeaturesQuerySchema,
clientFeaturesSchema,
clientMetricsSchema,
clientVariantSchema,
cloneFeatureSchema,
constraintSchema,
contextFieldSchema,

View File

@ -17,7 +17,7 @@ Object {
}
`;
exports[`featureSchema overrides 1`] = `
exports[`featureSchema variant override values must be an array 1`] = `
Object {
"errors": Array [
Object {

View File

@ -2,7 +2,8 @@ import { FromSchema } from 'json-schema-to-ts';
import { constraintSchema } from './constraint-schema';
import { parametersSchema } from './parameters-schema';
import { featureStrategySchema } from './feature-strategy-schema';
import { clientVariantSchema } from './client-variant-schema';
import { variantSchema } from './variant-schema';
import { overrideSchema } from './override-schema';
export const clientFeatureSchema = {
$id: '#/components/schemas/clientFeatureSchema',
@ -52,7 +53,7 @@ export const clientFeatureSchema = {
variants: {
type: 'array',
items: {
$ref: '#/components/schemas/clientVariantSchema',
$ref: '#/components/schemas/variantSchema',
},
nullable: true,
},
@ -62,7 +63,8 @@ export const clientFeatureSchema = {
constraintSchema,
parametersSchema,
featureStrategySchema,
clientVariantSchema,
variantSchema,
overrideSchema,
},
},
} as const;

View File

@ -22,6 +22,16 @@ test('clientFeaturesSchema required fields', () => {
weight: 1,
weightType: 'b',
stickiness: 'c',
payload: {
type: 'a',
value: 'b',
},
overrides: [
{
contextName: 'a',
values: ['b'],
},
],
},
],
},

View File

@ -7,7 +7,7 @@ import { overrideSchema } from './override-schema';
import { parametersSchema } from './parameters-schema';
import { featureStrategySchema } from './feature-strategy-schema';
import { clientFeatureSchema } from './client-feature-schema';
import { clientVariantSchema } from './client-variant-schema';
import { variantSchema } from './variant-schema';
export const clientFeaturesSchema = {
$id: '#/components/schemas/clientFeaturesSchema',
@ -43,7 +43,7 @@ export const clientFeaturesSchema = {
overrideSchema,
parametersSchema,
featureStrategySchema,
clientVariantSchema,
variantSchema,
},
},
} as const;

View File

@ -1,37 +0,0 @@
import { FromSchema } from 'json-schema-to-ts';
export const clientVariantSchema = {
$id: '#/components/schemas/clientVariantSchema',
type: 'object',
additionalProperties: false,
required: ['name', 'weight'],
properties: {
name: {
type: 'string',
},
weight: {
type: 'number',
},
weightType: {
type: 'string',
},
stickiness: {
type: 'string',
},
payload: {
type: 'object',
required: ['type', 'value'],
properties: {
type: {
type: 'string',
},
value: {
type: 'string',
},
},
},
},
components: {},
} as const;
export type ClientVariantSchema = FromSchema<typeof clientVariantSchema>;

View File

@ -52,7 +52,23 @@ test('featureSchema constraints', () => {
).toMatchSnapshot();
});
test('featureSchema overrides', () => {
test('featureSchema variants should only have a few required fields', () => {
const data = {
name: 'a',
variants: [
{
name: 'a',
weight: 1,
},
],
};
expect(
validateSchema('#/components/schemas/featureSchema', data),
).toBeUndefined();
});
test('featureSchema variant override values must be an array', () => {
const data = {
name: 'a',
variants: [

View File

@ -5,7 +5,7 @@ export const variantSchema = {
$id: '#/components/schemas/variantSchema',
type: 'object',
additionalProperties: false,
required: ['name', 'weight', 'weightType', 'stickiness'],
required: ['name', 'weight'],
properties: {
name: {
type: 'string',

View File

@ -7,9 +7,11 @@ import {
} from '../../helpers/test-helper';
import getLogger from '../../../fixtures/no-logger';
import { DEFAULT_ENV } from '../../../../lib/util/constants';
import { FeatureSchema } from '../../../../lib/openapi/spec/feature-schema';
import { VariantSchema } from '../../../../lib/openapi/spec/variant-schema';
import { FeatureStrategySchema } from '../../../../lib/openapi/spec/feature-strategy-schema';
import {
FeatureToggleDTO,
IStrategyConfig,
IVariant,
} from '../../../../lib/types/model';
let app: IUnleashTest;
let db: ITestDb;
@ -25,8 +27,8 @@ beforeAll(async () => {
app = await setupApp(db.stores);
const createToggle = async (
toggle: Omit<FeatureSchema, 'archivedAt' | 'createdAt'>,
strategy: Omit<FeatureStrategySchema, 'id'> = defaultStrategy,
toggle: FeatureToggleDTO,
strategy: Omit<IStrategyConfig, 'id'> = defaultStrategy,
projectId: string = 'default',
username: string = 'test',
) => {
@ -43,7 +45,7 @@ beforeAll(async () => {
};
const createVariants = async (
featureName: string,
variants: VariantSchema[],
variants: IVariant[],
projectId: string = 'default',
username: string = 'test',
) => {
@ -58,14 +60,12 @@ beforeAll(async () => {
await createToggle({
name: 'featureX',
description: 'the #1 feature',
project: 'some-project',
});
await createToggle(
{
name: 'featureY',
description: 'soon to be the #1 feature',
project: 'some-project',
},
{
name: 'baz',
@ -80,7 +80,6 @@ beforeAll(async () => {
{
name: 'featureZ',
description: 'terrible feature',
project: 'some-project',
},
{
name: 'baz',
@ -95,7 +94,6 @@ beforeAll(async () => {
{
name: 'featureArchivedX',
description: 'the #1 feature',
project: 'some-project',
},
{
name: 'default',
@ -113,7 +111,6 @@ beforeAll(async () => {
{
name: 'featureArchivedY',
description: 'soon to be the #1 feature',
project: 'some-project',
},
{
name: 'baz',
@ -133,7 +130,6 @@ beforeAll(async () => {
{
name: 'featureArchivedZ',
description: 'terrible feature',
project: 'some-project',
},
{
name: 'baz',
@ -152,7 +148,6 @@ beforeAll(async () => {
await createToggle({
name: 'feature.with.variants',
description: 'A feature toggle with variants',
project: 'some-project',
});
await createVariants('feature.with.variants', [
{

View File

@ -513,7 +513,7 @@ Object {
},
"variants": Object {
"items": Object {
"$ref": "#/components/schemas/clientVariantSchema",
"$ref": "#/components/schemas/variantSchema",
},
"nullable": true,
"type": "array",
@ -639,43 +639,6 @@ Object {
],
"type": "object",
},
"clientVariantSchema": Object {
"additionalProperties": false,
"properties": Object {
"name": Object {
"type": "string",
},
"payload": Object {
"properties": Object {
"type": Object {
"type": "string",
},
"value": Object {
"type": "string",
},
},
"required": Array [
"type",
"value",
],
"type": "object",
},
"stickiness": Object {
"type": "string",
},
"weight": Object {
"type": "number",
},
"weightType": Object {
"type": "string",
},
},
"required": Array [
"name",
"weight",
],
"type": "object",
},
"cloneFeatureSchema": Object {
"properties": Object {
"name": Object {
@ -3083,8 +3046,6 @@ Object {
"required": Array [
"name",
"weight",
"weightType",
"stickiness",
],
"type": "object",
},