UNPKG

2.22 kBTypeScriptView Raw
1import { ApplicationConfig } from '@loopback/core';
2import { OASEnhancer, OpenApiSpec } from '@loopback/openapi-v3';
3/**
4 * This enhancer consolidates schemas into `/components/schemas` and replaces
5 * instances of said schema with a $ref pointer.
6 *
7 * Please note that the title property must be set on a schema in order to be
8 * considered for consolidation.
9 *
10 * For example, with the following schema instance:
11 *
12 * ```json
13 * schema: {
14 * title: 'loopback.example',
15 * properties: {
16 * test: {
17 * type: 'string',
18 * },
19 * },
20 * }
21 * ```
22 *
23 * The consolidator will copy the schema body to
24 * `/components/schemas/loopback.example` and replace any instance of the schema
25 * with a reference to the component schema as follows:
26 *
27 * ```json
28 * schema: {
29 * $ref: '#/components/schemas/loopback.example',
30 * }
31 * ```
32 *
33 * When comparing schemas to avoid naming collisions, the description field
34 * is ignored.
35 */
36export declare class ConsolidationEnhancer implements OASEnhancer {
37 readonly config?: ApplicationConfig | undefined;
38 name: string;
39 disabled: boolean;
40 constructor(config?: ApplicationConfig | undefined);
41 modifySpec(spec: OpenApiSpec): OpenApiSpec;
42 /**
43 * Recursively search OpenApiSpec PathsObject for SchemaObjects with title
44 * property. Moves reusable schema bodies to #/components/schemas and replace
45 * with json pointer. It handles title collisions with schema body comparision.
46 */
47 private consolidateSchemaObjects;
48 private recursiveWalk;
49 /**
50 * Carry out schema consolidation after tree traversal. If 'title' property
51 * set then we consider current schema for consolidation. SchemaObjects with
52 * properties (and title set) are moved to #/components/schemas/<title> and
53 * replaced with ReferenceObject.
54 *
55 * Features:
56 * - name collision protection
57 *
58 * @param schema - current schema element to process
59 * @param parentPath - path object to parent
60 * @param spec - subject OpenApi specification
61 */
62 private processSchema;
63 private getRefSchema;
64 private patchRef;
65 private patchPath;
66 private ifConsolidationCandidate;
67 private isTraversable;
68}