Skip to content

[@loopback/repository-json-schema] Using "includeRelations" can lead to circular $ref chains that break OAS renderers like Redocly #11502

@KalleV

Description

@KalleV

Describe the bug

The JSON schema generator correctly blocks infinite recursion, but it can still generate circular "$ref" tags, which break evaluation for OAS documentation generators like: https://redocly.com/. This causes the browser page to freeze and crash.

Logs

N/A

Additional information

To get around this issue, I have maintained a local patch like this, but I wanted to help contribute a fix upstream instead (pr link):

diff --git a/dist/build-schema.js b/dist/build-schema.js
index 898f9fb7d70d40ea660648dbe37f6b494dcfd9d3..96e490e2647ee4091337139d7fba0d40fdf3b093 100644
--- a/dist/build-schema.js
+++ b/dist/build-schema.js
@@ -462,6 +462,10 @@ function modelToJsonSchema(ctor, jsonSchemaOptions = {}) {
             // when generating the relation or property schemas
             const targetOptions = { ...options };
             delete targetOptions.title;
+
+            // Do not cascade `includeRelations` to nested entities.
+            delete targetOptions.includeRelations;
+
             const targetSchema = getJsonSchema(targetType, targetOptions);
             const targetRef = { $ref: `#/definitions/${targetSchema.title}` };
             const propDef = getNavigationalPropertyForRelation(relMeta, targetRef);

Reproduction

#11501

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions