Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/utils/schema-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ export function schemaInObjectNotation(rawSchema, options, level = 0, suffix = '
// If allOf is an array of multiple elements, then all the keys makes a single object
const objWithAllProps = {};
allOf.map((v, i) => {
if (v.type === 'object' || v.properties || v.allOf || v.anyOf || v.oneOf) {
if (v.type === 'object' || v.properties || v.patternProperties || v.allOf || v.anyOf || v.oneOf) {
const propSuffix = (v.anyOf || v.oneOf) && i > 0 ? i : '';
const partialObj = schemaInObjectNotation(v, options, (level + 1), propSuffix);
Object.assign(objWithAllProps, partialObj);
Expand Down Expand Up @@ -358,7 +358,7 @@ export function schemaInObjectNotation(rawSchema, options, level = 0, suffix = '
let readOnly = true;

(anyOf || oneOf || []).forEach((v, index) => {
if (v.type === 'object' || v.properties || v.allOf || v.anyOf || v.oneOf || v.type === 'array' || v.items) {
if (v.type === 'object' || v.properties || v.patternProperties || v.allOf || v.anyOf || v.oneOf || v.type === 'array' || v.items) {
const partialObj = schemaInObjectNotation(v, options);
if (partialObj) {
objWithAnyOfProps[`::OPTION~${index + 1}${v.title ? `~${v.title}` : ''}`] = partialObj;
Expand Down Expand Up @@ -455,6 +455,9 @@ export function schemaInObjectNotation(rawSchema, options, level = 0, suffix = '
objTypeOption[key] = schemaInObjectNotation(schemaProperties[key], options, (level + 1));
}
}
for (const key in schemaPatternProperties) {
objTypeOption[`<pattern: ${key}>`] = schemaInObjectNotation(schemaPatternProperties[key], options, (level + 1));
}

if (schema.additionalProperties) {
objTypeOption['<any-key>'] = schemaInObjectNotation(schema.additionalProperties, options);
Expand All @@ -481,7 +484,7 @@ export function schemaInObjectNotation(rawSchema, options, level = 0, suffix = '
return obj;
}

if (propertyType === 'object' || schemaProperties || schema.circularReference) {
if (propertyType === 'object' || schemaProperties || schemaPatternProperties || schema.circularReference) {
const obj = { '::type': '' };
obj['::title'] = schema.title || '';
obj['::description'] = schema.description || '';
Expand Down
29 changes: 28 additions & 1 deletion tests/schema-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import test from 'ava';
import { expect } from 'chai';

import xmlFormatter from '../src/utils/xml/xml.js';
import { getExampleValuesFromSchema } from '../src/utils/schema-utils.js';
import { getExampleValuesFromSchema, schemaInObjectNotation } from '../src/utils/schema-utils.js';

test('schema-utils.js getExampleValuesFromSchema', t => {
const schema = {
Expand Down Expand Up @@ -70,3 +70,30 @@ test('schema-utils.js getExampleValuesFromSchema', t => {
expect(formattedResult).to.eql(expectedFormattedResult);
t.pass();
});

const patternProperties = { '[a-z]{2}': { type: 'object', properties: { name: { type: 'string' } } } };
const patternKey = '<pattern: [a-z]{2}>';
const findPatternOwner = node => {
if (!node || typeof node !== 'object') { return undefined; }
if (node[patternKey]) { return node; }
return Object.values(node).map(findPatternOwner).find(n => n);
};
[
['a multi-type object', { type: ['object', 'null'], patternProperties }],
['array items of a multi-type object', { type: 'array', items: { type: ['object', 'null'], patternProperties } }],
['additionalProperties of a multi-type object', { type: 'object', additionalProperties: { type: ['object', 'null'], patternProperties } }],
['a typed one-of option', { oneOf: [{ type: 'object', patternProperties }, { type: 'null' }] }],
['a typed any-of option', { anyOf: [{ type: 'object', patternProperties }, { type: 'string' }] }],
['an untyped one-of option', { oneOf: [{ patternProperties }, { type: 'null' }] }],
['an untyped any-of option', { anyOf: [{ patternProperties }, { type: 'string' }] }],
['a typed all-of member', { allOf: [{ type: 'object', patternProperties }] }],
['an untyped all-of member', { allOf: [{ patternProperties }] }],
['an untyped object', { patternProperties }]
].forEach(([name, schema]) => {
test(`schema-utils.js schemaInObjectNotation renders patternProperties of ${name}`, t => {
const owner = findPatternOwner(schemaInObjectNotation(schema, {}));
expect(owner, 'pattern key rendered').to.not.equal(undefined);
expect(owner[patternKey]).to.have.property('name');
t.pass();
});
});
Loading