Skip to content

Commit

Permalink
feat: add support prefix items
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVarchuk committed May 30, 2022
1 parent eb0917d commit 27a9dba
Show file tree
Hide file tree
Showing 13 changed files with 644 additions and 19 deletions.
20 changes: 20 additions & 0 deletions demo/openapi-3-1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,26 @@ components:
type: string
contentEncoding: base64
contentMediaType: image/png
addresses:
type: array
minItems: 0
maxLength: 10
prefixItems:
- type: object
properties:
city:
type: string
minLength: 0
country:
type: string
minLength: 0
street:
description: includes build/apartment number
type: string
minLength: 0
- type: number
items:
type: string
if:
title: userStatus === 10
properties:
Expand Down
20 changes: 20 additions & 0 deletions demo/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,26 @@ components:
description: User status
type: integer
format: int32
addresses:
type: array
minItems: 0
maxLength: 10
items:
- type: object
properties:
city:
type: string
minLength: 0
country:
type: string
minLength: 0
street:
description: includes build/apartment number
type: string
minLength: 0
- type: number
additionalItems:
type: string
xml:
name: User
requestBodies:
Expand Down
2 changes: 1 addition & 1 deletion src/components/Fields/ArrayItemDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function ArrayItemDetails({ schema }: { schema: SchemaModel }) {
((!schema?.pattern || hideSchemaPattern) &&
!schema.items &&
!schema.displayFormat &&
!schema.constraints.length)
!schema.constraints.length) // return null for cases where all constraints are empty
) {
return null;
}
Expand Down
4 changes: 4 additions & 0 deletions src/components/Schema/ArraySchema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ArrayClosingLabel, ArrayOpenningLabel } from '../../common-elements';
import styled from '../../styled-components';
import { humanizeConstraints } from '../../utils';
import { TypeName } from '../../common-elements/fields';
import { ObjectSchema } from './ObjectSchema';

const PaddedSchema = styled.div`
padding-left: ${({ theme }) => theme.spacing.unit * 2}px;
Expand All @@ -21,6 +22,9 @@ export class ArraySchema extends React.PureComponent<SchemaProps> {
? ''
: `(${humanizeConstraints(schema)})`;

if (schema.fields) {
return <ObjectSchema {...(this.props as any)} level={this.props.level} />;
}
if (schema.displayType && !itemsSchema && !minMaxItems.length) {
return (
<div>
Expand Down
16 changes: 13 additions & 3 deletions src/services/OpenAPIParser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { OpenAPIRef, OpenAPISchema, OpenAPISpec, Referenced } from '../types';

import { appendToMdHeading, isArray, IS_BROWSER } from '../utils/';
import { appendToMdHeading, isArray, isBoolean, IS_BROWSER } from '../utils/';
import { JsonPointer } from '../utils/JsonPointer';
import {
getDefinitionName,
Expand Down Expand Up @@ -318,9 +318,19 @@ export class OpenAPIParser {
}

if (items !== undefined) {
receiver.items = receiver.items || {};
const receiverItems = isBoolean(receiver.items)
? { items: receiver.items }
: receiver.items
? (Object.assign({}, receiver.items) as OpenAPISchema)
: {};
const subSchemaItems = isBoolean(items)
? { items }
: (Object.assign({}, items) as OpenAPISchema);
// merge inner properties
receiver.items = this.mergeAllOf({ allOf: [receiver.items, items] }, $ref + '/items');
receiver.items = this.mergeAllOf(
{ allOf: [receiverItems, subSchemaItems] },
$ref + '/items',
);
}

if (required !== undefined) {
Expand Down
154 changes: 154 additions & 0 deletions src/services/__tests__/fixtures/3.1/prefixItems.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
{
"openapi": "3.1.0",
"info": {
"title": "Schema definition with prefixItems",
"version": "1.0.0"
},
"servers": [
{
"url": "example.com"
}
],
"components": {
"schemas": {
"Case1": {
"type": "array",
"minItems": 1,
"maxItems": 10,
"prefixItems": [
{
"type": "string",
"minLength": 0,
"maxLength": 10
},
{
"type": "number",
"minimum": 0,
"maximum": 10
},
{
"$ref": "#/components/schemas/Cat"
}
],
"items": false
},
"Case2": {
"type": "array",
"minItems": 1,
"maxItems": 10,
"prefixItems": [
{
"type": "string",
"minLength": 0,
"maxLength": 10
},
{
"type": "number",
"minimum": 0,
"maximum": 10
},
{
"$ref": "#/components/schemas/Cat"
}
],
"items": true
},
"Case3": {
"type": "array",
"minItems": 1,
"maxItems": 10,
"prefixItems": [
{
"type": "string",
"minLength": 0,
"maxLength": 10
},
{
"type": "number",
"minimum": 0,
"maximum": 10
},
{
"$ref": "#/components/schemas/Cat"
}
],
"items": {
"$ref": "#/components/schemas/Dog"
}
},
"Case4": {
"type": "array",
"minItems": 1,
"maxItems": 10,
"prefixItems": [
{
"type": "string",
"minLength": 0,
"maxLength": 10
},
{
"type": "number",
"minimum": 0,
"maximum": 10
},
{
"$ref": "#/components/schemas/Cat"
}
],
"items": {
"type": "object",
"properties": {
"firstItem": {
"type": "string"
}
}
}
},
"Case5": {
"type": "array",
"minItems": 1,
"maxItems": 10,
"prefixItems": [
{
"type": "string",
"minLength": 0,
"maxLength": 10
},
{
"type": "number",
"minimum": 0,
"maximum": 10
},
{
"$ref": "#/components/schemas/Cat"
}
],
"items": {
"type": "array",
"items": [
{
"type": "string",
"minLength": 0
}
]
}
},
"Cat": {
"type": "object",
"properties": {
"color": {
"type": "string"
}
}
},
"Dog": {
"type": "object",
"properties": {
"size": {
"type": "string"
}
}
}
}
}
}
Loading

0 comments on commit 27a9dba

Please sign in to comment.