From fdcac3082926c81339841500fda92fc8f652d7bd Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Sun, 12 May 2019 20:44:39 +0300 Subject: [PATCH] fix: pluralize arrray of types --- src/services/__tests__/models/Schema.test.ts | 2 +- src/services/models/Schema.ts | 3 ++- src/utils/__tests__/openapi.test.ts | 24 ++++++++++++++++++++ src/utils/openapi.ts | 7 ++++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/services/__tests__/models/Schema.test.ts b/src/services/__tests__/models/Schema.test.ts index b0c164ef0d..ed75fce602 100644 --- a/src/services/__tests__/models/Schema.test.ts +++ b/src/services/__tests__/models/Schema.test.ts @@ -37,7 +37,7 @@ describe('Models', () => { parser = new OpenAPIParser(spec, undefined, opts); const schema = new SchemaModel(parser, spec.components.schemas.WithArray, '', opts); 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'); }); }); }); diff --git a/src/services/models/Schema.ts b/src/services/models/Schema.ts index 037340da44..668cd86d94 100644 --- a/src/services/models/Schema.ts +++ b/src/services/models/Schema.ts @@ -14,6 +14,7 @@ import { isNamedDefinition, isPrimitiveType, JsonPointer, + pluralizeType, sortByField, sortByRequired, } from '../../utils/'; @@ -145,7 +146,7 @@ export class SchemaModel { this.fields = buildFields(parser, schema, this.pointer, this.options); } else if (this.type === 'array' && schema.items) { 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.typePrefix = this.items.typePrefix + 'Array of '; this.title = this.title || this.items.title; diff --git a/src/utils/__tests__/openapi.test.ts b/src/utils/__tests__/openapi.test.ts index c6ce7838be..d026f51982 100644 --- a/src/utils/__tests__/openapi.test.ts +++ b/src/utils/__tests__/openapi.test.ts @@ -7,6 +7,7 @@ import { isPrimitiveType, mergeParams, normalizeServers, + pluralizeType, } from '../'; import { OpenAPIParser } from '../../services'; @@ -353,4 +354,27 @@ describe('Utils', () => { 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 ')).toEqual('strings '); + }); + + it('should pluralize oneOf-ed dislay types', () => { + expect(pluralizeType('object or string')).toEqual('objects or strings'); + expect(pluralizeType('object (Pet) or number ')).toEqual( + 'objects (Pet) or numbers ', + ); + }); + }); }); diff --git a/src/utils/openapi.ts b/src/utils/openapi.ts index a6d142f1d6..a500d3135d 100644 --- a/src/utils/openapi.ts +++ b/src/utils/openapi.ts @@ -434,3 +434,10 @@ export function extractExtensions(obj: object, showExtensions: string[] | true): 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 '); +}