Skip to content

Commit

Permalink
fix feature-schema.ts (#2729)
Browse files Browse the repository at this point in the history
Signed-off-by: andreas-unleash <andreas@getunleash.ai>

<!-- Thanks for creating a PR! To make it easier for reviewers and
everyone else to understand what your changes relate to, please add some
relevant content to the headings below. Feel free to ignore or delete
sections that you don't think are relevant. Thank you! ❤️ -->
This PR fixes the admin feature schema by moving the `strategies`
(featureStrategySchema) from the root of `feature` object to
`feature.environment`
## About the changes
<!-- Describe the changes introduced. What are they and why are they
being introduced? Feel free to also add screenshots or steps to view the
changes if they're visual. -->

<!-- Does it close an issue? Multiple? -->
Closes #

<!-- (For internal contributors): Does it relate to an issue on public
roadmap? -->
<!--
Relates to [roadmap](https://github.com/orgs/Unleash/projects/10) item:
#
-->

### Important files
<!-- PRs can contain a lot of changes, but not all changes are equally
important. Where should a reviewer start looking to get an overview of
the changes? Are any files particularly important? -->


## Discussion points
<!-- Anything about the PR you'd like to discuss before it gets merged?
Got any questions or doubts? -->

Signed-off-by: andreas-unleash <andreas@getunleash.ai>
  • Loading branch information
andreas-unleash committed Jan 11, 2023
1 parent cf3987e commit e050495
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 153 deletions.
Expand Up @@ -4,7 +4,7 @@ exports[`featureSchema constraints 1`] = `
{
"errors": [
{
"instancePath": "/strategies/0/constraints/0",
"instancePath": "/environments/0/strategies/0/constraints/0",
"keyword": "required",
"message": "must have required property 'operator'",
"params": {
Expand Down
5 changes: 1 addition & 4 deletions src/lib/openapi/spec/feature-environment-schema.test.ts
Expand Up @@ -8,10 +8,7 @@ test('featureEnvironmentSchema', () => {
strategies: [
{
id: '',
featureName: '',
projectId: '',
environment: '',
strategyName: '',
name: '',
constraints: [{ contextName: '', operator: 'IN' }],
parameters: { a: '' },
},
Expand Down
47 changes: 3 additions & 44 deletions src/lib/openapi/spec/feature-environment-schema.ts
@@ -1,6 +1,7 @@
import { FromSchema } from 'json-schema-to-ts';
import { constraintSchema } from './constraint-schema';
import { parametersSchema } from './parameters-schema';
import { featureStrategySchema } from './feature-strategy-schema';

export const featureEnvironmentSchema = {
$id: '#/components/schemas/featureEnvironmentSchema',
Expand All @@ -23,57 +24,15 @@ export const featureEnvironmentSchema = {
strategies: {
type: 'array',
items: {
type: 'object',
additionalProperties: false,
required: [
'id',
'featureName',
'projectId',
'environment',
'strategyName',
'constraints',
'parameters',
],
properties: {
id: {
type: 'string',
},
featureName: {
type: 'string',
},
projectId: {
type: 'string',
},
environment: {
type: 'string',
},
strategyName: {
type: 'string',
},
sortOrder: {
type: 'number',
},
createdAt: {
type: 'string',
format: 'date-time',
},
constraints: {
type: 'array',
items: {
$ref: '#/components/schemas/constraintSchema',
},
},
parameters: {
$ref: '#/components/schemas/parametersSchema',
},
},
$ref: '#/components/schemas/featureStrategySchema',
},
},
},
components: {
schemas: {
constraintSchema,
parametersSchema,
featureStrategySchema,
},
},
} as const;
Expand Down
37 changes: 23 additions & 14 deletions src/lib/openapi/spec/feature-schema.test.ts
Expand Up @@ -4,19 +4,6 @@ import { FeatureSchema } from './feature-schema';
test('featureSchema', () => {
const data: FeatureSchema = {
name: 'a',
strategies: [
{
id: 'a',
name: 'a',
constraints: [
{
contextName: 'a',
operator: 'IN',
},
],
segments: [1],
},
],
variants: [
{
name: 'a',
Expand All @@ -32,6 +19,19 @@ test('featureSchema', () => {
name: 'a',
type: 'b',
enabled: true,
strategies: [
{
id: 'a',
name: 'a',
constraints: [
{
contextName: 'a',
operator: 'IN',
},
],
segments: [1],
},
],
},
],
};
Expand All @@ -44,7 +44,16 @@ test('featureSchema', () => {
test('featureSchema constraints', () => {
const data = {
name: 'a',
strategies: [{ name: 'a', constraints: [{ contextName: 'a' }] }],
environments: [
{
name: 'a',
type: 'b',
enabled: true,
strategies: [
{ name: 'a', constraints: [{ contextName: 'a' }] },
],
},
],
};

expect(
Expand Down
14 changes: 4 additions & 10 deletions src/lib/openapi/spec/feature-schema.ts
Expand Up @@ -3,9 +3,9 @@ import { variantSchema } from './variant-schema';
import { constraintSchema } from './constraint-schema';
import { overrideSchema } from './override-schema';
import { parametersSchema } from './parameters-schema';
import { environmentSchema } from './environment-schema';
import { featureStrategySchema } from './feature-strategy-schema';
import { tagSchema } from './tag-schema';
import { featureEnvironmentSchema } from './feature-environment-schema';

export const featureSchema = {
$id: '#/components/schemas/featureSchema',
Expand Down Expand Up @@ -58,13 +58,7 @@ export const featureSchema = {
environments: {
type: 'array',
items: {
$ref: '#/components/schemas/environmentSchema',
},
},
strategies: {
type: 'array',
items: {
$ref: '#/components/schemas/featureStrategySchema',
$ref: '#/components/schemas/featureEnvironmentSchema',
},
},
variants: {
Expand All @@ -84,10 +78,10 @@ export const featureSchema = {
components: {
schemas: {
constraintSchema,
environmentSchema,
featureEnvironmentSchema,
featureStrategySchema,
overrideSchema,
parametersSchema,
featureStrategySchema,
variantSchema,
tagSchema,
},
Expand Down
4 changes: 3 additions & 1 deletion src/lib/openapi/spec/features-schema.ts
Expand Up @@ -6,6 +6,7 @@ import { overrideSchema } from './override-schema';
import { constraintSchema } from './constraint-schema';
import { featureStrategySchema } from './feature-strategy-schema';
import { environmentSchema } from './environment-schema';
import { featureEnvironmentSchema } from './feature-environment-schema';

export const featuresSchema = {
$id: '#/components/schemas/featuresSchema',
Expand All @@ -29,8 +30,9 @@ export const featuresSchema = {
environmentSchema,
featureSchema,
overrideSchema,
parametersSchema,
featureEnvironmentSchema,
featureStrategySchema,
parametersSchema,
variantSchema,
},
},
Expand Down
2 changes: 2 additions & 0 deletions src/lib/openapi/spec/health-overview-schema.ts
Expand Up @@ -6,6 +6,7 @@ import { featureStrategySchema } from './feature-strategy-schema';
import { featureSchema } from './feature-schema';
import { constraintSchema } from './constraint-schema';
import { environmentSchema } from './environment-schema';
import { featureEnvironmentSchema } from './feature-environment-schema';

export const healthOverviewSchema = {
$id: '#/components/schemas/healthOverviewSchema',
Expand Down Expand Up @@ -54,6 +55,7 @@ export const healthOverviewSchema = {
constraintSchema,
environmentSchema,
featureSchema,
featureEnvironmentSchema,
overrideSchema,
parametersSchema,
featureStrategySchema,
Expand Down
62 changes: 34 additions & 28 deletions src/lib/routes/admin-api/project/features.ts
@@ -1,11 +1,10 @@
import { Request, Response } from 'express';
import { applyPatch, Operation } from 'fast-json-patch';
import Controller from '../../controller';
import { IUnleashConfig } from '../../../types/option';
import { IUnleashServices } from '../../../types';
import FeatureToggleService from '../../../services/feature-toggle-service';
import { Logger } from '../../../logger';
import {
IUnleashConfig,
IUnleashServices,
serializeDates,
CREATE_FEATURE,
CREATE_FEATURE_STRATEGY,
DELETE_FEATURE,
Expand All @@ -14,37 +13,35 @@ import {
UPDATE_FEATURE,
UPDATE_FEATURE_ENVIRONMENT,
UPDATE_FEATURE_STRATEGY,
} from '../../../types/permissions';
import { extractUsername } from '../../../util/extract-user';
} from '../../../types';
import { Logger } from '../../../logger';
import { extractUsername } from '../../../util';
import { IAuthRequest } from '../../unleash-types';
import { CreateFeatureSchema } from '../../../openapi/spec/create-feature-schema';
import {
AdminFeaturesQuerySchema,
CreateFeatureSchema,
CreateFeatureStrategySchema,
createRequestSchema,
createResponseSchema,
emptyResponse,
FeatureEnvironmentSchema,
featureSchema,
FeatureSchema,
} from '../../../openapi/spec/feature-schema';
import { FeatureStrategySchema } from '../../../openapi/spec/feature-strategy-schema';
import { ParametersSchema } from '../../../openapi/spec/parameters-schema';
import {
featuresSchema,
FeaturesSchema,
} from '../../../openapi/spec/features-schema';
import { UpdateFeatureSchema } from '../../../openapi/spec/update-feature-schema';
import { UpdateFeatureStrategySchema } from '../../../openapi/spec/update-feature-strategy-schema';
import { CreateFeatureStrategySchema } from '../../../openapi/spec/create-feature-strategy-schema';
import { serializeDates } from '../../../types/serialize-dates';
import { OpenApiService } from '../../../services/openapi-service';
import { createRequestSchema } from '../../../openapi/util/create-request-schema';
import { createResponseSchema } from '../../../openapi/util/create-response-schema';
import { FeatureEnvironmentSchema } from '../../../openapi/spec/feature-environment-schema';
import { SetStrategySortOrderSchema } from '../../../openapi/spec/set-strategy-sort-order-schema';

import {
emptyResponse,
FeatureStrategySchema,
getStandardResponses,
} from '../../../openapi/util/standard-responses';
import { SegmentService } from '../../../services/segment-service';
ParametersSchema,
SetStrategySortOrderSchema,
UpdateFeatureSchema,
UpdateFeatureStrategySchema,
} from '../../../openapi';
import {
OpenApiService,
SegmentService,
FeatureToggleService,
} from '../../../services';
import { querySchema } from '../../../schema/feature-schema';
import { AdminFeaturesQuerySchema } from '../../../openapi';

interface FeatureStrategyParams {
projectId: string;
Expand Down Expand Up @@ -589,11 +586,20 @@ export default class ProjectFeaturesController extends Controller {
environment,
featureName,
);

const result = {
...environmentInfo,
strategies: environmentInfo.strategies.map((strategy) => {
const { strategyName, ...rest } = strategy;
return { ...rest, name: strategyName };
}),
};

this.openApiService.respondWithValidation(
200,
res,
featureSchema.$id,
serializeDates(environmentInfo),
serializeDates(result),
);
}

Expand Down
53 changes: 2 additions & 51 deletions src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap
Expand Up @@ -1079,50 +1079,7 @@ exports[`should serve the OpenAPI spec 1`] = `
},
"strategies": {
"items": {
"additionalProperties": false,
"properties": {
"constraints": {
"items": {
"$ref": "#/components/schemas/constraintSchema",
},
"type": "array",
},
"createdAt": {
"format": "date-time",
"type": "string",
},
"environment": {
"type": "string",
},
"featureName": {
"type": "string",
},
"id": {
"type": "string",
},
"parameters": {
"$ref": "#/components/schemas/parametersSchema",
},
"projectId": {
"type": "string",
},
"sortOrder": {
"type": "number",
},
"strategyName": {
"type": "string",
},
},
"required": [
"id",
"featureName",
"projectId",
"environment",
"strategyName",
"constraints",
"parameters",
],
"type": "object",
"$ref": "#/components/schemas/featureStrategySchema",
},
"type": "array",
},
Expand Down Expand Up @@ -1204,7 +1161,7 @@ exports[`should serve the OpenAPI spec 1`] = `
},
"environments": {
"items": {
"$ref": "#/components/schemas/environmentSchema",
"$ref": "#/components/schemas/featureEnvironmentSchema",
},
"type": "array",
},
Expand All @@ -1228,12 +1185,6 @@ exports[`should serve the OpenAPI spec 1`] = `
"stale": {
"type": "boolean",
},
"strategies": {
"items": {
"$ref": "#/components/schemas/featureStrategySchema",
},
"type": "array",
},
"tags": {
"items": {
"$ref": "#/components/schemas/tagSchema",
Expand Down

0 comments on commit e050495

Please sign in to comment.