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 * as React from 'react';
import { FieldLabel, ExampleValue } from '../../common-elements/fields'; import { FieldLabel, ExampleValue } from '../../common-elements/fields';
import { getSerializedValue } from '../../utils'; import { getSerializedValue, isArray } from '../../utils';
import { l } from '../../services/Labels'; import { l } from '../../services/Labels';
import { FieldModel } from '../../services'; import { FieldModel } from '../../services';
@ -15,16 +15,26 @@ export function Examples({ field }: { field: FieldModel }) {
return ( return (
<> <>
<FieldLabel> {l('examples')}: </FieldLabel> <FieldLabel> {l('examples')}: </FieldLabel>
<ExamplesList> {isArray(field.examples) ? (
{Object.values(field.examples).map((example, idx) => { field.examples.map((example, idx) => {
const value = getSerializedValue(field, example);
const stringifyValue = field.in ? String(value) : JSON.stringify(value);
return ( 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> -{' '} <ExampleValue>{getSerializedValue(field, example.value)}</ExampleValue> -{' '}
{example.summary || example.description} {example.summary || example.description}
</li> </li>
); ))}
})}
</ExamplesList> </ExamplesList>
)}
</> </>
); );
} }

View File

@ -19,6 +19,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"displayType": "object", "displayType": "object",
"enum": Array [], "enum": Array [],
"example": undefined, "example": undefined,
"examples": undefined,
"externalDocs": undefined, "externalDocs": undefined,
"format": undefined, "format": undefined,
"isCircular": undefined, "isCircular": undefined,
@ -39,6 +40,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"displayType": "object", "displayType": "object",
"enum": Array [], "enum": Array [],
"example": undefined, "example": undefined,
"examples": undefined,
"externalDocs": undefined, "externalDocs": undefined,
"fields": Array [ "fields": Array [
FieldModel { FieldModel {
@ -65,6 +67,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"displayType": "number", "displayType": "number",
"enum": Array [], "enum": Array [],
"example": undefined, "example": undefined,
"examples": undefined,
"externalDocs": undefined, "externalDocs": undefined,
"format": undefined, "format": undefined,
"isCircular": undefined, "isCircular": undefined,
@ -316,6 +319,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"displayType": "string", "displayType": "string",
"enum": Array [], "enum": Array [],
"example": undefined, "example": undefined,
"examples": undefined,
"externalDocs": undefined, "externalDocs": undefined,
"format": undefined, "format": undefined,
"isCircular": undefined, "isCircular": undefined,
@ -809,6 +813,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"displayType": "object", "displayType": "object",
"enum": Array [], "enum": Array [],
"example": undefined, "example": undefined,
"examples": undefined,
"externalDocs": undefined, "externalDocs": undefined,
"fields": Array [ "fields": Array [
FieldModel { FieldModel {
@ -835,6 +840,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"displayType": "string", "displayType": "string",
"enum": Array [], "enum": Array [],
"example": undefined, "example": undefined,
"examples": undefined,
"externalDocs": undefined, "externalDocs": undefined,
"format": undefined, "format": undefined,
"isCircular": undefined, "isCircular": undefined,
@ -1086,6 +1092,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"displayType": "number", "displayType": "number",
"enum": Array [], "enum": Array [],
"example": undefined, "example": undefined,
"examples": undefined,
"externalDocs": undefined, "externalDocs": undefined,
"format": undefined, "format": undefined,
"isCircular": undefined, "isCircular": undefined,
@ -1826,6 +1833,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"displayType": "object", "displayType": "object",
"enum": Array [], "enum": Array [],
"example": undefined, "example": undefined,
"examples": undefined,
"externalDocs": undefined, "externalDocs": undefined,
"fields": Array [ "fields": Array [
FieldModel { FieldModel {
@ -1852,6 +1860,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"displayType": "number", "displayType": "number",
"enum": Array [], "enum": Array [],
"example": undefined, "example": undefined,
"examples": undefined,
"externalDocs": undefined, "externalDocs": undefined,
"format": undefined, "format": undefined,
"isCircular": undefined, "isCircular": undefined,
@ -2103,6 +2112,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"displayType": "string", "displayType": "string",
"enum": Array [], "enum": Array [],
"example": undefined, "example": undefined,
"examples": undefined,
"externalDocs": undefined, "externalDocs": undefined,
"format": undefined, "format": undefined,
"isCircular": undefined, "isCircular": undefined,
@ -2617,6 +2627,7 @@ exports[`Components SchemaView discriminator should correctly render discriminat
"displayType": "number", "displayType": "number",
"enum": Array [], "enum": Array [],
"example": undefined, "example": undefined,
"examples": undefined,
"externalDocs": undefined, "externalDocs": undefined,
"format": undefined, "format": undefined,
"isCircular": undefined, "isCircular": undefined,
@ -2673,6 +2684,7 @@ exports[`Components SchemaView discriminator should correctly render discriminat
"displayType": "string", "displayType": "string",
"enum": Array [], "enum": Array [],
"example": undefined, "example": undefined,
"examples": undefined,
"externalDocs": undefined, "externalDocs": undefined,
"format": undefined, "format": undefined,
"isCircular": undefined, "isCircular": undefined,

View File

@ -12,7 +12,7 @@ import { extractExtensions } from '../../utils/openapi';
import { OpenAPIParser } from '../OpenAPIParser'; import { OpenAPIParser } from '../OpenAPIParser';
import { SchemaModel } from './Schema'; import { SchemaModel } from './Schema';
import { ExampleModel } from './Example'; import { ExampleModel } from './Example';
import { mapValues } from '../../utils/helpers'; import { isArray, mapValues } from '../../utils/helpers';
const DEFAULT_SERIALIZATION: Record< const DEFAULT_SERIALIZATION: Record<
OpenAPIParameterLocation, OpenAPIParameterLocation,
@ -48,7 +48,7 @@ export class FieldModel {
required: boolean; required: boolean;
description: string; description: string;
example?: string; example?: string;
examples?: Record<string, ExampleModel>; examples?: Record<string, ExampleModel> | any[];
deprecated: boolean; deprecated: boolean;
in?: OpenAPIParameterLocation; in?: OpenAPIParameterLocation;
kind: string; kind: string;
@ -85,9 +85,12 @@ export class FieldModel {
info.description === undefined ? this.schema.description || '' : info.description; info.description === undefined ? this.schema.description || '' : info.description;
this.example = info.example || this.schema.example; this.example = info.example || this.schema.example;
if (info.examples !== undefined) { if (info.examples !== undefined || this.schema.examples !== undefined) {
this.examples = mapValues( const exampleValue = info.examples || this.schema.examples;
info.examples, this.examples = isArray(exampleValue)
? exampleValue
: mapValues(
exampleValue!,
(example, name) => new ExampleModel(parser, example, name, info.encoding), (example, name) => new ExampleModel(parser, example, name, info.encoding),
); );
} }

View File

@ -42,6 +42,7 @@ export class SchemaModel {
deprecated: boolean; deprecated: boolean;
pattern?: string; pattern?: string;
example?: any; example?: any;
examples?: any[];
enum: any[]; enum: any[];
default?: any; default?: any;
readOnly: boolean; readOnly: boolean;
@ -118,6 +119,7 @@ export class SchemaModel {
this.format = schema.format; this.format = schema.format;
this.enum = schema.enum || []; this.enum = schema.enum || [];
this.example = schema.example; this.example = schema.example;
this.examples = schema.examples;
this.deprecated = !!schema.deprecated; this.deprecated = !!schema.deprecated;
this.pattern = schema.pattern; this.pattern = schema.pattern;
this.externalDocs = schema.externalDocs; this.externalDocs = schema.externalDocs;

View File

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