mirror of
https://github.com/Redocly/redoc.git
synced 2025-02-17 02:10:39 +03:00
fix: pluralize arrray of types
This commit is contained in:
parent
5c187f34c9
commit
fdcac30829
|
@ -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');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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>',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -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 ');
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user