-
Notifications
You must be signed in to change notification settings - Fork 289
Description
Describe the bug
When using a discriminator with a sub-schema that inherits all fields from its parent (using allOf) and does not define any new properties, the Docusaurus OpenAPI plugin throws a runtime error:
Cannot read properties of undefined (reading 'type')
This happens because the code attempts to access a property on an undefined object when merging discriminator properties.
Expected behavior
The documentation should render the schema and endpoint correctly, even if a sub-schema inherits all fields from its parent and does not define any new properties. No runtime errors should occur.
Current behavior
The documentation build or runtime fails with the error:
Cannot read properties of undefined (reading 'type')
This prevents the endpoint and schema from being displayed in the generated docs.
Possible solution
Add a safeguard check in the plugin code (packages/docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx) to ensure subProperties is defined before accessing its properties. Specifically, change:
if (subProperties[discriminator.propertyName]) {
to:
// Add a safeguard check to avoid referencing subProperties if it's undefined
if (subProperties && subProperties[discriminator.propertyName]) {
This prevents the error and allows the docs to render correctly.
Steps to reproduce
-
In
demo/examples/tests/discriminator.yaml, define an endpoint/discriminator-empty-subschemawith a discriminator and a sub-schema that only uses allOf to inherit from the parent, without adding new properties:/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"
And in the same file add the following under
schemas:BaseEmptySubschema: type: object discriminator: propertyName: type mapping: EmptyType: '#/components/schemas/EmptyType' properties: type: type: string oneOf: - $ref: '#/components/schemas/EmptyType' EmptyType: type: object allOf: - $ref: '#/components/schemas/BaseEmptySubschema'
-
Build and start the Docusaurus dev server and navigate to the endpoint in the docs.
-
Observe the runtime error in the browser or terminal.
Screenshots
Context
This issue blocks us from documenting APIs that use inheritance patterns where sub-models do not add new fields beyond what they inherit. It is a valid OpenAPI pattern and should be supported by the plugin.
Your Environment
- Version used: docusaurus-plugin-openapi-docs v4.3.7
- Operating System and version: macOS