From 5b9aa27af03a1c4616f7e0195afeba47d1deeaa0 Mon Sep 17 00:00:00 2001 From: Alex Varchuk Date: Fri, 6 May 2022 19:15:31 +0300 Subject: [PATCH] fix: examples in json schemas --- src/components/Fields/Examples.tsx | 24 +++++++++++++------ .../DiscriminatorDropdown.test.tsx.snap | 12 ++++++++++ src/services/models/Field.ts | 17 +++++++------ src/services/models/Schema.ts | 2 ++ src/types/open-api.ts | 1 + 5 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/components/Fields/Examples.tsx b/src/components/Fields/Examples.tsx index 120d7c55..c1fcfaab 100644 --- a/src/components/Fields/Examples.tsx +++ b/src/components/Fields/Examples.tsx @@ -1,7 +1,7 @@ import * as React from 'react'; import { FieldLabel, ExampleValue } from '../../common-elements/fields'; -import { getSerializedValue } from '../../utils'; +import { getSerializedValue, isArray } from '../../utils'; import { l } from '../../services/Labels'; import { FieldModel } from '../../services'; @@ -15,16 +15,26 @@ export function Examples({ field }: { field: FieldModel }) { return ( <> {l('examples')}: - - {Object.values(field.examples).map((example, idx) => { + {isArray(field.examples) ? ( + field.examples.map((example, idx) => { + const value = getSerializedValue(field, example); + const stringifyValue = field.in ? String(value) : JSON.stringify(value); return ( -
  • + + {stringifyValue}{' '} + + ); + }) + ) : ( + + {Object.values(field.examples).map((example, idx) => ( +
  • {getSerializedValue(field, example.value)} -{' '} {example.summary || example.description}
  • - ); - })} -
    + ))} + + )} ); } diff --git a/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap b/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap index d9490d64..a227c7da 100644 --- a/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap @@ -19,6 +19,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView "displayType": "object", "enum": Array [], "example": undefined, + "examples": undefined, "externalDocs": undefined, "format": undefined, "isCircular": undefined, @@ -39,6 +40,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView "displayType": "object", "enum": Array [], "example": undefined, + "examples": undefined, "externalDocs": undefined, "fields": Array [ FieldModel { @@ -65,6 +67,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView "displayType": "number", "enum": Array [], "example": undefined, + "examples": undefined, "externalDocs": undefined, "format": undefined, "isCircular": undefined, @@ -316,6 +319,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView "displayType": "string", "enum": Array [], "example": undefined, + "examples": undefined, "externalDocs": undefined, "format": undefined, "isCircular": undefined, @@ -809,6 +813,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView "displayType": "object", "enum": Array [], "example": undefined, + "examples": undefined, "externalDocs": undefined, "fields": Array [ FieldModel { @@ -835,6 +840,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView "displayType": "string", "enum": Array [], "example": undefined, + "examples": undefined, "externalDocs": undefined, "format": undefined, "isCircular": undefined, @@ -1086,6 +1092,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView "displayType": "number", "enum": Array [], "example": undefined, + "examples": undefined, "externalDocs": undefined, "format": undefined, "isCircular": undefined, @@ -1826,6 +1833,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView "displayType": "object", "enum": Array [], "example": undefined, + "examples": undefined, "externalDocs": undefined, "fields": Array [ FieldModel { @@ -1852,6 +1860,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView "displayType": "number", "enum": Array [], "example": undefined, + "examples": undefined, "externalDocs": undefined, "format": undefined, "isCircular": undefined, @@ -2103,6 +2112,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView "displayType": "string", "enum": Array [], "example": undefined, + "examples": undefined, "externalDocs": undefined, "format": undefined, "isCircular": undefined, @@ -2617,6 +2627,7 @@ exports[`Components SchemaView discriminator should correctly render discriminat "displayType": "number", "enum": Array [], "example": undefined, + "examples": undefined, "externalDocs": undefined, "format": undefined, "isCircular": undefined, @@ -2673,6 +2684,7 @@ exports[`Components SchemaView discriminator should correctly render discriminat "displayType": "string", "enum": Array [], "example": undefined, + "examples": undefined, "externalDocs": undefined, "format": undefined, "isCircular": undefined, diff --git a/src/services/models/Field.ts b/src/services/models/Field.ts index 84efa5f0..83c6ad6d 100644 --- a/src/services/models/Field.ts +++ b/src/services/models/Field.ts @@ -12,7 +12,7 @@ import { extractExtensions } from '../../utils/openapi'; import { OpenAPIParser } from '../OpenAPIParser'; import { SchemaModel } from './Schema'; import { ExampleModel } from './Example'; -import { mapValues } from '../../utils/helpers'; +import { isArray, mapValues } from '../../utils/helpers'; const DEFAULT_SERIALIZATION: Record< OpenAPIParameterLocation, @@ -48,7 +48,7 @@ export class FieldModel { required: boolean; description: string; example?: string; - examples?: Record; + examples?: Record | any[]; deprecated: boolean; in?: OpenAPIParameterLocation; kind: string; @@ -85,11 +85,14 @@ export class FieldModel { info.description === undefined ? this.schema.description || '' : info.description; this.example = info.example || this.schema.example; - if (info.examples !== undefined) { - this.examples = mapValues( - info.examples, - (example, name) => new ExampleModel(parser, example, name, info.encoding), - ); + if (info.examples !== undefined || this.schema.examples !== undefined) { + const exampleValue = info.examples || this.schema.examples; + this.examples = isArray(exampleValue) + ? exampleValue + : mapValues( + exampleValue!, + (example, name) => new ExampleModel(parser, example, name, info.encoding), + ); } if (serializationMime) { diff --git a/src/services/models/Schema.ts b/src/services/models/Schema.ts index a0a3b38c..c0754b4a 100644 --- a/src/services/models/Schema.ts +++ b/src/services/models/Schema.ts @@ -42,6 +42,7 @@ export class SchemaModel { deprecated: boolean; pattern?: string; example?: any; + examples?: any[]; enum: any[]; default?: any; readOnly: boolean; @@ -118,6 +119,7 @@ export class SchemaModel { this.format = schema.format; this.enum = schema.enum || []; this.example = schema.example; + this.examples = schema.examples; this.deprecated = !!schema.deprecated; this.pattern = schema.pattern; this.externalDocs = schema.externalDocs; diff --git a/src/types/open-api.ts b/src/types/open-api.ts index 3d74e380..98ce1f9e 100644 --- a/src/types/open-api.ts +++ b/src/types/open-api.ts @@ -147,6 +147,7 @@ export interface OpenAPISchema { minProperties?: number; enum?: any[]; example?: any; + examples?: any[]; const?: string; contentEncoding?: string; contentMediaType?: string;