From 18d299e0f55338ef3aca668c115e9457a13638ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaro=20Mari=C3=B1o?= Date: Fri, 25 Apr 2025 13:58:42 +0200 Subject: [PATCH] fix: add discriminator empty subschema and safeguard check in DiscriminatorNode --- demo/examples/tests/discriminator.yaml | 46 +++++++++++++++++++ .../src/theme/Schema/index.tsx | 3 +- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/demo/examples/tests/discriminator.yaml b/demo/examples/tests/discriminator.yaml index b95e56b30..186db492c 100644 --- a/demo/examples/tests/discriminator.yaml +++ b/demo/examples/tests/discriminator.yaml @@ -321,6 +321,35 @@ paths: application/json: schema: $ref: "#/components/schemas/BaseRequiredMapping" + + /discriminator-empty-subschema: + get: + tags: + - discriminator + summary: Discriminator with Subschema Inheriting All Fields (No Extra Fields) + description: | + This schema reproduces a sub-schema that inherits all fields from the parent (via allOf) and does not define any new properties. + Schema: + ```yaml + type: object + discriminator: + propertyName: type + mapping: + EmptyType: '#/components/schemas/EmptyType' + properties: + type: + type: string + oneOf: + - $ref: '#/components/schemas/EmptyType' + ``` + responses: + "200": + description: Successful response + content: + application/json: + schema: + $ref: "#/components/schemas/BaseEmptySubschema" + components: schemas: BaseBasic: @@ -470,6 +499,18 @@ components: - $ref: "#/components/schemas/TypeA" - $ref: "#/components/schemas/TypeB" + BaseEmptySubschema: + type: object + discriminator: + propertyName: type + mapping: + EmptyType: "#/components/schemas/EmptyType" + properties: + type: + type: string + oneOf: + - $ref: "#/components/schemas/EmptyType" + TypeA: type: object properties: @@ -523,3 +564,8 @@ components: type: boolean required: - type + + EmptyType: + type: object + allOf: + - $ref: "#/components/schemas/BaseEmptySubschema" diff --git a/packages/docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx b/packages/docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx index 0538aecce..79fde278c 100644 --- a/packages/docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx +++ b/packages/docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx @@ -352,7 +352,8 @@ const DiscriminatorNode: React.FC = ({ } const subProperties = subSchema.properties || mergedSubSchema.properties; - if (subProperties[discriminator.propertyName]) { + // Add a safeguard check to avoid referencing subProperties if it's undefined + if (subProperties && subProperties[discriminator.propertyName]) { if (schema.properties) { schema.properties![discriminator.propertyName] = { ...schema.properties![discriminator.propertyName],