Skip to content

Commit

Permalink
fix: pluralize arrray of types
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed May 12, 2019
1 parent 5c187f3 commit fdcac30
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/services/__tests__/models/Schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
3 changes: 2 additions & 1 deletion src/services/models/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
isNamedDefinition,
isPrimitiveType,
JsonPointer,
pluralizeType,
sortByField,
sortByRequired,
} from '../../utils/';
Expand Down Expand Up @@ -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;
Expand Down
24 changes: 24 additions & 0 deletions src/utils/__tests__/openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
isPrimitiveType,
mergeParams,
normalizeServers,
pluralizeType,
} from '../';

import { OpenAPIParser } from '../../services';
Expand Down Expand Up @@ -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 <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>',
);
});
});
});
7 changes: 7 additions & 0 deletions src/utils/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ');
}

0 comments on commit fdcac30

Please sign in to comment.