From cd2d6f76e87c8385786a9c8e51c0d11c79d9707c Mon Sep 17 00:00:00 2001 From: AlexVarchuk Date: Tue, 3 Aug 2021 13:36:06 +0300 Subject: [PATCH] fix: improve openapi 3.1 (#1700) --- demo/openapi-3-1.yaml | 5 +++++ src/components/Fields/FieldDetails.tsx | 18 ++++++++++++++++-- src/components/Schema/ArraySchema.tsx | 13 +++++++++++-- .../DiscriminatorDropdown.test.tsx.snap | 4 ++++ src/services/models/Schema.ts | 10 ++++++++-- src/types/open-api.ts | 2 ++ .../loadAndBundleSpec.test.ts.snap | 6 ++++++ src/utils/openapi.ts | 2 ++ 8 files changed, 54 insertions(+), 6 deletions(-) diff --git a/demo/openapi-3-1.yaml b/demo/openapi-3-1.yaml index c905d4d8ae..31063d4b53 100644 --- a/demo/openapi-3-1.yaml +++ b/demo/openapi-3-1.yaml @@ -1166,6 +1166,11 @@ components: description: User status type: integer format: int32 + image: + description: User image + type: string + contentEncoding: base64 + contentMediaType: image/png xml: name: User requestBodies: diff --git a/src/components/Fields/FieldDetails.tsx b/src/components/Fields/FieldDetails.tsx index 9ac6cea235..1aad8c6182 100644 --- a/src/components/Fields/FieldDetails.tsx +++ b/src/components/Fields/FieldDetails.tsx @@ -59,7 +59,7 @@ export class FieldDetails extends React.PureComponent; + renderedExamples = ; } } @@ -76,6 +76,20 @@ export class FieldDetails extends React.PureComponent )} + {schema.contentEncoding && ( + + {' '}< + {schema.contentEncoding} + >{' '} + + )} + {schema.contentMediaType && ( + + {' '}< + {schema.contentMediaType} + >{' '} + + )} {schema.title && !hideSchemaTitles && ({schema.title}) } {schema.pattern && !hideSchemaPattern && ( @@ -110,7 +124,7 @@ export class FieldDetails extends React.PureComponent )} {(renderDiscriminatorSwitch && renderDiscriminatorSwitch(this.props)) || null} - {field.const && () || null} + {field.const && () || null} ); } diff --git a/src/components/Schema/ArraySchema.tsx b/src/components/Schema/ArraySchema.tsx index 48e7dc4135..eab3759575 100644 --- a/src/components/Schema/ArraySchema.tsx +++ b/src/components/Schema/ArraySchema.tsx @@ -4,7 +4,8 @@ import { Schema, SchemaProps } from './Schema'; import { ArrayClosingLabel, ArrayOpenningLabel } from '../../common-elements'; import styled from '../../styled-components'; -import {humanizeConstraints} from "../../utils"; +import { humanizeConstraints } from '../../utils'; +import { TypeName } from '../../common-elements/fields'; const PaddedSchema = styled.div` padding-left: ${({ theme }) => theme.spacing.unit * 2}px; @@ -13,12 +14,20 @@ const PaddedSchema = styled.div` export class ArraySchema extends React.PureComponent { render() { const itemsSchema = this.props.schema.items!; + const schema = this.props.schema; + 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 = humanizeConstraints(itemConstraintSchema(itemsSchema?.schema?.minItems, itemsSchema?.schema?.maxItems)); + + if (schema.displayType && !itemsSchema && !minMaxItems.length) { + return (
+ {schema.displayType} +
); + } return (
diff --git a/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap b/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap index f293088f37..fe5617ed56 100644 --- a/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap @@ -20,6 +20,8 @@ exports[`Components SchemaView discriminator should correctly render discriminat "activeOneOf": 0, "const": "", "constraints": Array [], + "contentEncoding": undefined, + "contentMediaType": undefined, "default": undefined, "deprecated": false, "description": "", @@ -71,6 +73,8 @@ exports[`Components SchemaView discriminator should correctly render discriminat "activeOneOf": 0, "const": "", "constraints": Array [], + "contentEncoding": undefined, + "contentMediaType": undefined, "default": undefined, "deprecated": false, "description": "", diff --git a/src/services/models/Schema.ts b/src/services/models/Schema.ts index adb2de197b..e5b36ae1e0 100644 --- a/src/services/models/Schema.ts +++ b/src/services/models/Schema.ts @@ -61,6 +61,8 @@ export class SchemaModel { schema: MergedOpenAPISchema; extensions?: Record; const: any; + contentEncoding?: string; + contentMediaType?: string; /** * @param isChild if schema discriminator Child @@ -120,10 +122,14 @@ export class SchemaModel { this.readOnly = !!schema.readOnly; this.writeOnly = !!schema.writeOnly; this.const = schema.const || ''; + this.contentEncoding = schema.contentEncoding; + this.contentMediaType = schema.contentMediaType; - if (!!schema.nullable) { - if (Array.isArray(this.type) && !this.type.includes('null')) { + if (!!schema.nullable || schema['x-nullable']) { + if (Array.isArray(this.type) && !this.type.some((value) => value === null || value === 'null')) { this.type = [...this.type, 'null']; + } else if (!Array.isArray(this.type) && (this.type !== null || this.type !== 'null')) { + this.type = [this.type, 'null']; } } diff --git a/src/types/open-api.ts b/src/types/open-api.ts index d55e44b13b..03898457d3 100644 --- a/src/types/open-api.ts +++ b/src/types/open-api.ts @@ -147,6 +147,8 @@ export interface OpenAPISchema { enum?: any[]; example?: any; const?: string; + contentEncoding?: string; + contentMediaType?: string; } export interface OpenAPIDiscriminator { diff --git a/src/utils/__tests__/__snapshots__/loadAndBundleSpec.test.ts.snap b/src/utils/__tests__/__snapshots__/loadAndBundleSpec.test.ts.snap index 1aa507f97e..7807e39ed7 100644 --- a/src/utils/__tests__/__snapshots__/loadAndBundleSpec.test.ts.snap +++ b/src/utils/__tests__/__snapshots__/loadAndBundleSpec.test.ts.snap @@ -2187,6 +2187,12 @@ Object { "id": Object { "$ref": "#/components/schemas/Id", }, + "image": Object { + "contentEncoding": "base64", + "contentMediaType": "image/png", + "description": "User image", + "type": "string", + }, "lastName": Object { "description": "User last name", "example": "Smith", diff --git a/src/utils/openapi.ts b/src/utils/openapi.ts index c31e3cd21c..ad267722d5 100644 --- a/src/utils/openapi.ts +++ b/src/utils/openapi.ts @@ -83,6 +83,8 @@ const schemaKeywordTypes = { maxLength: 'string', minLength: 'string', pattern: 'string', + contentEncoding: 'string', + contentMediaType: 'string', items: 'array', maxItems: 'array',