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 { OpenAPIV3 } from 'openapi-types';
import { addonParameterSchema } from './spec/addon-parameter-schema'; import { addonParameterSchema } from './spec/addon-parameter-schema';
import { addonSchema } from './spec/addon-schema'; import { addonSchema } from './spec/addon-schema';
import { addonsSchema } from './spec/addons-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 { clientFeaturesQuerySchema } from './spec/client-features-query-schema';
import { clientFeaturesSchema } from './spec/client-features-schema'; import { clientFeaturesSchema } from './spec/client-features-schema';
import { clientMetricsSchema } from './spec/client-metrics-schema'; import { clientMetricsSchema } from './spec/client-metrics-schema';
import { clientVariantSchema } from './spec/client-variant-schema';
import { cloneFeatureSchema } from './spec/clone-feature-schema'; import { cloneFeatureSchema } from './spec/clone-feature-schema';
import { constraintSchema } from './spec/constraint-schema'; import { constraintSchema } from './spec/constraint-schema';
import { contextFieldSchema } from './spec/context-field-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 { variantSchema } from './spec/variant-schema';
import { variantsSchema } from './spec/variants-schema'; import { variantsSchema } from './spec/variants-schema';
import { versionSchema } from './spec/version-schema'; import { versionSchema } from './spec/version-schema';
import { IServerOption } from '../types'; import { IServerOption } from '../types';
import { URL } from 'url'; import { URL } from 'url';
import { groupSchema } from './spec/group-schema'; import { groupSchema } from './spec/group-schema';
@ -126,7 +123,6 @@ export const schemas = {
clientFeaturesQuerySchema, clientFeaturesQuerySchema,
clientFeaturesSchema, clientFeaturesSchema,
clientMetricsSchema, clientMetricsSchema,
clientVariantSchema,
cloneFeatureSchema, cloneFeatureSchema,
constraintSchema, constraintSchema,
contextFieldSchema, contextFieldSchema,

View File

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

View File

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

View File

@ -22,6 +22,16 @@ test('clientFeaturesSchema required fields', () => {
weight: 1, weight: 1,
weightType: 'b', weightType: 'b',
stickiness: 'c', 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 { parametersSchema } from './parameters-schema';
import { featureStrategySchema } from './feature-strategy-schema'; import { featureStrategySchema } from './feature-strategy-schema';
import { clientFeatureSchema } from './client-feature-schema'; import { clientFeatureSchema } from './client-feature-schema';
import { clientVariantSchema } from './client-variant-schema'; import { variantSchema } from './variant-schema';
export const clientFeaturesSchema = { export const clientFeaturesSchema = {
$id: '#/components/schemas/clientFeaturesSchema', $id: '#/components/schemas/clientFeaturesSchema',
@ -43,7 +43,7 @@ export const clientFeaturesSchema = {
overrideSchema, overrideSchema,
parametersSchema, parametersSchema,
featureStrategySchema, featureStrategySchema,
clientVariantSchema, variantSchema,
}, },
}, },
} as const; } 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(); ).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 = { const data = {
name: 'a', name: 'a',
variants: [ variants: [

View File

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

View File

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

View File

@ -513,7 +513,7 @@ Object {
}, },
"variants": Object { "variants": Object {
"items": Object { "items": Object {
"$ref": "#/components/schemas/clientVariantSchema", "$ref": "#/components/schemas/variantSchema",
}, },
"nullable": true, "nullable": true,
"type": "array", "type": "array",
@ -639,43 +639,6 @@ Object {
], ],
"type": "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 { "cloneFeatureSchema": Object {
"properties": Object { "properties": Object {
"name": Object { "name": Object {
@ -3083,8 +3046,6 @@ Object {
"required": Array [ "required": Array [
"name", "name",
"weight", "weight",
"weightType",
"stickiness",
], ],
"type": "object", "type": "object",
}, },