diff --git a/src/lib/openapi/spec/client-features-query-schema.ts b/src/lib/openapi/spec/client-features-query-schema.ts index d96576de8f..89ea44d3a2 100644 --- a/src/lib/openapi/spec/client-features-query-schema.ts +++ b/src/lib/openapi/spec/client-features-query-schema.ts @@ -3,7 +3,6 @@ import { FromSchema } from 'json-schema-to-ts'; export const clientFeaturesQuerySchema = { $id: '#/components/schemas/clientFeaturesQuerySchema', type: 'object', - required: [], additionalProperties: false, properties: { tag: { diff --git a/src/lib/openapi/spec/export-parameters-schema.ts b/src/lib/openapi/spec/export-parameters-schema.ts index 0dceaada20..ef82acf2ae 100644 --- a/src/lib/openapi/spec/export-parameters-schema.ts +++ b/src/lib/openapi/spec/export-parameters-schema.ts @@ -1,5 +1,34 @@ import { FromSchema } from 'json-schema-to-ts'; +export const exportParameters = { + format: { + type: 'string', + enum: ['json', 'yaml'], + default: 'json', + description: '', + }, + download: { + type: 'boolean', + default: 'false', + description: '', + }, + strategies: { + type: 'boolean', + }, + featureToggles: { + type: 'boolean', + }, + projects: { + type: 'boolean', + }, + tags: { + type: 'boolean', + }, + environments: { + type: 'boolean', + }, +}; + export const exportParametersSchema = { $id: '#/components/schemas/exportParametersSchema', type: 'object', diff --git a/src/lib/openapi/spec/feedback-schema.ts b/src/lib/openapi/spec/feedback-schema.ts index 390c2870a9..ca23d2576d 100644 --- a/src/lib/openapi/spec/feedback-schema.ts +++ b/src/lib/openapi/spec/feedback-schema.ts @@ -4,7 +4,6 @@ export const feedbackSchema = { $id: '#/components/schemas/feedbackSchema', type: 'object', additionalProperties: false, - required: [], properties: { userId: { type: 'number', diff --git a/src/lib/openapi/util/request-parameters.test.ts b/src/lib/openapi/util/request-parameters.test.ts new file mode 100644 index 0000000000..5ac64c076c --- /dev/null +++ b/src/lib/openapi/util/request-parameters.test.ts @@ -0,0 +1,39 @@ +import { SchemaObject } from 'ajv'; +import fc from 'fast-check'; +import { createRequestParameters } from './request-parameters'; + +describe('request parameter utils', () => { + it('turns an object of names and descriptions into a an expected parameter list', () => { + fc.assert( + fc.property( + fc.dictionary(fc.string({ minLength: 1 }), fc.string()), + (parameters) => { + const result = createRequestParameters(parameters); + + return result.every((paramsObject) => { + return ( + paramsObject.description === + parameters[paramsObject.name] + ); + }); + }, + ), + ); + }); + + it('says every parameter is of type string and goes in the query', () => { + fc.assert( + fc.property( + fc.dictionary(fc.string({ minLength: 1 }), fc.string()), + + (parameters) => { + return createRequestParameters(parameters).every( + (paramsObject) => + (paramsObject.schema as SchemaObject).type === + 'string' && paramsObject.in === 'query', + ); + }, + ), + ); + }); +}); diff --git a/src/lib/openapi/util/request-parameters.ts b/src/lib/openapi/util/request-parameters.ts new file mode 100644 index 0000000000..fa571ea7bb --- /dev/null +++ b/src/lib/openapi/util/request-parameters.ts @@ -0,0 +1,25 @@ +import { OpenAPIV3 } from 'openapi-types'; + +// type ParameterDetails = { +// description: string; +// } & ( +// | { type: 'boolean'; default?: boolean; enum?: boolean[] } +// | { type: 'string'; default?: string; enum?: string[] } +// | { type: 'number'; default?: number; enum?: number[] } +// ); + +// { [parameterName: string]: Parameter }; +// type Parameters = Record; + +type ParameterName = string; +type Description = string; + +export const createRequestParameters = ( + params: Record, +): OpenAPIV3.ParameterObject[] => + Object.entries(params).map(([name, description]) => ({ + name, + description, + schema: { type: 'string' }, + in: 'query', + })); diff --git a/src/lib/routes/admin-api/state.ts b/src/lib/routes/admin-api/state.ts index 1e22ebbeea..dbd45f9daa 100644 --- a/src/lib/routes/admin-api/state.ts +++ b/src/lib/routes/admin-api/state.ts @@ -14,8 +14,12 @@ import { IAuthRequest } from '../unleash-types'; import { OpenApiService } from '../../services/openapi-service'; import { createRequestSchema } from '../../openapi/util/create-request-schema'; import { createResponseSchema } from '../../openapi/util/create-response-schema'; -import { ExportParametersSchema } from '../../openapi/spec/export-parameters-schema'; +import { + exportParametersSchema, + ExportParametersSchema, +} from '../../openapi/spec/export-parameters-schema'; import { emptyResponse } from '../../openapi/util/standard-responses'; +import { createRequestParameters } from 'lib/openapi/util/request-parameters'; const upload = multer({ limits: { fileSize: 5242880 } }); const paramToBool = (param, def) => { @@ -75,11 +79,7 @@ class StateController extends Controller { responses: { 200: createResponseSchema('stateSchema'), }, - parameters: [ - { - $ref: '#/components/schema/exportParametersSchema', - }, - ], + parameters: createRequestParameters(exportParametersSchema), }), ], });