1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-07-26 13:48:33 +02:00

Wip: fix openapi spec

This commit is contained in:
Thomas Heartman 2022-07-20 08:24:02 +02:00
parent 012da8469f
commit 5af24d0a11
6 changed files with 99 additions and 8 deletions

View File

@ -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: {

View File

@ -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',

View File

@ -4,7 +4,6 @@ export const feedbackSchema = {
$id: '#/components/schemas/feedbackSchema',
type: 'object',
additionalProperties: false,
required: [],
properties: {
userId: {
type: 'number',

View File

@ -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',
);
},
),
);
});
});

View File

@ -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<ParameterName, ParameterDetails>;
type ParameterName = string;
type Description = string;
export const createRequestParameters = (
params: Record<ParameterName, Description>,
): OpenAPIV3.ParameterObject[] =>
Object.entries(params).map(([name, description]) => ({
name,
description,
schema: { type: 'string' },
in: 'query',
}));

View File

@ -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),
}),
],
});