Skip to content

Commit

Permalink
Merge pull request #240 from Authress-Engineering/render-unknown-sche…
Browse files Browse the repository at this point in the history
…ma-types

Prevent issues with explicitly null properties. fix #238.
  • Loading branch information
wparad authored Feb 19, 2024
2 parents 710ba83 + 18a424f commit a8c5bf8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/components/schema-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ export default class SchemaTable extends LitElement {
} else if (data['::type']) {
displaySchemaLink = data['::link'];
if (dataType === 'array') {
detailObjType = data['::link'] || keyLabel.replace(/(s|Collection|List)[*]?$/i, ''); // Array of Object
detailObjType = data['::link'] || keyLabel.replace(/(s|Collection|List)[*]?$/i, '').replace(/[*]$/, ''); // Array of Object
} else {
detailObjType = data['::link'] || data['::type'];
detailObjType = (data['::link'] || data['::type']).replace(/[*]$/, '');
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/common-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function pathIsInSearch(searchVal, path) {
|| stringToSearch.toLowerCase().normalize('NFD').replace(/[\u0300-\u036f]/g, '').includes(searchVal);
}

export function schemaKeys(schemaProps, result = new Set()) {
function schemaKeys(schemaProps, result = new Set()) {
if (!schemaProps) {
return result;
}
Expand Down
25 changes: 15 additions & 10 deletions src/utils/schema-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ function getSimpleValueResult(schema, config, namespace, prefix, xmlAttributes,
let objectExamples = [{}];

Object.keys(schema.properties || {}).forEach((propertyName) => {
const innerSchema = schema.properties[propertyName];
const innerSchema = schema.properties[propertyName] || {};
if (innerSchema.deprecated) { return; }
if (innerSchema.readOnly && !config.includeReadOnly) { return; }
if (innerSchema.writeOnly && !config.includeWriteOnly) { return; }
Expand Down Expand Up @@ -311,10 +311,7 @@ export function isPatternProperty(label) {
* @param {string} suffix - used for suffixing property names to avoid duplicate props during object composition
*/
export function schemaInObjectNotation(rawSchema, options, level = 0, suffix = '') {
if (!rawSchema) {
return undefined;
}
const { allOf, oneOf, anyOf, items: arrayItemsSchema, properties: schemaProperties, patternProperties: schemaPatternProperties, ...schema } = rawSchema;
const { allOf, oneOf, anyOf, items: arrayItemsSchema, properties: schemaProperties, patternProperties: schemaPatternProperties, ...schema } = (rawSchema || {});
const propertyType = schema.type;
const metadata = { constraints: [] };
if (schema.uniqueItems) { metadata.constraints.push('Requires unique items'); }
Expand Down Expand Up @@ -342,7 +339,9 @@ export function schemaInObjectNotation(rawSchema, options, level = 0, suffix = '

const obj = schemaInObjectNotation(schema, options, 0);
return Object.assign({}, objWithAllProps, typeof obj === 'object' && !Array.isArray(obj) ? obj : {});
} else if (anyOf || oneOf) {
}

if (anyOf || oneOf) {
const objWithAnyOfProps = {};
let writeOnly = true;
let readOnly = true;
Expand Down Expand Up @@ -385,7 +384,9 @@ export function schemaInObjectNotation(rawSchema, options, level = 0, suffix = '
resultObj['::description'] = schema.description || '';
resultObj['::metadata'] = metadata;
return resultObj;
} else if (Array.isArray(propertyType)) {
}

if (Array.isArray(propertyType)) {
const obj = { '::type': '' };
// When a property has multiple types, then check further if any of the types are array or object, if yes then modify the schema using one-of
// Clone the schema - as it will be modified to replace multi-data-types with one-of;
Expand Down Expand Up @@ -467,7 +468,9 @@ export function schemaInObjectNotation(rawSchema, options, level = 0, suffix = '
obj['::ONE~OF'] = multiTypeOptions;
}
return obj;
} else if (propertyType === 'object' || schemaProperties) {
}

if (propertyType === 'object' || schemaProperties) {
const obj = { '::type': '' };
obj['::title'] = schema.title || '';
obj['::description'] = schema.description || '';
Expand All @@ -477,7 +480,7 @@ export function schemaInObjectNotation(rawSchema, options, level = 0, suffix = '
obj['::deprecated'] = schema.deprecated || false;
obj['::metadata'] = metadata;
for (const key in schemaProperties) {
if (!schema.deprecated && !schemaProperties[key].deprecated && schema.required?.includes(key)) {
if (!schema.deprecated && !schemaProperties[key]?.deprecated && schema.required?.includes(key)) {
obj[`${key}*`] = schemaInObjectNotation(schemaProperties[key], options, (level + 1));
} else {
obj[key] = schemaInObjectNotation(schemaProperties[key], options, (level + 1));
Expand All @@ -490,7 +493,9 @@ export function schemaInObjectNotation(rawSchema, options, level = 0, suffix = '
obj['<any-key>'] = schemaInObjectNotation(schema.additionalProperties, options);
}
return obj;
} else if (propertyType === 'array' || arrayItemsSchema) { // If Array
}

if (propertyType === 'array' || arrayItemsSchema) { // If Array
const obj = { '::type': '' };
obj['::title'] = schema.title || '';
obj['::description'] = schema.description || (arrayItemsSchema?.description ? `array&lt;${arrayItemsSchema.description}&gt;` : '');
Expand Down

0 comments on commit a8c5bf8

Please sign in to comment.