Skip to content

Type error when using a discriminator with a sub-schema that inherits all fields from its parent and does not define any new properties #1132

@amalv

Description

@amalv

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

  1. In demo/examples/tests/discriminator.yaml, define an endpoint /discriminator-empty-subschema with 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'
  2. Build and start the Docusaurus dev server and navigate to the endpoint in the docs.

  3. Observe the runtime error in the browser or terminal.

Screenshots

Image

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions