(schema, obj, level = 0, suffix = '')
| 721 | * @param {string} suffix - used for suffixing property names to avoid duplicate props during object composion |
| 722 | */ |
| 723 | export function schemaInObjectNotation(schema, obj, level = 0, suffix = '') { |
| 724 | if (!schema) { |
| 725 | return; |
| 726 | } |
| 727 | if (schema.allOf) { |
| 728 | const objWithAllProps = {}; |
| 729 | if (schema.allOf.length === 1 && !schema.allOf[0].properties && !schema.allOf[0].items) { |
| 730 | // If allOf has single item and the type is not an object or array, then its a primitive |
| 731 | const tempSchema = schema.allOf[0]; |
| 732 | return `${getTypeInfo(tempSchema).html}`; |
| 733 | } |
| 734 | // If allOf is an array of multiple elements, then all the keys makes a single object |
| 735 | schema.allOf.map((v, i) => { |
| 736 | if (v.type === 'object' || v.properties || v.allOf || v.anyOf || v.oneOf) { |
| 737 | const propSuffix = (v.anyOf || v.oneOf) && i > 0 ? i : ''; |
| 738 | const partialObj = schemaInObjectNotation(v, {}, (level + 1), propSuffix); |
| 739 | Object.assign(objWithAllProps, partialObj); |
| 740 | } else if (v.type === 'array' || v.items) { |
| 741 | const partialObj = schemaInObjectNotation(v, {}, (level + 1)); |
| 742 | Object.assign(objWithAllProps, partialObj); |
| 743 | } else if (v.type) { |
| 744 | const prop = `prop${Object.keys(objWithAllProps).length}`; |
| 745 | const typeObj = getTypeInfo(v); |
| 746 | objWithAllProps[prop] = `${typeObj.html}`; |
| 747 | } else { |
| 748 | return ''; |
| 749 | } |
| 750 | }); |
| 751 | obj = objWithAllProps; |
| 752 | } else if (schema.anyOf || schema.oneOf) { |
| 753 | obj['::description'] = schema.description || ''; |
| 754 | // 1. First iterate the regular properties |
| 755 | if (schema.type === 'object' || schema.properties) { |
| 756 | obj['::description'] = schema.description || ''; |
| 757 | obj['::type'] = 'object'; |
| 758 | // obj['::deprecated'] = schema.deprecated || false; |
| 759 | for (const key in schema.properties) { |
| 760 | if (schema.required && schema.required.includes(key)) { |
| 761 | obj[`${key}*`] = schemaInObjectNotation(schema.properties[key], {}, (level + 1)); |
| 762 | } else { |
| 763 | obj[key] = schemaInObjectNotation(schema.properties[key], {}, (level + 1)); |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | // 2. Then show allof/anyof objects |
| 768 | const objWithAnyOfProps = {}; |
| 769 | const xxxOf = schema.anyOf ? 'anyOf' : 'oneOf'; |
| 770 | schema[xxxOf].forEach((v, index) => { |
| 771 | if (v.type === 'object' || v.properties || v.allOf || v.anyOf || v.oneOf) { |
| 772 | const partialObj = schemaInObjectNotation(v, {}); |
| 773 | objWithAnyOfProps[`::OPTION~${index + 1}${v.title ? `~${v.title}` : ''}`] = partialObj; |
| 774 | objWithAnyOfProps[`::OPTION~${index + 1}${v.title ? `~${v.title}` : ''}`]['::readwrite'] = ''; // xxx-options cannot be read or write only |
| 775 | objWithAnyOfProps['::type'] = 'xxx-of-option'; |
| 776 | } else if (v.type === 'array' || v.items) { |
| 777 | // This else-if block never seems to get executed |
| 778 | const partialObj = schemaInObjectNotation(v, {}); |
| 779 | objWithAnyOfProps[`::OPTION~${index + 1}${v.title ? `~${v.title}` : ''}`] = partialObj; |
| 780 | objWithAnyOfProps[`::OPTION~${index + 1}${v.title ? `~${v.title}` : ''}`]['::readwrite'] = ''; // xxx-options cannot be read or write only |
no test coverage detected
searching dependent graphs…