fix: exclusiveMin/Max shows incorect range

This commit is contained in:
Oprysk 2021-11-23 14:35:01 +02:00
parent bf6b85d4ba
commit 3662a1b027

View File

@ -113,7 +113,10 @@ export function detectType(schema: OpenAPISchema): string {
return 'any';
}
export function isPrimitiveType(schema: OpenAPISchema, type: string | string[] | undefined = schema.type) {
export function isPrimitiveType(
schema: OpenAPISchema,
type: string | string[] | undefined = schema.type,
) {
if (schema.oneOf !== undefined || schema.anyOf !== undefined) {
return false;
}
@ -122,7 +125,8 @@ export function isPrimitiveType(schema: OpenAPISchema, type: string | string[] |
const isArray = Array.isArray(type);
if (type === 'object' || (isArray && type?.includes('object'))) {
isPrimitive = schema.properties !== undefined
isPrimitive =
schema.properties !== undefined
? Object.keys(schema.properties).length === 0
: schema.additionalProperties === undefined;
}
@ -144,10 +148,10 @@ export function isFormUrlEncoded(contentType: string): boolean {
function delimitedEncodeField(fieldVal: any, fieldName: string, delimiter: string): string {
if (Array.isArray(fieldVal)) {
return fieldVal.map(v => v.toString()).join(delimiter);
return fieldVal.map((v) => v.toString()).join(delimiter);
} else if (typeof fieldVal === 'object') {
return Object.keys(fieldVal)
.map(k => `${k}${delimiter}${fieldVal[k]}`)
.map((k) => `${k}${delimiter}${fieldVal[k]}`)
.join(delimiter);
} else {
return fieldName + '=' + fieldVal.toString();
@ -160,7 +164,7 @@ function deepObjectEncodeField(fieldVal: any, fieldName: string): string {
return '';
} else if (typeof fieldVal === 'object') {
return Object.keys(fieldVal)
.map(k => `${fieldName}[${k}]=${fieldVal[k]}`)
.map((k) => `${fieldName}[${k}]=${fieldVal[k]}`)
.join('&');
} else {
console.warn('deepObject style cannot be used with non-object value:' + fieldVal.toString());
@ -192,7 +196,7 @@ export function urlFormEncodePayload(
throw new Error('Payload must have fields: ' + payload.toString());
} else {
return Object.keys(payload)
.map(fieldName => {
.map((fieldName) => {
const fieldVal = payload[fieldName];
const { style = 'form', explode = true } = encoding[fieldName] || {};
switch (style) {
@ -376,7 +380,7 @@ export function isNamedDefinition(pointer?: string): boolean {
export function getDefinitionName(pointer?: string): string | undefined {
if (!pointer) return undefined;
const match = pointer.match(/^#\/components\/(schemas|pathItems)\/([^\/]+)$/);
return match === null ? undefined : match[1]
return match === null ? undefined : match[1];
}
function humanizeMultipleOfConstraint(multipleOf: number | undefined): string | undefined {
@ -448,16 +452,18 @@ export function humanizeConstraints(schema: OpenAPISchema): string[] {
numberRange += schema.minimum;
}
if (typeof schema.exclusiveMinimum === 'number' || typeof schema.exclusiveMaximum === 'number') {
let minimum = 0;
let maximum = 0;
if (schema.minimum) minimum = schema.minimum;
if (typeof schema.exclusiveMinimum === 'number') minimum = minimum <= schema.exclusiveMinimum ? minimum : schema.exclusiveMinimum;
if (schema.maximum) maximum = schema.maximum;
if (typeof schema.exclusiveMaximum === 'number') maximum = maximum > schema.exclusiveMaximum ? maximum : schema.exclusiveMaximum;
numberRange = `[${minimum} .. ${maximum}]`
if (typeof schema.exclusiveMinimum === 'number' && typeof schema.exclusiveMaximum === 'number') {
numberRange = '[';
numberRange += schema.exclusiveMinimum;
numberRange += ' .. ';
numberRange += schema.exclusiveMaximum;
numberRange += ']';
} else if (typeof schema.exclusiveMinimum === 'number') {
numberRange = '> ';
numberRange += schema.exclusiveMinimum;
} else if (typeof schema.exclusiveMaximum === 'number') {
numberRange = '< ';
numberRange += schema.exclusiveMaximum;
}
if (numberRange !== undefined) {
@ -476,7 +482,7 @@ export function sortByRequired(fields: FieldModel[], order: string[] = []) {
const orderedFields: FieldModel[] = [];
const unorderedFields: FieldModel[] = [];
fields.forEach(field => {
fields.forEach((field) => {
if (field.required) {
order.includes(field.name) ? orderedFields.push(field) : unorderedFields.push(field);
} else {
@ -504,13 +510,13 @@ export function mergeParams(
operationParams: Array<Referenced<OpenAPIParameter>> = [],
): Array<Referenced<OpenAPIParameter>> {
const operationParamNames = {};
operationParams.forEach(param => {
operationParams.forEach((param) => {
param = parser.shallowDeref(param);
operationParamNames[param.name + '_' + param.in] = true;
});
// filter out path params overridden by operation ones with the same name
pathParams = pathParams.filter(param => {
pathParams = pathParams.filter((param) => {
param = parser.shallowDeref(param);
return !operationParamNames[param.name + '_' + param.in];
});
@ -522,7 +528,7 @@ export function mergeSimilarMediaTypes(
types: Record<string, OpenAPIMediaType>,
): Record<string, OpenAPIMediaType> {
const mergedTypes = {};
Object.keys(types).forEach(name => {
Object.keys(types).forEach((name) => {
const mime = types[name];
// ignore content type parameters (e.g. charset) and merge
const normalizedMimeName = name.split(';')[0].trim();
@ -570,7 +576,7 @@ export function normalizeServers(
return resolveUrl(baseUrl, url);
}
return servers.map(server => {
return servers.map((server) => {
return {
...server,
url: normalizeUrl(server.url),
@ -588,7 +594,7 @@ export function setSecuritySchemePrefix(prefix: string) {
SECURITY_SCHEMES_SECTION_PREFIX = prefix;
}
export const shortenHTTPVerb = verb =>
export const shortenHTTPVerb = (verb) =>
({
delete: 'del',
options: 'opts',
@ -619,7 +625,7 @@ export function extractExtensions(
showExtensions: string[] | true,
): Record<string, any> {
return Object.keys(obj)
.filter(key => {
.filter((key) => {
if (showExtensions === true) {
return key.startsWith('x-') && !isRedocExtension(key);
}
@ -634,6 +640,6 @@ export function extractExtensions(
export function pluralizeType(displayType: string): string {
return displayType
.split(' or ')
.map(type => type.replace(/^(string|object|number|integer|array|boolean)s?( ?.*)/, '$1s$2'))
.map((type) => type.replace(/^(string|object|number|integer|array|boolean)s?( ?.*)/, '$1s$2'))
.join(' or ');
}