Skip to content

Commit

Permalink
fix: The number of items in the array in the array is incorrect #1762 (
Browse files Browse the repository at this point in the history
  • Loading branch information
zalesky authored Oct 11, 2021
1 parent 43451ba commit 3b8d644
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 12 deletions.
14 changes: 14 additions & 0 deletions demo/openapi-3-1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}':
Expand Down
2 changes: 2 additions & 0 deletions demo/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,9 @@ paths:
application/xml:
schema:
type: array
maxItems: 999
items:
maxItems: 111
$ref: '#/components/schemas/Pet'
'400':
description: Invalid tag value
Expand Down
13 changes: 5 additions & 8 deletions src/components/Schema/ArraySchema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@ const PaddedSchema = styled.div`

export class ArraySchema extends React.PureComponent<SchemaProps> {
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 (<div>
Expand All @@ -31,7 +28,7 @@ export class ArraySchema extends React.PureComponent<SchemaProps> {

return (
<div>
<ArrayOpenningLabel> Array ({minMaxItems})</ArrayOpenningLabel>
<ArrayOpenningLabel> Array {minMaxItems}</ArrayOpenningLabel>
<PaddedSchema>
<Schema {...this.props} schema={itemsSchema} />
</PaddedSchema>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ exports[`Components SchemaView discriminator should correctly render discriminat
"format": undefined,
"isCircular": undefined,
"isPrimitive": true,
"maxItems": undefined,
"minItems": undefined,
"options": "<<<filtered>>>",
"pattern": undefined,
"pointer": "#/components/schemas/Dog/properties/packSize",
Expand Down Expand Up @@ -86,6 +88,8 @@ exports[`Components SchemaView discriminator should correctly render discriminat
"format": undefined,
"isCircular": undefined,
"isPrimitive": true,
"maxItems": undefined,
"minItems": undefined,
"options": "<<<filtered>>>",
"pattern": undefined,
"pointer": "#/components/schemas/Dog/properties/type",
Expand Down
4 changes: 4 additions & 0 deletions src/services/models/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export class SchemaModel {
const: any;
contentEncoding?: string;
contentMediaType?: string;
minItems?: number;
maxItems?: number;

/**
* @param isChild if schema discriminator Child
Expand Down Expand Up @@ -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')) {
Expand Down
22 changes: 22 additions & 0 deletions src/utils/__tests__/__snapshots__/loadAndBundleSpec.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,9 @@ try {
"schema": Object {
"items": Object {
"$ref": "#/components/schemas/Pet",
"maxItems": 111,
},
"maxItems": 999,
"type": "array",
},
},
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions src/utils/__tests__/openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 3b8d644

Please sign in to comment.