Skip to content

Commit b010b55

Browse files
YiuRULEtim-lai
andauthored
fix(ui): handle missing schema reference case for discriminant definitions (#8257)
Co-authored-by: Tim Lai <[email protected]>
1 parent 0e8a040 commit b010b55

File tree

2 files changed

+61
-0
lines changed
  • src/core/plugins/samples
  • test/unit/core/plugins/samples

2 files changed

+61
-0
lines changed

src/core/plugins/samples/fn.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ export const sampleFromSchemaGeneric = (schema, config={}, exampleOverride = und
350350
schema.discriminator &&
351351
Object.prototype.hasOwnProperty.call(schema.discriminator, "mapping") &&
352352
schema.discriminator.mapping &&
353+
Object.prototype.hasOwnProperty.call(schema, "$$ref") &&
354+
schema.$$ref &&
353355
schema.discriminator.propertyName === propName) {
354356
for (let pair in schema.discriminator.mapping){
355357
if (schema.$$ref.search(schema.discriminator.mapping[pair]) !== -1) {

test/unit/core/plugins/samples/fn.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,65 @@ describe("sampleFromSchema", () => {
671671

672672
expect(sampleFromSchema(definition)).toEqual(expected)
673673
})
674+
675+
it("should not throw if expected $$ref is missing, and should fallback to default behavior", () => {
676+
let definition = {
677+
"type": "array",
678+
"items": {
679+
"oneOf": [
680+
{
681+
"required": [
682+
"type"
683+
],
684+
"type": "object",
685+
"properties": {
686+
"type": {
687+
"type": "string",
688+
"enum": [
689+
"TYPE1",
690+
"TYPE2"
691+
]
692+
}
693+
},
694+
"discriminator": {
695+
"propertyName": "type",
696+
"mapping": {
697+
"TYPE1": "#/components/schemas/FirstDto",
698+
"TYPE2": "#/components/schemas/SecondDto"
699+
}
700+
},
701+
},
702+
{
703+
"required": [
704+
"type"
705+
],
706+
"type": "object",
707+
"properties": {
708+
"type": {
709+
"type": "string",
710+
"enum": [
711+
"TYPE1",
712+
"TYPE2"
713+
]
714+
}
715+
},
716+
"discriminator": {
717+
"propertyName": "type",
718+
"mapping": {
719+
"TYPE1": "#/components/schemas/FirstDto",
720+
"TYPE2": "#/components/schemas/SecondDto"
721+
}
722+
},
723+
}
724+
]
725+
}
726+
}
727+
728+
expect(() => {
729+
sampleFromSchema(definition)
730+
}).not.toThrow()
731+
})
732+
674733
})
675734

676735
it("should use overrideExample when defined", () => {

0 commit comments

Comments
 (0)