FBI-451: Improve example ID

This commit is contained in:
Rishi Tank 2023-06-14 09:43:01 +01:00
parent 00256a8da4
commit 18aadd5497
No known key found for this signature in database
GPG Key ID: BF025217B93532E7
2 changed files with 36 additions and 29 deletions

View File

@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`generateXmlExample should generate xml example with a complex list and each property has an example: example-0 1`] = ` exports[`generateXmlExample should generate xml example with a complex list and each property has an example: Example 1 1`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?> "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
<root> <root>
<foo> <foo>
@ -14,7 +14,7 @@ exports[`generateXmlExample should generate xml example with a complex list and
</root>" </root>"
`; `;
exports[`generateXmlExample should generate xml example with a list: example-0 1`] = ` exports[`generateXmlExample should generate xml example with a list: Example 1 1`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?> "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
<root> <root>
<foo>string</foo> <foo>string</foo>
@ -22,7 +22,7 @@ exports[`generateXmlExample should generate xml example with a list: example-0 1
</root>" </root>"
`; `;
exports[`generateXmlExample should generate xml example with readOnly and writeOnly: example-0 1`] = ` exports[`generateXmlExample should generate xml example with readOnly and writeOnly: Example 1 1`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?> "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
<root> <root>
<foo>string</foo> <foo>string</foo>
@ -30,7 +30,7 @@ exports[`generateXmlExample should generate xml example with readOnly and writeO
</root>" </root>"
`; `;
exports[`generateXmlExample should generate xml example with readOnly: example-0 1`] = ` exports[`generateXmlExample should generate xml example with readOnly: Example 1 1`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?> "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
<root> <root>
<foo>string</foo> <foo>string</foo>
@ -38,7 +38,7 @@ exports[`generateXmlExample should generate xml example with readOnly: example-0
</root>" </root>"
`; `;
exports[`generateXmlExample should generate xml example with writeOnly: example-0 1`] = ` exports[`generateXmlExample should generate xml example with writeOnly: Example 1 1`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?> "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
<root> <root>
<foo>string</foo> <foo>string</foo>
@ -46,7 +46,7 @@ exports[`generateXmlExample should generate xml example with writeOnly: example-
</root>" </root>"
`; `;
exports[`generateXmlExample should generate xml example with xml attributes: example-0 1`] = ` exports[`generateXmlExample should generate xml example with xml attributes: Example 1 1`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?> "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
<root> <root>
<Pet> <Pet>
@ -76,7 +76,7 @@ exports[`generateXmlExample should generate xml example with xml attributes: exa
</root>" </root>"
`; `;
exports[`generateXmlExample should generate xml example: example-0 1`] = ` exports[`generateXmlExample should generate xml example: Example 1 1`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?> "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
<root> <root>
<foo>string</foo> <foo>string</foo>
@ -84,7 +84,7 @@ exports[`generateXmlExample should generate xml example: example-0 1`] = `
</root>" </root>"
`; `;
exports[`generateXmlExample should generate xml for schemas with an array of items: example-0 1`] = ` exports[`generateXmlExample should generate xml for schemas with an array of items: Example 1 1`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?> "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
<User> <User>
<id>0</id> <id>0</id>
@ -127,7 +127,7 @@ exports[`generateXmlExample should generate xml for schemas with an array of ite
</User>" </User>"
`; `;
exports[`generateXmlExample should generate xml for schemas with an array of items: example-1 1`] = ` exports[`generateXmlExample should generate xml for schemas with an array of items: Example 2 1`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?> "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
<User> <User>
<id>0</id> <id>0</id>

View File

@ -93,8 +93,9 @@ const mergePropertyExamples = (
const mergedObj = {}; const mergedObj = {};
for (const exampleKey in obj) { for (const exampleKey in obj) {
for (const propExampleKey in propExamples) { for (const propExampleKey in propExamples) {
mergedObj[`example-${i}`] = { ...obj[exampleKey] }; const exampleId = getExampleId(i + 1);
mergedObj[`example-${i}`][propertyName] = propExamples[propExampleKey]; mergedObj[exampleId] = { ...obj[exampleKey] };
mergedObj[exampleId][propertyName] = propExamples[propExampleKey];
i++; i++;
if (i >= maxCombinations) { if (i >= maxCombinations) {
break; break;
@ -261,12 +262,15 @@ const getSampleValueByType = (schemaObj: OpenAPISchema) => {
return '?'; return '?';
}; };
const getExampleId = (id: number = 1): string => `Example ${id}`;
/* For changing JSON-Schema to a Sample Object, as per the schema (to generate examples based on schema) */ /* For changing JSON-Schema to a Sample Object, as per the schema (to generate examples based on schema) */
const schemaToSampleObj = (schema: OpenAPISchema, config: ExampleConfig = {}) => { const schemaToSampleObj = (schema: OpenAPISchema, config: ExampleConfig = {}) => {
let obj = {}; let obj = {};
if (!schema) { if (!schema) {
return; return;
} }
const defaultExampleId = getExampleId();
if (schema.allOf) { if (schema.allOf) {
const objWithAllProps = {}; const objWithAllProps = {};
@ -340,7 +344,7 @@ const schemaToSampleObj = (schema: OpenAPISchema, config: ExampleConfig = {}) =>
* *
* The above Schema should generate the following 2 examples * The above Schema should generate the following 2 examples
* *
* Example-1 * Example 1
* { * {
* prop1: 'string', * prop1: 'string',
* prop2: 'AAAAAAAAAA', <-- min-length 10 * prop2: 'AAAAAAAAAA', <-- min-length 10
@ -348,7 +352,7 @@ const schemaToSampleObj = (schema: OpenAPISchema, config: ExampleConfig = {}) =>
* option1_PropB: 'string' * option1_PropB: 'string'
* } * }
* *
* Example-2 * Example 2
* { * {
* prop1: 'string', * prop1: 'string',
* prop2: 'AAAAAAAAAA', <-- min-length 10 * prop2: 'AAAAAAAAAA', <-- min-length 10
@ -360,6 +364,7 @@ const schemaToSampleObj = (schema: OpenAPISchema, config: ExampleConfig = {}) =>
for (const key in schema.oneOf) { for (const key in schema.oneOf) {
const oneOfSamples = schemaToSampleObj(schema.oneOf[key], config); const oneOfSamples = schemaToSampleObj(schema.oneOf[key], config);
for (const sampleKey in oneOfSamples) { for (const sampleKey in oneOfSamples) {
const exampleId = getExampleId(i + 1);
// 2. In the final example include a one-of item along with properties // 2. In the final example include a one-of item along with properties
let finalExample; let finalExample;
if (Object.keys(objWithSchemaProps).length > 0) { if (Object.keys(objWithSchemaProps).length > 0) {
@ -372,8 +377,8 @@ const schemaToSampleObj = (schema: OpenAPISchema, config: ExampleConfig = {}) =>
} else { } else {
finalExample = oneOfSamples[sampleKey]; finalExample = oneOfSamples[sampleKey];
} }
obj[`example-${i}`] = finalExample; obj[exampleId] = finalExample;
addSchemaInfoToExample(schema.oneOf[key], obj[`example-${i}`]); addSchemaInfoToExample(schema.oneOf[key], obj[exampleId]);
i++; i++;
} }
} }
@ -382,7 +387,7 @@ const schemaToSampleObj = (schema: OpenAPISchema, config: ExampleConfig = {}) =>
// First generate values for regular properties // First generate values for regular properties
let commonObj; let commonObj;
if (schema.type === 'object' || schema.properties) { if (schema.type === 'object' || schema.properties) {
commonObj = { 'example-0': {} }; commonObj = { [defaultExampleId]: {} };
for (const propertyName in schema.properties) { for (const propertyName in schema.properties) {
if (schema.example) { if (schema.example) {
commonObj = schema; commonObj = schema;
@ -410,22 +415,23 @@ const schemaToSampleObj = (schema: OpenAPISchema, config: ExampleConfig = {}) =>
for (const key in schema.anyOf) { for (const key in schema.anyOf) {
const anyOfSamples = schemaToSampleObj(schema.anyOf[key], config); const anyOfSamples = schemaToSampleObj(schema.anyOf[key], config);
for (const sampleKey in anyOfSamples) { for (const sampleKey in anyOfSamples) {
const exampleId = getExampleId(i + 1);
if (typeof commonObj !== 'undefined') { if (typeof commonObj !== 'undefined') {
for (const commonKey in commonObj) { for (const commonKey in commonObj) {
obj[`example-${i}`] = { ...commonObj[commonKey], ...anyOfSamples[sampleKey] }; obj[exampleId] = { ...commonObj[commonKey], ...anyOfSamples[sampleKey] };
} }
} else { } else {
obj[`example-${i}`] = anyOfSamples[sampleKey]; obj[exampleId] = anyOfSamples[sampleKey];
} }
addSchemaInfoToExample(schema.anyOf[key], obj[`example-${i}`]); addSchemaInfoToExample(schema.anyOf[key], obj[exampleId]);
i++; i++;
} }
} }
} else if (schema.type === 'object' || schema.properties) { } else if (schema.type === 'object' || schema.properties) {
obj['example-0'] = {}; obj[defaultExampleId] = {};
addSchemaInfoToExample(schema, obj['example-0']); addSchemaInfoToExample(schema, obj[defaultExampleId]);
if (schema.example) { if (schema.example) {
obj['example-0'] = schema.example; obj[defaultExampleId] = schema.example;
} else { } else {
for (const propertyName in schema.properties) { for (const propertyName in schema.properties) {
const prop = schema.properties[propertyName] as OpenAPISchema; const prop = schema.properties[propertyName] as OpenAPISchema;
@ -455,7 +461,7 @@ const schemaToSampleObj = (schema: OpenAPISchema, config: ExampleConfig = {}) =>
if (prop.xml?.wrapped) { if (prop.xml?.wrapped) {
const wrappedItemSample = JSON.parse( const wrappedItemSample = JSON.parse(
`{ "${xmlTagName}" : { "${xmlTagName}" : ${JSON.stringify( `{ "${xmlTagName}" : { "${xmlTagName}" : ${JSON.stringify(
itemSamples['example-0'], itemSamples[defaultExampleId],
)} } }`, )} } }`,
); );
obj = mergePropertyExamples(obj, xmlTagName, wrappedItemSample); obj = mergePropertyExamples(obj, xmlTagName, wrappedItemSample);
@ -483,24 +489,25 @@ const schemaToSampleObj = (schema: OpenAPISchema, config: ExampleConfig = {}) =>
const schemaItems = schema.items as OpenAPISchema; const schemaItems = schema.items as OpenAPISchema;
if (schemaItems || schema.example) { if (schemaItems || schema.example) {
if (schema.example) { if (schema.example) {
obj['example-0'] = schema.example; obj[defaultExampleId] = schema.example;
} else if (schemaItems?.example) { } else if (schemaItems?.example) {
// schemas and properties support single example but not multiple examples. // schemas and properties support single example but not multiple examples.
obj['example-0'] = [schemaItems.example]; obj[defaultExampleId] = [schemaItems.example];
} else { } else {
const samples = schemaToSampleObj(schemaItems, config); const samples = schemaToSampleObj(schemaItems, config);
let i = 0; let i = 0;
for (const key in samples) { for (const key in samples) {
obj[`example-${i}`] = [samples[key]]; const exampleId = getExampleId(i + 1);
addSchemaInfoToExample(schemaItems, obj[`example-${i}`]); obj[exampleId] = [samples[key]];
addSchemaInfoToExample(schemaItems, obj[exampleId]);
i++; i++;
} }
} }
} else { } else {
obj['example-0'] = []; obj[defaultExampleId] = [];
} }
} else { } else {
return { 'example-0': getSampleValueByType(schema) }; return { [defaultExampleId]: getSampleValueByType(schema) };
} }
return obj; return obj;
}; };