diff --git a/src/components/PayloadSamples/MediaTypeSamples.tsx b/src/components/PayloadSamples/MediaTypeSamples.tsx index 5fedce01..d175b58a 100644 --- a/src/components/PayloadSamples/MediaTypeSamples.tsx +++ b/src/components/PayloadSamples/MediaTypeSamples.tsx @@ -1,9 +1,7 @@ import * as React from 'react'; - -import styled from '../../styled-components'; - import { DropdownProps } from '../../common-elements'; import { MediaTypeModel } from '../../services/models'; +import styled from '../../styled-components'; import { Markdown } from '../Markdown/Markdown'; import { Example } from './Example'; import { DropdownLabel, DropdownWrapper, NoSampleLabel } from './styled.elements'; @@ -28,6 +26,7 @@ export class MediaTypeSamples extends React.Component { const expandByDefault = this.context.expandSingleSchemaField && filteredFields.length === 1; return ( - - {showTitle && {this.props.schema.title}} - - {mapWithLast(filteredFields, (field, isLast) => { - return ( - ( - - ))) || - undefined - } - className={field.expanded ? 'expanded' : undefined} - showExamples={false} - skipReadOnly={this.props.skipReadOnly} - skipWriteOnly={this.props.skipWriteOnly} - showTitle={this.props.showTitle} - /> - ); - })} - - +
+ + + {showTitle && {this.props.schema.title}} + + {mapWithLast(filteredFields, (field, isLast) => { + return ( + ( + + ))) || + undefined + } + className={field.expanded ? 'expanded' : undefined} + showExamples={false} + skipReadOnly={this.props.skipReadOnly} + skipWriteOnly={this.props.skipWriteOnly} + showTitle={this.props.showTitle} + /> + ); + })} + + +
); } } diff --git a/src/components/Schema/Schema.tsx b/src/components/Schema/Schema.tsx index 2a533190..eea6cb84 100644 --- a/src/components/Schema/Schema.tsx +++ b/src/components/Schema/Schema.tsx @@ -1,17 +1,13 @@ import { observer } from 'mobx-react'; import * as React from 'react'; - import { RecursiveLabel, TypeName, TypeTitle } from '../../common-elements/fields'; -import { FieldDetails } from '../Fields/FieldDetails'; - +import { l } from '../../services/Labels'; import { FieldModel, SchemaModel } from '../../services/models'; - +import { FieldDetails } from '../Fields/FieldDetails'; import { ArraySchema } from './ArraySchema'; import { ObjectSchema } from './ObjectSchema'; import { OneOfSchema } from './OneOfSchema'; -import { l } from '../../services/Labels'; - export interface SchemaOptions { showTitle?: boolean; skipReadOnly?: boolean; @@ -43,10 +39,9 @@ export class Schema extends React.Component> { if (discriminatorProp !== undefined) { if (!oneOf || !oneOf.length) { - throw new Error( - `Looks like you are using discriminator wrong: you don't have any definition inherited from the ${schema.title}`, - ); + return ; } + return ( - - + + + + >>", - "pattern": undefined, - "pointer": "#/components/schemas/Dog/properties/packSize", - "rawSchema": Object { + "expanded": undefined, + "explode": false, + "in": undefined, + "kind": "field", + "name": "packSize", + "required": false, + "schema": SchemaModel { + "activeOneOf": 0, + "constraints": Array [], "default": undefined, + "deprecated": false, + "description": "", + "displayFormat": undefined, + "displayType": "number", + "enum": Array [], + "example": undefined, + "externalDocs": undefined, + "format": undefined, + "isCircular": undefined, + "isPrimitive": true, + "nullable": false, + "options": "<<>>", + "pattern": undefined, + "pointer": "#/components/schemas/Dog/properties/packSize", + "rawSchema": Object { + "default": undefined, + "type": "number", + }, + "readOnly": false, + "schema": Object { + "default": undefined, + "type": "number", + }, + "title": "", "type": "number", + "typePrefix": "", + "writeOnly": false, }, - "readOnly": false, - "schema": Object { - "default": undefined, - "type": "number", - }, - "title": "", - "type": "number", - "typePrefix": "", - "writeOnly": false, - }, + } } - } - isLast={false} - key="packSize" - showExamples={false} - /> - + >>", - "pattern": undefined, - "pointer": "#/components/schemas/Dog/properties/type", - "rawSchema": Object { + "expanded": undefined, + "explode": false, + "in": undefined, + "kind": "field", + "name": "type", + "required": true, + "schema": SchemaModel { + "activeOneOf": 0, + "constraints": Array [], "default": undefined, + "deprecated": false, + "description": "", + "displayFormat": undefined, + "displayType": "string", + "enum": Array [], + "example": undefined, + "externalDocs": undefined, + "format": undefined, + "isCircular": undefined, + "isPrimitive": true, + "nullable": false, + "options": "<<>>", + "pattern": undefined, + "pointer": "#/components/schemas/Dog/properties/type", + "rawSchema": Object { + "default": undefined, + "type": "string", + }, + "readOnly": false, + "schema": Object { + "default": undefined, + "type": "string", + }, + "title": "", "type": "string", + "typePrefix": "", + "writeOnly": false, }, - "readOnly": false, - "schema": Object { - "default": undefined, - "type": "string", - }, - "title": "", - "type": "string", - "typePrefix": "", - "writeOnly": false, - }, + } } - } - isLast={true} - key="type" - renderDiscriminatorSwitch={[Function]} - showExamples={false} - /> - - + isLast={true} + key="type" + renderDiscriminatorSwitch={[Function]} + showExamples={false} + /> + + + `; diff --git a/src/services/__tests__/models/Schema.test.ts b/src/services/__tests__/models/Schema.test.ts index 460ca7c1..819ecabb 100644 --- a/src/services/__tests__/models/Schema.test.ts +++ b/src/services/__tests__/models/Schema.test.ts @@ -13,7 +13,7 @@ describe('Models', () => { const spec = require('../fixtures/discriminator.json'); parser = new OpenAPIParser(spec, undefined, opts); const schema = new SchemaModel(parser, spec.components.schemas.Foo, '', opts); - expect(schema.oneOf).toHaveLength(1); + expect(schema.oneOf).toHaveLength(0); expect(schema.discriminatorProp).toEqual('type'); }); diff --git a/src/services/models/MediaType.ts b/src/services/models/MediaType.ts index eda76e32..82326429 100644 --- a/src/services/models/MediaType.ts +++ b/src/services/models/MediaType.ts @@ -1,12 +1,10 @@ import * as Sampler from 'openapi-sampler'; - import { OpenAPIMediaType } from '../../types'; -import { RedocNormalizedOptions } from '../RedocNormalizedOptions'; -import { SchemaModel } from './Schema'; - import { isJsonLike, mapValues } from '../../utils'; import { OpenAPIParser } from '../OpenAPIParser'; +import { RedocNormalizedOptions } from '../RedocNormalizedOptions'; import { ExampleModel } from './Example'; +import { SchemaModel } from './Schema'; export class MediaTypeModel { examples?: { [name: string]: ExampleModel }; @@ -54,7 +52,7 @@ export class MediaTypeModel { skipNonRequired: this.isRequestType && this.onlyRequiredInSamples, skipWriteOnly: !this.isRequestType, }; - if (this.schema && this.schema.oneOf) { + if (this.schema && this.schema.oneOf && this.schema.oneOf.length > 0) { this.examples = {}; for (const subSchema of this.schema.oneOf) { const sample = Sampler.sample(subSchema.rawSchema, samplerOptions, parser.spec); diff --git a/src/services/models/Schema.ts b/src/services/models/Schema.ts index 49e89f12..78120cfc 100644 --- a/src/services/models/Schema.ts +++ b/src/services/models/Schema.ts @@ -1,12 +1,6 @@ import { action, observable } from 'mobx'; - -import { OpenAPIExternalDocumentation, OpenAPISchema, Referenced } from '../../types'; - -import { OpenAPIParser } from '../OpenAPIParser'; -import { RedocNormalizedOptions } from '../RedocNormalizedOptions'; -import { FieldModel } from './Field'; - import { MergedOpenAPISchema } from '../'; +import { OpenAPIExternalDocumentation, OpenAPISchema, Referenced } from '../../types'; import { detectType, extractExtensions, @@ -18,8 +12,10 @@ import { sortByField, sortByRequired, } from '../../utils/'; - import { l } from '../Labels'; +import { OpenAPIParser } from '../OpenAPIParser'; +import { RedocNormalizedOptions } from '../RedocNormalizedOptions'; +import { FieldModel } from './Field'; // TODO: refactor this model, maybe use getters instead of copying all the values export class SchemaModel { @@ -225,7 +221,7 @@ export class SchemaModel { const discriminator = getDiscriminator(schema)!; this.discriminatorProp = discriminator.propertyName; const implicitInversedMapping = parser.findDerived([ - ...(schema.parentRefs || []), + ...[], //...(schema.parentRefs || []), // including parentRefs in all cases is wrong (it is correct for a class with subclasses, but not for one, which only has a superclass) this.pointer, ]); @@ -312,6 +308,8 @@ export class SchemaModel { innerSchema.title = name; return innerSchema; }); + + this.fields = buildFields(parser, schema, this.pointer, this.options); } }