fix: Overrides user-provided discriminator values with object names (#1794)

This commit is contained in:
Robert Mela 2021-12-05 12:50:35 -05:00
parent 8b1eea8c0c
commit 969e4b961e
3 changed files with 25 additions and 4 deletions

View File

@ -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,
};
});

View File

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

View File

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