fix: pluralize arrray of types

This commit is contained in:
Roman Hotsiy 2019-05-12 20:44:39 +03:00
parent 5c187f34c9
commit fdcac30829
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
4 changed files with 34 additions and 2 deletions

View File

@ -37,7 +37,7 @@ describe('Models', () => {
parser = new OpenAPIParser(spec, undefined, opts); parser = new OpenAPIParser(spec, undefined, opts);
const schema = new SchemaModel(parser, spec.components.schemas.WithArray, '', opts); const schema = new SchemaModel(parser, spec.components.schemas.WithArray, '', opts);
expect(schema.oneOf).toHaveLength(2); expect(schema.oneOf).toHaveLength(2);
expect(schema.displayType).toBe('(Array of string or number) or string'); expect(schema.displayType).toBe('(Array of strings or numbers) or string');
}); });
}); });
}); });

View File

@ -14,6 +14,7 @@ import {
isNamedDefinition, isNamedDefinition,
isPrimitiveType, isPrimitiveType,
JsonPointer, JsonPointer,
pluralizeType,
sortByField, sortByField,
sortByRequired, sortByRequired,
} from '../../utils/'; } from '../../utils/';
@ -145,7 +146,7 @@ export class SchemaModel {
this.fields = buildFields(parser, schema, this.pointer, this.options); this.fields = buildFields(parser, schema, this.pointer, this.options);
} else if (this.type === 'array' && schema.items) { } else if (this.type === 'array' && schema.items) {
this.items = new SchemaModel(parser, schema.items, this.pointer + '/items', this.options); this.items = new SchemaModel(parser, schema.items, this.pointer + '/items', this.options);
this.displayType = this.items.displayType; this.displayType = pluralizeType(this.items.displayType);
this.displayFormat = this.items.format; this.displayFormat = this.items.format;
this.typePrefix = this.items.typePrefix + 'Array of '; this.typePrefix = this.items.typePrefix + 'Array of ';
this.title = this.title || this.items.title; this.title = this.title || this.items.title;

View File

@ -7,6 +7,7 @@ import {
isPrimitiveType, isPrimitiveType,
mergeParams, mergeParams,
normalizeServers, normalizeServers,
pluralizeType,
} from '../'; } from '../';
import { OpenAPIParser } from '../../services'; import { OpenAPIParser } from '../../services';
@ -353,4 +354,27 @@ describe('Utils', () => {
expect(humanizeConstraints(itemConstraintSchema(1))).toContain('non-empty'); expect(humanizeConstraints(itemConstraintSchema(1))).toContain('non-empty');
}); });
}); });
describe('OpenAPI pluralizeType', () => {
it('should pluralize all simple types', () => {
expect(pluralizeType('string')).toEqual('strings');
expect(pluralizeType('number')).toEqual('numbers');
expect(pluralizeType('object')).toEqual('objects');
expect(pluralizeType('integer')).toEqual('integers');
expect(pluralizeType('boolean')).toEqual('booleans');
expect(pluralizeType('array')).toEqual('arrays');
});
it('should pluralize complex dislay types', () => {
expect(pluralizeType('object (Pet)')).toEqual('objects (Pet)');
expect(pluralizeType('string <email>')).toEqual('strings <email>');
});
it('should pluralize oneOf-ed dislay types', () => {
expect(pluralizeType('object or string')).toEqual('objects or strings');
expect(pluralizeType('object (Pet) or number <int64>')).toEqual(
'objects (Pet) or numbers <int64>',
);
});
});
}); });

View File

@ -434,3 +434,10 @@ export function extractExtensions(obj: object, showExtensions: string[] | true):
return acc; return acc;
}, {}); }, {});
} }
export function pluralizeType(displayType: string): string {
return displayType
.split(' or ')
.map(type => type.replace(/^(string|object|number|integer|array|boolean)( ?.*)/, '$1s$2'))
.join(' or ');
}