Skip to content

Commit f5bb456

Browse files
feat(example): set discriminated properties to mapped value (#8213)
1 parent 7936ec9 commit f5bb456

File tree

2 files changed

+82
-1
lines changed
  • src/core/plugins/samples
  • test/unit/core/plugins/samples

2 files changed

+82
-1
lines changed

src/core/plugins/samples/fn.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,20 @@ export const sampleFromSchemaGeneric = (schema, config={}, exampleOverride = und
346346
if(!canAddProperty(propName)) {
347347
return
348348
}
349-
res[propName] = sampleFromSchemaGeneric(props[propName], config, overrideE, respectXML)
349+
if(Object.prototype.hasOwnProperty.call(schema, "discriminator") &&
350+
schema.discriminator &&
351+
Object.prototype.hasOwnProperty.call(schema.discriminator, "mapping") &&
352+
schema.discriminator.mapping &&
353+
schema.discriminator.propertyName === propName) {
354+
for (let pair in schema.discriminator.mapping){
355+
if (schema.$$ref.search(schema.discriminator.mapping[pair]) !== -1) {
356+
res[propName] = pair
357+
break
358+
}
359+
}
360+
} else {
361+
res[propName] = sampleFromSchemaGeneric(props[propName], config, overrideE, respectXML)
362+
}
350363
propertyAddedCounter++
351364
}
352365
}

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,74 @@ describe("sampleFromSchema", () => {
605605
})
606606
})
607607

608+
describe("discriminator mapping example", () => {
609+
it("returns an example where discriminated field is equal to mapping value", () => {
610+
let definition = {
611+
"type": "array",
612+
"items": {
613+
"oneOf": [
614+
{
615+
"required": [
616+
"type"
617+
],
618+
"type": "object",
619+
"properties": {
620+
"type": {
621+
"type": "string",
622+
"enum": [
623+
"TYPE1",
624+
"TYPE2"
625+
]
626+
}
627+
},
628+
"discriminator": {
629+
"propertyName": "type",
630+
"mapping": {
631+
"TYPE1": "#/components/schemas/FirstDto",
632+
"TYPE2": "#/components/schemas/SecondDto"
633+
}
634+
},
635+
"$$ref": "examples/swagger-config.yaml#/components/schemas/FirstDto"
636+
},
637+
{
638+
"required": [
639+
"type"
640+
],
641+
"type": "object",
642+
"properties": {
643+
"type": {
644+
"type": "string",
645+
"enum": [
646+
"TYPE1",
647+
"TYPE2"
648+
]
649+
}
650+
},
651+
"discriminator": {
652+
"propertyName": "type",
653+
"mapping": {
654+
"TYPE1": "#/components/schemas/FirstDto",
655+
"TYPE2": "#/components/schemas/SecondDto"
656+
}
657+
},
658+
"$$ref": "examples/swagger-config.yaml#/components/schemas/SecondDto"
659+
}
660+
]
661+
}
662+
}
663+
664+
let expected = [
665+
{
666+
"type": "TYPE1"
667+
}, {
668+
"type": "TYPE2"
669+
}
670+
]
671+
672+
expect(sampleFromSchema(definition)).toEqual(expected)
673+
})
674+
})
675+
608676
it("should use overrideExample when defined", () => {
609677
const definition = {
610678
type: "object",

0 commit comments

Comments
 (0)