Skip to content

Enhance getJsonSchema to describe navigational properties #2630

@bajtos

Description

@bajtos

Improve the code converting LoopBack Model definitions into JSON Schema documents to optionally describe navigational properties for relations defined by the model. The decision is controlled by a new option passed to modelToJsonSchema and
related methods.

// own properties only
const spec = getJsonSchema(Product);
// include navigational properties too
const spec = getJsonSchema(Product, {includeRelations: true});

Add a new property to all relation definitions. This property will signal whether the relations is targeting a single model instance (e.g. BelongsTo, HasOne) or an array of instances (e.g. HasMany).

 export interface RelationDefinitionBase {
   /**
    * The type of the relation, must be one of RelationType values.
    */
   type: RelationType;

+  /**
+   * True for relations targeting multiple instances (e.g. HasMany),
+   * false for relations with a single target (e.g. BelongsTo, HasOne).
+   * This property is need by OpenAPI/JSON Schema generator.
+   */
+  targetsMany: boolean;

   // ...
}

Add a new property to options object used by getJsonSchema and modelToJsonSchema:

 export interface JsonSchemaOptions {
+  includeRelations?: boolean;
   visited?: {[key: string]: JSONSchema};
 }

Modify the implementation of modelToJsonSchema to include navigational properties when includeRelations is set.

See the spike #2592 and especially the commits 2256435 for more details. Please note the commit shows changes before the review feedback was applied. You need to review the final pull request diff of the PoC before copying any code over.

This story requires us to fix handling of circular references first, see #2628.

Acceptance criteria

  • Implement the changes described above
  • Test coverage
  • Update API documentation

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions