diff --git a/src/lib/openapi/index.ts b/src/lib/openapi/index.ts index ffe97e1290..3515a3f5e4 100644 --- a/src/lib/openapi/index.ts +++ b/src/lib/openapi/index.ts @@ -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, diff --git a/src/lib/openapi/spec/__snapshots__/feature-schema.test.ts.snap b/src/lib/openapi/spec/__snapshots__/feature-schema.test.ts.snap index 76dc708e3f..50dd2a1b4d 100644 --- a/src/lib/openapi/spec/__snapshots__/feature-schema.test.ts.snap +++ b/src/lib/openapi/spec/__snapshots__/feature-schema.test.ts.snap @@ -17,7 +17,7 @@ Object { } `; -exports[`featureSchema overrides 1`] = ` +exports[`featureSchema variant override values must be an array 1`] = ` Object { "errors": Array [ Object { diff --git a/src/lib/openapi/spec/client-feature-schema.ts b/src/lib/openapi/spec/client-feature-schema.ts index 3b7fe12c28..966d8ff2f8 100644 --- a/src/lib/openapi/spec/client-feature-schema.ts +++ b/src/lib/openapi/spec/client-feature-schema.ts @@ -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; diff --git a/src/lib/openapi/spec/client-features-schema.test.ts b/src/lib/openapi/spec/client-features-schema.test.ts index 620b976ecf..20bf229691 100644 --- a/src/lib/openapi/spec/client-features-schema.test.ts +++ b/src/lib/openapi/spec/client-features-schema.test.ts @@ -22,6 +22,16 @@ test('clientFeaturesSchema required fields', () => { weight: 1, weightType: 'b', stickiness: 'c', + payload: { + type: 'a', + value: 'b', + }, + overrides: [ + { + contextName: 'a', + values: ['b'], + }, + ], }, ], }, diff --git a/src/lib/openapi/spec/client-features-schema.ts b/src/lib/openapi/spec/client-features-schema.ts index e1d62617d2..29d935e64a 100644 --- a/src/lib/openapi/spec/client-features-schema.ts +++ b/src/lib/openapi/spec/client-features-schema.ts @@ -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; diff --git a/src/lib/openapi/spec/client-variant-schema.ts b/src/lib/openapi/spec/client-variant-schema.ts deleted file mode 100644 index 432575e04c..0000000000 --- a/src/lib/openapi/spec/client-variant-schema.ts +++ /dev/null @@ -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; diff --git a/src/lib/openapi/spec/feature-schema.test.ts b/src/lib/openapi/spec/feature-schema.test.ts index 61f2bc2a8e..706051ac2a 100644 --- a/src/lib/openapi/spec/feature-schema.test.ts +++ b/src/lib/openapi/spec/feature-schema.test.ts @@ -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: [ diff --git a/src/lib/openapi/spec/variant-schema.ts b/src/lib/openapi/spec/variant-schema.ts index 65e6880033..8418dc0f8e 100644 --- a/src/lib/openapi/spec/variant-schema.ts +++ b/src/lib/openapi/spec/variant-schema.ts @@ -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', diff --git a/src/test/e2e/api/admin/feature.e2e.test.ts b/src/test/e2e/api/admin/feature.e2e.test.ts index b9501b7b8d..b45e91d8c2 100644 --- a/src/test/e2e/api/admin/feature.e2e.test.ts +++ b/src/test/e2e/api/admin/feature.e2e.test.ts @@ -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, - strategy: Omit = defaultStrategy, + toggle: FeatureToggleDTO, + strategy: Omit = 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', [ { diff --git a/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap b/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap index 804af6d43c..c7e50a41d2 100644 --- a/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap +++ b/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap @@ -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", },