Skip to content

Commit 18d299e

Browse files
author
Amaro Mariño
committed
fix: add discriminator empty subschema and safeguard check in DiscriminatorNode
1 parent e1aed04 commit 18d299e

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

demo/examples/tests/discriminator.yaml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,35 @@ paths:
321321
application/json:
322322
schema:
323323
$ref: "#/components/schemas/BaseRequiredMapping"
324+
325+
/discriminator-empty-subschema:
326+
get:
327+
tags:
328+
- discriminator
329+
summary: Discriminator with Subschema Inheriting All Fields (No Extra Fields)
330+
description: |
331+
This schema reproduces a sub-schema that inherits all fields from the parent (via allOf) and does not define any new properties.
332+
Schema:
333+
```yaml
334+
type: object
335+
discriminator:
336+
propertyName: type
337+
mapping:
338+
EmptyType: '#/components/schemas/EmptyType'
339+
properties:
340+
type:
341+
type: string
342+
oneOf:
343+
- $ref: '#/components/schemas/EmptyType'
344+
```
345+
responses:
346+
"200":
347+
description: Successful response
348+
content:
349+
application/json:
350+
schema:
351+
$ref: "#/components/schemas/BaseEmptySubschema"
352+
324353
components:
325354
schemas:
326355
BaseBasic:
@@ -470,6 +499,18 @@ components:
470499
- $ref: "#/components/schemas/TypeA"
471500
- $ref: "#/components/schemas/TypeB"
472501

502+
BaseEmptySubschema:
503+
type: object
504+
discriminator:
505+
propertyName: type
506+
mapping:
507+
EmptyType: "#/components/schemas/EmptyType"
508+
properties:
509+
type:
510+
type: string
511+
oneOf:
512+
- $ref: "#/components/schemas/EmptyType"
513+
473514
TypeA:
474515
type: object
475516
properties:
@@ -523,3 +564,8 @@ components:
523564
type: boolean
524565
required:
525566
- type
567+
568+
EmptyType:
569+
type: object
570+
allOf:
571+
- $ref: "#/components/schemas/BaseEmptySubschema"

packages/docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,8 @@ const DiscriminatorNode: React.FC<DiscriminatorNodeProps> = ({
352352
}
353353

354354
const subProperties = subSchema.properties || mergedSubSchema.properties;
355-
if (subProperties[discriminator.propertyName]) {
355+
// Add a safeguard check to avoid referencing subProperties if it's undefined
356+
if (subProperties && subProperties[discriminator.propertyName]) {
356357
if (schema.properties) {
357358
schema.properties![discriminator.propertyName] = {
358359
...schema.properties![discriminator.propertyName],

0 commit comments

Comments
 (0)