Skip to content

Commit

Permalink
Add safe guards array without items (fixes #199)
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Feb 9, 2017
1 parent ee36392 commit 5f909ca
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/components/JsonSchema/json-schema.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
15 changes: 15 additions & 0 deletions lib/services/schema-helper.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
9 changes: 7 additions & 2 deletions lib/services/schema-helper.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { WarningsService } from './warnings.service';
import * as slugify from 'slugify';

interface PropertyPreprocessOptions {
childFor: string;
childFor?: string;
skipReadOnly?: boolean;
discriminator?: string;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 => {
Expand Down
2 changes: 1 addition & 1 deletion lib/services/schema-normalizer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5f909ca

Please sign in to comment.