diff --git a/src/components/Schema/DiscriminatorDropdown.tsx b/src/components/Schema/DiscriminatorDropdown.tsx index d3290a2a..04c9524e 100644 --- a/src/components/Schema/DiscriminatorDropdown.tsx +++ b/src/components/Schema/DiscriminatorDropdown.tsx @@ -33,7 +33,7 @@ export class DiscriminatorDropdown extends React.Component<{ const options = parent.oneOf.map((subSchema, idx) => { return { - value: subSchema.title, + value: subSchema.discriminant(parent.discriminatorProp), idx, }; }); diff --git a/src/services/models/MediaType.ts b/src/services/models/MediaType.ts index 25dae807..0a99094e 100644 --- a/src/services/models/MediaType.ts +++ b/src/services/models/MediaType.ts @@ -63,10 +63,11 @@ export class MediaTypeModel { const sample = Sampler.sample(subSchema.rawSchema as any, samplerOptions, parser.spec); if (this.schema.discriminatorProp && typeof sample === 'object' && sample) { - sample[this.schema.discriminatorProp] = subSchema.title; + sample[this.schema.discriminatorProp] = subSchema.discriminant( + this.schema.discriminatorProp, + ); } - - this.examples[subSchema.title] = new ExampleModel( + this.examples[subSchema.discriminant()] = new ExampleModel( parser, { value: sample, diff --git a/src/services/models/Schema.ts b/src/services/models/Schema.ts index b48f232c..bccd7bce 100644 --- a/src/services/models/Schema.ts +++ b/src/services/models/Schema.ts @@ -102,6 +102,26 @@ export class SchemaModel { this.activeOneOf = idx; } + discriminant(propName?): string { + if (!propName) { + propName = this.schema.discriminator?.propertyName; + } + + if (!propName) { + return this.title; + } + + const properties = this.schema.properties; + + if (!properties) { + return this.title; + } + + const prop = properties[propName] || null; + + return (prop && prop.enum && prop.enum.length && prop.enum[0]) || this.title; + } + hasType(type: string) { return this.type === type || (Array.isArray(this.type) && this.type.includes(type)); }