fix(ui): handle missing schema reference case for discriminant definitions (#8257)
Co-authored-by: Tim Lai <timothy.lai@gmail.com>
This commit is contained in:
committed by
GitHub
parent
0e8a0407fa
commit
b010b558f1
@@ -350,6 +350,8 @@ export const sampleFromSchemaGeneric = (schema, config={}, exampleOverride = und
|
|||||||
schema.discriminator &&
|
schema.discriminator &&
|
||||||
Object.prototype.hasOwnProperty.call(schema.discriminator, "mapping") &&
|
Object.prototype.hasOwnProperty.call(schema.discriminator, "mapping") &&
|
||||||
schema.discriminator.mapping &&
|
schema.discriminator.mapping &&
|
||||||
|
Object.prototype.hasOwnProperty.call(schema, "$$ref") &&
|
||||||
|
schema.$$ref &&
|
||||||
schema.discriminator.propertyName === propName) {
|
schema.discriminator.propertyName === propName) {
|
||||||
for (let pair in schema.discriminator.mapping){
|
for (let pair in schema.discriminator.mapping){
|
||||||
if (schema.$$ref.search(schema.discriminator.mapping[pair]) !== -1) {
|
if (schema.$$ref.search(schema.discriminator.mapping[pair]) !== -1) {
|
||||||
|
|||||||
@@ -671,6 +671,65 @@ describe("sampleFromSchema", () => {
|
|||||||
|
|
||||||
expect(sampleFromSchema(definition)).toEqual(expected)
|
expect(sampleFromSchema(definition)).toEqual(expected)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should not throw if expected $$ref is missing, and should fallback to default behavior", () => {
|
||||||
|
let definition = {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"oneOf": [
|
||||||
|
{
|
||||||
|
"required": [
|
||||||
|
"type"
|
||||||
|
],
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"type": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": [
|
||||||
|
"TYPE1",
|
||||||
|
"TYPE2"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"discriminator": {
|
||||||
|
"propertyName": "type",
|
||||||
|
"mapping": {
|
||||||
|
"TYPE1": "#/components/schemas/FirstDto",
|
||||||
|
"TYPE2": "#/components/schemas/SecondDto"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"required": [
|
||||||
|
"type"
|
||||||
|
],
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"type": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": [
|
||||||
|
"TYPE1",
|
||||||
|
"TYPE2"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"discriminator": {
|
||||||
|
"propertyName": "type",
|
||||||
|
"mapping": {
|
||||||
|
"TYPE1": "#/components/schemas/FirstDto",
|
||||||
|
"TYPE2": "#/components/schemas/SecondDto"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(() => {
|
||||||
|
sampleFromSchema(definition)
|
||||||
|
}).not.toThrow()
|
||||||
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should use overrideExample when defined", () => {
|
it("should use overrideExample when defined", () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user