Add safe guards array without items (fixes #199)

This commit is contained in:
Roman Hotsiy 2017-02-09 15:24:35 +02:00
parent ee36392164
commit 5f909cac47
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
4 changed files with 24 additions and 4 deletions

View File

@ -68,7 +68,7 @@
</td>
<td class="param-info">
<div>
<span class="param-type {{prop.type}}" [ngClass]="{'with-hint': prop._displayTypeHint, 'tuple': prop._isTuple}"
<span class="param-type {{prop.type}}" [ngClass]="{'with-hint': prop._displayTypeHint, 'tuple': prop._isTuple, 'array': prop._isArray}"
title="{{prop._displayTypeHint}}"> {{prop._displayType}} {{prop._displayFormat}}
<span class="param-range" *ngIf="prop._range"> {{prop._range}} </span>
</span>

View File

@ -17,4 +17,19 @@ describe('Spec Helper', () => {
(<jasmine.Spy>console.warn).and.callThrough();
});
});
describe('preprocessProperties', () => {
it('should not throw when type array and items are not defined', () => {
let schema = {
type: 'object',
properties: {
prop1: {
type: 'array'
}
}
};
(() => SchemaHelper.preprocessProperties(schema, '#/', {})).should.not.throw();
});
});
});

View File

@ -5,7 +5,7 @@ import { WarningsService } from './warnings.service';
import * as slugify from 'slugify';
interface PropertyPreprocessOptions {
childFor: string;
childFor?: string;
skipReadOnly?: boolean;
discriminator?: string;
}
@ -54,6 +54,7 @@ const injectors = {
return propertySchema.type === 'array' && !Array.isArray(propertySchema.items);
},
inject: (injectTo, propertySchema = injectTo, propPointer) => {
if (!propertySchema.items) propertySchema.items = {};
if (!(SchemaHelper.detectType(propertySchema.items) === 'object')) {
injectTo._isArray = true;
injectTo._pointer = propertySchema.items._pointer
@ -207,7 +208,11 @@ export class SchemaHelper {
static preprocessProperties(schema:any, pointer:string, opts: PropertyPreprocessOptions) {
let requiredMap = {};
if (schema.required) {
schema.required.forEach(prop => requiredMap[prop] = true);
if (Array.isArray(schema.required)) {
schema.required.forEach(prop => requiredMap[prop] = true);
} else {
WarningsService.warn(`required must be an array: "${typeof schema.required}" found at ${pointer}`);
}
}
let props = schema.properties && Object.keys(schema.properties).map(propName => {

View File

@ -29,7 +29,7 @@ export class SchemaNormalizer {
if (opts.childFor) this._dereferencer.visit(opts.childFor);
if (schema['x-redoc-normalized']) return schema;
let res = SchemaWalker.walk(schema, ptr, (subSchema, ptr) => {
let res = SchemaWalker.walk(schema, ptr, (subSchema, ptr) => {
let resolved = this._dereferencer.dereference(subSchema, ptr);
if (resolved.allOf) {
resolved._pointer = resolved._pointer || ptr;