From 3b8d6441bd9978b849a53021d40fd4fe150272ea Mon Sep 17 00:00:00 2001 From: Andriy Zaleskyy Date: Mon, 11 Oct 2021 13:05:50 +0300 Subject: [PATCH] fix: The number of items in the array in the array is incorrect #1762 (#1763) --- demo/openapi-3-1.yaml | 14 ++++++++++++ demo/openapi.yaml | 2 ++ src/components/Schema/ArraySchema.tsx | 13 +++++------ .../DiscriminatorDropdown.test.tsx.snap | 4 ++++ src/services/models/Schema.ts | 4 ++++ .../loadAndBundleSpec.test.ts.snap | 22 +++++++++++++++++++ src/utils/__tests__/openapi.test.ts | 8 +++---- 7 files changed, 55 insertions(+), 12 deletions(-) diff --git a/demo/openapi-3-1.yaml b/demo/openapi-3-1.yaml index 31063d4b..ac89619d 100644 --- a/demo/openapi-3-1.yaml +++ b/demo/openapi-3-1.yaml @@ -533,6 +533,20 @@ paths: subscriptionId: type: string example: AAA-123-BBB-456 + '200': + description: Successful operation + content: + application/json: + schema: + type: array + maxItems: 999 + minItems: 0 + items: + type: array + maxItems: 777 + minItems: 111 + items: + type: number callbacks: orderInProgress: '{$request.body#/callbackUrl}?event={$request.body#/eventName}': diff --git a/demo/openapi.yaml b/demo/openapi.yaml index c570cc60..5cf19340 100644 --- a/demo/openapi.yaml +++ b/demo/openapi.yaml @@ -377,7 +377,9 @@ paths: application/xml: schema: type: array + maxItems: 999 items: + maxItems: 111 $ref: '#/components/schemas/Pet' '400': description: Invalid tag value diff --git a/src/components/Schema/ArraySchema.tsx b/src/components/Schema/ArraySchema.tsx index eab37595..0b555c07 100644 --- a/src/components/Schema/ArraySchema.tsx +++ b/src/components/Schema/ArraySchema.tsx @@ -13,15 +13,12 @@ const PaddedSchema = styled.div` export class ArraySchema extends React.PureComponent { render() { - const itemsSchema = this.props.schema.items!; const schema = this.props.schema; + const itemsSchema = schema.items; - const itemConstraintSchema = ( - min: number | undefined = undefined, - max: number | undefined = undefined, - ) => ({ type: 'array', minItems: min, maxItems: max }); - - const minMaxItems = humanizeConstraints(itemConstraintSchema(itemsSchema?.schema?.minItems, itemsSchema?.schema?.maxItems)); + const minMaxItems = schema.minItems === undefined && schema.maxItems === undefined ? + '' : + `(${humanizeConstraints(schema)})`; if (schema.displayType && !itemsSchema && !minMaxItems.length) { return (
@@ -31,7 +28,7 @@ export class ArraySchema extends React.PureComponent { return (
- Array ({minMaxItems}) + Array {minMaxItems} diff --git a/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap b/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap index fe5617ed..7af7b482 100644 --- a/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap @@ -33,6 +33,8 @@ exports[`Components SchemaView discriminator should correctly render discriminat "format": undefined, "isCircular": undefined, "isPrimitive": true, + "maxItems": undefined, + "minItems": undefined, "options": "<<>>", "pattern": undefined, "pointer": "#/components/schemas/Dog/properties/packSize", @@ -86,6 +88,8 @@ exports[`Components SchemaView discriminator should correctly render discriminat "format": undefined, "isCircular": undefined, "isPrimitive": true, + "maxItems": undefined, + "minItems": undefined, "options": "<<>>", "pattern": undefined, "pointer": "#/components/schemas/Dog/properties/type", diff --git a/src/services/models/Schema.ts b/src/services/models/Schema.ts index 1b23cf5f..81dc7ee3 100644 --- a/src/services/models/Schema.ts +++ b/src/services/models/Schema.ts @@ -63,6 +63,8 @@ export class SchemaModel { const: any; contentEncoding?: string; contentMediaType?: string; + minItems?: number; + maxItems?: number; /** * @param isChild if schema discriminator Child @@ -128,6 +130,8 @@ export class SchemaModel { this.const = schema.const || ''; this.contentEncoding = schema.contentEncoding; this.contentMediaType = schema.contentMediaType; + this.minItems = schema.minItems; + this.maxItems = schema.maxItems; if (!!schema.nullable || schema['x-nullable']) { if (Array.isArray(this.type) && !this.type.some((value) => value === null || value === 'null')) { diff --git a/src/utils/__tests__/__snapshots__/loadAndBundleSpec.test.ts.snap b/src/utils/__tests__/__snapshots__/loadAndBundleSpec.test.ts.snap index 7807e39e..6cd0a034 100644 --- a/src/utils/__tests__/__snapshots__/loadAndBundleSpec.test.ts.snap +++ b/src/utils/__tests__/__snapshots__/loadAndBundleSpec.test.ts.snap @@ -729,7 +729,9 @@ try { "schema": Object { "items": Object { "$ref": "#/components/schemas/Pet", + "maxItems": 111, }, + "maxItems": 999, "type": "array", }, }, @@ -3245,6 +3247,26 @@ culpa qui officia deserunt mollit anim id est laborum. }, }, "responses": Object { + "200": Object { + "content": Object { + "application/json": Object { + "schema": Object { + "items": Object { + "items": Object { + "type": "number", + }, + "maxItems": 777, + "minItems": 111, + "type": "array", + }, + "maxItems": 999, + "minItems": 0, + "type": "array", + }, + }, + }, + "description": "Successful operation", + }, "201": Object { "content": Object { "application/json": Object { diff --git a/src/utils/__tests__/openapi.test.ts b/src/utils/__tests__/openapi.test.ts index d4f23e02..6f40d974 100644 --- a/src/utils/__tests__/openapi.test.ts +++ b/src/utils/__tests__/openapi.test.ts @@ -412,10 +412,10 @@ describe('Utils', () => { describe('openapi humanizeConstraints', () => { const itemConstraintSchema = ( - min: number | undefined = undefined, - max: number | undefined = undefined, - multipleOf: number | undefined = undefined, - uniqueItems?: boolean, + min?: number, + max?: number, + multipleOf?: number, + uniqueItems?: boolean ) => ({ type: 'array', minItems: min, maxItems: max, multipleOf, uniqueItems }); it('should not have a humanized constraint without schema constraints', () => {