fix: examples in json schemas

This commit is contained in:
Alex Varchuk 2022-05-06 19:15:31 +03:00 committed by AlexVarchuk
parent e7a6d38a56
commit 5b9aa27af0
5 changed files with 42 additions and 14 deletions

View File

@ -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 (
<>
<FieldLabel> {l('examples')}: </FieldLabel>
<ExamplesList>
{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 (
<li key={idx}>
<React.Fragment key={idx}>
<ExampleValue>{stringifyValue}</ExampleValue>{' '}
</React.Fragment>
);
})
) : (
<ExamplesList>
{Object.values(field.examples).map((example, idx) => (
<li key={idx + example.value}>
<ExampleValue>{getSerializedValue(field, example.value)}</ExampleValue> -{' '}
{example.summary || example.description}
</li>
);
})}
))}
</ExamplesList>
)}
</>
);
}

View File

@ -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,

View File

@ -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<string, ExampleModel>;
examples?: Record<string, ExampleModel> | any[];
deprecated: boolean;
in?: OpenAPIParameterLocation;
kind: string;
@ -85,9 +85,12 @@ 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,
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),
);
}

View File

@ -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;

View File

@ -147,6 +147,7 @@ export interface OpenAPISchema {
minProperties?: number;
enum?: any[];
example?: any;
examples?: any[];
const?: string;
contentEncoding?: string;
contentMediaType?: string;