mirror of
https://github.com/Redocly/redoc.git
synced 2024-12-03 05:43:44 +03:00
FBI-459: improvements
This commit is contained in:
parent
c4133cfdf5
commit
1d4c0dc242
104
src/utils/csv.ts
104
src/utils/csv.ts
|
@ -12,7 +12,8 @@ interface CsvExampleProps {
|
||||||
samplerOptions: object;
|
samplerOptions: object;
|
||||||
}
|
}
|
||||||
|
|
||||||
const hasSameHeaders = (headers, sample) => Object.keys(sample).every(key => headers.includes(key));
|
const hasSameHeaders = (headers: string, sample: OpenAPIExample) =>
|
||||||
|
Object.keys(sample).every(key => headers.includes(key));
|
||||||
|
|
||||||
const getCsvRows = (sample: OpenAPIExample): string => {
|
const getCsvRows = (sample: OpenAPIExample): string => {
|
||||||
const headers = Object.keys(sample?.[0] ?? sample).join(',');
|
const headers = Object.keys(sample?.[0] ?? sample).join(',');
|
||||||
|
@ -22,10 +23,10 @@ const getCsvRows = (sample: OpenAPIExample): string => {
|
||||||
);
|
);
|
||||||
if (!hasValidHeaders) return '';
|
if (!hasValidHeaders) return '';
|
||||||
|
|
||||||
let values;
|
let values: string;
|
||||||
|
|
||||||
if (Array.isArray(sample)) {
|
if (Array.isArray(sample)) {
|
||||||
values = sample.map(row => Object.values(row)).join('\n');
|
values = sample.map(Object.values).join('\n');
|
||||||
} else {
|
} else {
|
||||||
values = Object.values(sample).join(',');
|
values = Object.values(sample).join(',');
|
||||||
}
|
}
|
||||||
|
@ -33,7 +34,7 @@ const getCsvRows = (sample: OpenAPIExample): string => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const cleanUpExamples = (examples: Example[]): Example[] =>
|
const cleanUpExamples = (examples: Example[]): Example[] =>
|
||||||
examples.filter(example => example.exampleValue !== '');
|
examples.filter(({ exampleValue }) => exampleValue);
|
||||||
|
|
||||||
export const generateCsvExample = ({
|
export const generateCsvExample = ({
|
||||||
parser,
|
parser,
|
||||||
|
@ -50,66 +51,65 @@ export const generateCsvExample = ({
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
const processSamplesWithSchema = subSchema => {
|
const processSamplesWithSchema = (subSchema: OpenAPISchema) => {
|
||||||
if (subSchema) {
|
if (!subSchema) {
|
||||||
const subItems = subSchema.items as OpenAPISchema;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (subSchema.type === 'array' && subItems && depthCount < MAX_ITEM_DEPTH) {
|
const subItems = subSchema.items as OpenAPISchema;
|
||||||
depthCount++;
|
if (subSchema.type === 'array' && subItems && depthCount < MAX_ITEM_DEPTH) {
|
||||||
processSamplesWithSchema(subItems);
|
depthCount++;
|
||||||
}
|
processSamplesWithSchema(subItems);
|
||||||
|
}
|
||||||
const metadata = {
|
const metadata = {
|
||||||
exampleDescription: subSchema.description || schema.description || '',
|
exampleDescription: subSchema.description || schema.description || '',
|
||||||
exampleSummary: subSchema.title || schema.title || 'Example CSV',
|
exampleSummary: subSchema.title || schema.title || 'Example CSV',
|
||||||
|
};
|
||||||
|
if (subSchema.allOf) {
|
||||||
|
const resolved: OpenAPISchema = {
|
||||||
|
...schema,
|
||||||
|
items: parser.deref(subSchema.allOf as MergedOpenAPISchema).resolved,
|
||||||
};
|
};
|
||||||
|
const sampleData = Sampler.sample(
|
||||||
|
resolved as any,
|
||||||
|
samplerOptions,
|
||||||
|
parser.spec,
|
||||||
|
) as OpenAPIExample;
|
||||||
|
|
||||||
if (subSchema.allOf) {
|
const csvRows = getCsvRows(sampleData);
|
||||||
const resolved: OpenAPISchema = {
|
examples.push({
|
||||||
...schema,
|
exampleId: `Example ${exampleCount++}`,
|
||||||
items: parser.deref(subSchema.allOf as MergedOpenAPISchema).resolved,
|
exampleValue: csvRows,
|
||||||
};
|
...metadata,
|
||||||
|
});
|
||||||
|
} else if (subSchema.oneOf) {
|
||||||
|
const oneOfExamples = subSchema.oneOf.map(oneOfSchema => {
|
||||||
|
const { resolved } = parser.deref(oneOfSchema as MergedOpenAPISchema);
|
||||||
const sampleData = Sampler.sample(
|
const sampleData = Sampler.sample(
|
||||||
resolved as any,
|
resolved as any,
|
||||||
samplerOptions,
|
samplerOptions,
|
||||||
parser.spec,
|
parser.spec,
|
||||||
) as OpenAPIExample;
|
) as OpenAPIExample;
|
||||||
|
|
||||||
const csvRows = getCsvRows(sampleData);
|
const csvRows = getCsvRows(sampleData);
|
||||||
examples.push({
|
const currentMetadata = {
|
||||||
exampleId: `Example ${exampleCount++}`,
|
exampleDescription: oneOfSchema.description || metadata.exampleDescription,
|
||||||
exampleValue: csvRows,
|
exampleSummary: oneOfSchema.title || metadata.exampleSummary,
|
||||||
...metadata,
|
};
|
||||||
});
|
|
||||||
} else if (subSchema.oneOf) {
|
|
||||||
const oneOfExamples = subSchema.oneOf.map(oneOfSchema => {
|
|
||||||
const { resolved } = parser.deref(oneOfSchema as MergedOpenAPISchema);
|
|
||||||
const sampleData = Sampler.sample(
|
|
||||||
resolved as any,
|
|
||||||
samplerOptions,
|
|
||||||
parser.spec,
|
|
||||||
) as OpenAPIExample;
|
|
||||||
const csvRows = getCsvRows(sampleData);
|
|
||||||
const currentMetadata = {
|
|
||||||
exampleDescription: oneOfSchema.description || metadata.exampleDescription,
|
|
||||||
exampleSummary: oneOfSchema.title || metadata.exampleSummary,
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
exampleId: `Example ${exampleCount++}`,
|
|
||||||
exampleValue: csvRows,
|
|
||||||
...currentMetadata,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
examples = [...examples, ...oneOfExamples];
|
|
||||||
} else if (subSchema.$ref) {
|
|
||||||
const csvRows = getCsvRows(sample);
|
|
||||||
examples.push({
|
|
||||||
exampleId: `Example ${exampleCount++}`,
|
exampleId: `Example ${exampleCount++}`,
|
||||||
exampleValue: csvRows,
|
exampleValue: csvRows,
|
||||||
...metadata,
|
...currentMetadata,
|
||||||
});
|
};
|
||||||
}
|
});
|
||||||
|
examples = [...examples, ...oneOfExamples];
|
||||||
|
} else if (subSchema.$ref) {
|
||||||
|
const csvRows = getCsvRows(sample);
|
||||||
|
examples.push({
|
||||||
|
exampleId: `Example ${exampleCount++}`,
|
||||||
|
exampleValue: csvRows,
|
||||||
|
...metadata,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user