Skip to content

Commit

Permalink
feat: strategy variant schema openapi (#4232)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew committed Jul 13, 2023
1 parent 8f5bda6 commit ce87806
Show file tree
Hide file tree
Showing 19 changed files with 131 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/lib/openapi/index.ts
Expand Up @@ -148,6 +148,8 @@ import {
versionSchema,
advancedPlaygroundFeatureSchema,
telemetrySettingsSchema,
strategyVariantSchema,
createStrategyVariantSchema,
} from './spec';
import { IServerOption } from '../types';
import { mapValues, omitKeys } from '../util';
Expand Down Expand Up @@ -353,6 +355,8 @@ export const schemas: UnleashSchemas = {
importTogglesValidateItemSchema,
contextFieldStrategiesSchema,
telemetrySettingsSchema,
strategyVariantSchema,
createStrategyVariantSchema,
};

// Remove JSONSchema keys that would result in an invalid OpenAPI spec.
Expand Down
2 changes: 2 additions & 0 deletions src/lib/openapi/spec/client-feature-schema.ts
Expand Up @@ -4,6 +4,7 @@ import { parametersSchema } from './parameters-schema';
import { featureStrategySchema } from './feature-strategy-schema';
import { variantSchema } from './variant-schema';
import { overrideSchema } from './override-schema';
import { strategyVariantSchema } from './strategy-variant-schema';

export const clientFeatureSchema = {
$id: '#/components/schemas/clientFeatureSchema',
Expand Down Expand Up @@ -78,6 +79,7 @@ export const clientFeatureSchema = {
constraintSchema,
parametersSchema,
featureStrategySchema,
strategyVariantSchema,
variantSchema,
overrideSchema,
},
Expand Down
2 changes: 2 additions & 0 deletions src/lib/openapi/spec/client-features-schema.ts
Expand Up @@ -8,6 +8,7 @@ import { parametersSchema } from './parameters-schema';
import { featureStrategySchema } from './feature-strategy-schema';
import { clientFeatureSchema } from './client-feature-schema';
import { variantSchema } from './variant-schema';
import { strategyVariantSchema } from './strategy-variant-schema';

export const clientFeaturesSchema = {
$id: '#/components/schemas/clientFeaturesSchema',
Expand Down Expand Up @@ -54,6 +55,7 @@ export const clientFeaturesSchema = {
overrideSchema,
parametersSchema,
featureStrategySchema,
strategyVariantSchema,
variantSchema,
},
},
Expand Down
9 changes: 9 additions & 0 deletions src/lib/openapi/spec/create-feature-strategy-schema.ts
@@ -1,6 +1,7 @@
import { FromSchema } from 'json-schema-to-ts';
import { parametersSchema } from './parameters-schema';
import { constraintSchema } from './constraint-schema';
import { createStrategyVariantSchema } from './create-strategy-variant-schema';

export const createFeatureStrategySchema = {
$id: '#/components/schemas/createFeatureStrategySchema',
Expand Down Expand Up @@ -48,6 +49,13 @@ export const createFeatureStrategySchema = {
$ref: '#/components/schemas/constraintSchema',
},
},
variants: {
type: 'array',
description: 'Strategy level variants',
items: {
$ref: '#/components/schemas/createStrategyVariantSchema',
},
},
parameters: {
description: 'An object containing the parameters for the strategy',
example: {
Expand All @@ -70,6 +78,7 @@ export const createFeatureStrategySchema = {
schemas: {
constraintSchema,
parametersSchema,
createStrategyVariantSchema,
},
},
} as const;
Expand Down
59 changes: 59 additions & 0 deletions src/lib/openapi/spec/create-strategy-variant-schema.ts
@@ -0,0 +1,59 @@
import { FromSchema } from 'json-schema-to-ts';

export const createStrategyVariantSchema = {
$id: '#/components/schemas/createStrategyVariantSchema',
type: 'object',
additionalProperties: true,
description:
"This is an experimental property. It may change or be removed as we work on it. Please don't depend on it yet. A strategy variant allows you to attach any data to strategies instead of only returning `true`/`false`. Strategy variants take precedence over feature variants.",
required: ['name', 'weight', 'weightType', 'stickiness'],
properties: {
name: {
type: 'string',
description:
'The variant name. Must be unique for this feature toggle',
example: 'blue_group',
},
weight: {
type: 'integer',
description:
'The weight is the likelihood of any one user getting this variant. It is an integer between 0 and 1000. See the section on [variant weights](https://docs.getunleash.io/reference/feature-toggle-variants#variant-weight) for more information',
minimum: 0,
maximum: 1000,
},
weightType: {
description:
'Set to `fix` if this variant must have exactly the weight allocated to it. If the type is `variable`, the weight will adjust so that the total weight of all variants adds up to 1000. Refer to the [variant weight documentation](https://docs.getunleash.io/reference/feature-toggle-variants#variant-weight).',
type: 'string',
example: 'fix',
},
stickiness: {
type: 'string',
description:
'The [stickiness](https://docs.getunleash.io/reference/feature-toggle-variants#variant-stickiness) to use for distribution of this variant. Stickiness is how Unleash guarantees that the same user gets the same variant every time',
example: 'custom.context.field',
},
payload: {
type: 'object',
required: ['type', 'value'],
description: 'Extra data configured for this variant',
properties: {
type: {
description:
'The type of the value. Commonly used types are string, json and csv.',
type: 'string',
},
value: {
description: 'The actual value of payload',
type: 'string',
},
},
example: { type: 'json', value: '{color: red}' },
},
},
components: {},
} as const;

export type CreateStrategyVariantSchema = FromSchema<
typeof createStrategyVariantSchema
>;
2 changes: 2 additions & 0 deletions src/lib/openapi/spec/environment-project-schema.ts
@@ -1,5 +1,6 @@
import { FromSchema } from 'json-schema-to-ts';
import { createFeatureStrategySchema } from './create-feature-strategy-schema';
import { createStrategyVariantSchema } from './create-strategy-variant-schema';

export const environmentProjectSchema = {
$id: '#/components/schemas/environmentProjectSchema',
Expand Down Expand Up @@ -60,6 +61,7 @@ export const environmentProjectSchema = {
components: {
schemas: {
createFeatureStrategySchema,
createStrategyVariantSchema,
},
},
} as const;
Expand Down
2 changes: 2 additions & 0 deletions src/lib/openapi/spec/export-result-schema.ts
Expand Up @@ -11,6 +11,7 @@ import { overrideSchema } from './override-schema';
import { variantsSchema } from './variants-schema';
import { constraintSchema } from './constraint-schema';
import { tagTypeSchema } from './tag-type-schema';
import { strategyVariantSchema } from './strategy-variant-schema';

export const exportResultSchema = {
$id: '#/components/schemas/exportResultSchema',
Expand Down Expand Up @@ -170,6 +171,7 @@ export const exportResultSchema = {
schemas: {
featureSchema,
featureStrategySchema,
strategyVariantSchema,
featureEnvironmentSchema,
contextFieldSchema,
featureTagSchema,
Expand Down
2 changes: 2 additions & 0 deletions src/lib/openapi/spec/feature-environment-schema.ts
Expand Up @@ -3,6 +3,7 @@ import { constraintSchema } from './constraint-schema';
import { parametersSchema } from './parameters-schema';
import { featureStrategySchema } from './feature-strategy-schema';
import { variantSchema } from './variant-schema';
import { strategyVariantSchema } from './strategy-variant-schema';

export const featureEnvironmentSchema = {
$id: '#/components/schemas/featureEnvironmentSchema',
Expand Down Expand Up @@ -65,6 +66,7 @@ export const featureEnvironmentSchema = {
constraintSchema,
parametersSchema,
featureStrategySchema,
strategyVariantSchema,
variantSchema,
},
},
Expand Down
2 changes: 2 additions & 0 deletions src/lib/openapi/spec/feature-schema.ts
Expand Up @@ -6,6 +6,7 @@ import { parametersSchema } from './parameters-schema';
import { featureStrategySchema } from './feature-strategy-schema';
import { tagSchema } from './tag-schema';
import { featureEnvironmentSchema } from './feature-environment-schema';
import { strategyVariantSchema } from './strategy-variant-schema';

export const featureSchema = {
$id: '#/components/schemas/featureSchema',
Expand Down Expand Up @@ -125,6 +126,7 @@ export const featureSchema = {
constraintSchema,
featureEnvironmentSchema,
featureStrategySchema,
strategyVariantSchema,
overrideSchema,
parametersSchema,
variantSchema,
Expand Down
9 changes: 9 additions & 0 deletions src/lib/openapi/spec/feature-strategy-schema.ts
@@ -1,6 +1,7 @@
import { FromSchema } from 'json-schema-to-ts';
import { constraintSchema } from './constraint-schema';
import { parametersSchema } from './parameters-schema';
import { strategyVariantSchema } from './strategy-variant-schema';

export const featureStrategySchema = {
$id: '#/components/schemas/featureStrategySchema',
Expand Down Expand Up @@ -59,6 +60,13 @@ export const featureStrategySchema = {
$ref: '#/components/schemas/constraintSchema',
},
},
variants: {
type: 'array',
description: 'Strategy level variants',
items: {
$ref: '#/components/schemas/strategyVariantSchema',
},
},
parameters: {
$ref: '#/components/schemas/parametersSchema',
},
Expand All @@ -67,6 +75,7 @@ export const featureStrategySchema = {
schemas: {
constraintSchema,
parametersSchema,
strategyVariantSchema,
},
},
} as const;
Expand Down
2 changes: 2 additions & 0 deletions src/lib/openapi/spec/features-schema.ts
Expand Up @@ -7,6 +7,7 @@ import { constraintSchema } from './constraint-schema';
import { featureStrategySchema } from './feature-strategy-schema';
import { environmentSchema } from './environment-schema';
import { featureEnvironmentSchema } from './feature-environment-schema';
import { strategyVariantSchema } from './strategy-variant-schema';

export const featuresSchema = {
$id: '#/components/schemas/featuresSchema',
Expand All @@ -32,6 +33,7 @@ export const featuresSchema = {
overrideSchema,
featureEnvironmentSchema,
featureStrategySchema,
strategyVariantSchema,
parametersSchema,
variantSchema,
},
Expand Down
4 changes: 4 additions & 0 deletions src/lib/openapi/spec/health-overview-schema.ts
Expand Up @@ -10,6 +10,8 @@ import { featureEnvironmentSchema } from './feature-environment-schema';
import { projectStatsSchema } from './project-stats-schema';
import { createFeatureStrategySchema } from './create-feature-strategy-schema';
import { projectEnvironmentSchema } from './project-environment-schema';
import { createStrategyVariantSchema } from './create-strategy-variant-schema';
import { strategyVariantSchema } from './strategy-variant-schema';

export const healthOverviewSchema = {
$id: '#/components/schemas/healthOverviewSchema',
Expand Down Expand Up @@ -121,12 +123,14 @@ export const healthOverviewSchema = {
environmentSchema,
projectEnvironmentSchema,
createFeatureStrategySchema,
createStrategyVariantSchema,
constraintSchema,
featureSchema,
featureEnvironmentSchema,
overrideSchema,
parametersSchema,
featureStrategySchema,
strategyVariantSchema,
variantSchema,
projectStatsSchema,
},
Expand Down
2 changes: 2 additions & 0 deletions src/lib/openapi/spec/import-toggles-schema.ts
Expand Up @@ -13,6 +13,7 @@ import { parametersSchema } from './parameters-schema';
import { legalValueSchema } from './legal-value-schema';
import { tagTypeSchema } from './tag-type-schema';
import { featureEnvironmentSchema } from './feature-environment-schema';
import { strategyVariantSchema } from './strategy-variant-schema';

export const importTogglesSchema = {
$id: '#/components/schemas/importTogglesSchema',
Expand Down Expand Up @@ -43,6 +44,7 @@ export const importTogglesSchema = {
exportResultSchema,
featureSchema,
featureStrategySchema,
strategyVariantSchema,
featureEnvironmentSchema,
contextFieldSchema,
featureTagSchema,
Expand Down
2 changes: 2 additions & 0 deletions src/lib/openapi/spec/index.ts
Expand Up @@ -147,3 +147,5 @@ export * from './advanced-playground-feature-schema';
export * from './advanced-playground-response-schema';
export * from './advanced-playground-request-schema';
export * from './telemetry-settings-schema';
export * from './create-strategy-variant-schema';
export * from './strategy-variant-schema';
2 changes: 2 additions & 0 deletions src/lib/openapi/spec/project-environment-schema.ts
@@ -1,5 +1,6 @@
import { FromSchema } from 'json-schema-to-ts';
import { createFeatureStrategySchema } from './create-feature-strategy-schema';
import { createStrategyVariantSchema } from './create-strategy-variant-schema';

export const projectEnvironmentSchema = {
$id: '#/components/schemas/projectEnvironmentSchema',
Expand Down Expand Up @@ -29,6 +30,7 @@ export const projectEnvironmentSchema = {
components: {
schemas: {
createFeatureStrategySchema,
createStrategyVariantSchema,
},
},
} as const;
Expand Down
4 changes: 4 additions & 0 deletions src/lib/openapi/spec/project-overview-schema.ts
Expand Up @@ -10,6 +10,8 @@ import { featureEnvironmentSchema } from './feature-environment-schema';
import { projectStatsSchema } from './project-stats-schema';
import { createFeatureStrategySchema } from './create-feature-strategy-schema';
import { projectEnvironmentSchema } from './project-environment-schema';
import { createStrategyVariantSchema } from './create-strategy-variant-schema';
import { strategyVariantSchema } from './strategy-variant-schema';

export const projectOverviewSchema = {
$id: '#/components/schemas/projectOverviewSchema',
Expand Down Expand Up @@ -123,12 +125,14 @@ export const projectOverviewSchema = {
environmentSchema,
projectEnvironmentSchema,
createFeatureStrategySchema,
createStrategyVariantSchema,
constraintSchema,
featureSchema,
featureEnvironmentSchema,
overrideSchema,
parametersSchema,
featureStrategySchema,
strategyVariantSchema,
variantSchema,
projectStatsSchema,
},
Expand Down
2 changes: 2 additions & 0 deletions src/lib/openapi/spec/state-schema.ts
Expand Up @@ -10,6 +10,7 @@ import { environmentSchema } from './environment-schema';
import { segmentSchema } from './segment-schema';
import { featureStrategySegmentSchema } from './feature-strategy-segment-schema';
import { strategySchema } from './strategy-schema';
import { strategyVariantSchema } from './strategy-variant-schema';

export const stateSchema = {
$id: '#/components/schemas/stateSchema',
Expand Down Expand Up @@ -98,6 +99,7 @@ export const stateSchema = {
featureTagSchema,
projectSchema,
featureStrategySchema,
strategyVariantSchema,
featureEnvironmentSchema,
environmentSchema,
segmentSchema,
Expand Down
17 changes: 17 additions & 0 deletions src/lib/openapi/spec/strategy-variant-schema.ts
@@ -0,0 +1,17 @@
import { FromSchema } from 'json-schema-to-ts';
import { createStrategyVariantSchema } from './create-strategy-variant-schema';

export const strategyVariantSchema = {
$id: '#/components/schemas/strategyVariantSchema',
type: 'object',
additionalProperties: false,
description:
"This is an experimental property. It may change or be removed as we work on it. Please don't depend on it yet. A strategy variant allows you to attach any data to strategies instead of only returning `true`/`false`. Strategy variants take precedence over feature variants.",
required: ['name', 'weight', 'weightType', 'stickiness'],
properties: {
...createStrategyVariantSchema.properties,
},
components: {},
} as const;

export type StrategyVariantSchema = FromSchema<typeof strategyVariantSchema>;
3 changes: 3 additions & 0 deletions src/lib/openapi/spec/variant-schema.ts
Expand Up @@ -39,9 +39,12 @@ export const variantSchema = {
description: 'Extra data configured for this variant',
properties: {
type: {
description:
'The type of the value. Commonly used types are string, json and csv.',
type: 'string',
},
value: {
description: 'The actual value of payload',
type: 'string',
},
},
Expand Down

0 comments on commit ce87806

Please sign in to comment.