-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathSchema.ts
268 lines (232 loc) · 7.24 KB
/
Schema.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import { action, observable } from 'mobx';
import { OpenAPISchema, Referenced } from '../../types';
import { OpenAPIParser } from '../OpenAPIParser';
import { RedocNormalizedOptions } from '../RedocNormalizedOptions';
import { FieldModel } from './Field';
import { MergedOpenAPISchema } from '../';
import {
detectType,
humanizeConstraints,
isNamedDefinition,
isPrimitiveType,
JsonPointer,
sortByRequired,
} from '../../utils/';
// TODO: refactor this model, maybe use getters instead of copying all the values
export class SchemaModel {
pointer: string;
type: string;
displayType: string;
typePrefix: string = '';
title: string;
description: string;
isPrimitive: boolean;
isCircular: boolean = false;
format?: string;
displayFormat?: string;
nullable: boolean;
deprecated: boolean;
pattern?: string;
example?: any;
enum: any[];
default?: any;
readOnly: boolean;
writeOnly: boolean;
constraints: string[];
fields?: FieldModel[];
items?: SchemaModel;
oneOf?: SchemaModel[];
oneOfType: string;
discriminatorProp: string;
@observable activeOneOf: number = 0;
rawSchema: OpenAPISchema;
schema: MergedOpenAPISchema;
/**
* @param isChild if schema discriminator Child
* When true forces dereferencing in allOfs even if circular
*/
constructor(
parser: OpenAPIParser,
schemaOrRef: Referenced<OpenAPISchema>,
pointer: string,
private options: RedocNormalizedOptions,
isChild: boolean = false,
) {
this.pointer = schemaOrRef.$ref || pointer || '';
this.rawSchema = parser.deref(schemaOrRef);
this.schema = parser.mergeAllOf(this.rawSchema, this.pointer, isChild);
this.init(parser, isChild);
parser.exitRef(schemaOrRef);
for (const parent$ref of this.schema.parentRefs || []) {
// exit all the refs visited during allOf traverse
parser.exitRef({ $ref: parent$ref });
}
}
/**
* Set specified alternative schema as active
* @param idx oneOf index
*/
@action
activateOneOf(idx: number) {
this.activeOneOf = idx;
}
init(parser: OpenAPIParser, isChild: boolean) {
const schema = this.schema;
this.isCircular = schema['x-circular-ref'];
this.title =
schema.title || (isNamedDefinition(this.pointer) && JsonPointer.baseName(this.pointer)) || '';
this.description = schema.description || '';
this.type = schema.type || detectType(schema);
this.format = schema.format;
this.nullable = !!schema.nullable;
this.enum = schema.enum || [];
this.example = schema.example;
this.deprecated = !!schema.deprecated;
this.pattern = schema.pattern;
this.constraints = humanizeConstraints(schema);
this.displayType = this.type;
this.displayFormat = this.format;
this.isPrimitive = isPrimitiveType(schema);
this.default = schema.default;
this.readOnly = !!schema.readOnly;
this.writeOnly = !!schema.writeOnly;
if (this.isCircular) {
return;
}
if (!isChild && getDiscriminator(schema) !== undefined) {
this.initDiscriminator(schema, parser);
return;
}
if (schema.oneOf !== undefined) {
this.initOneOf(schema.oneOf, parser);
this.oneOfType = 'One of';
if (schema.anyOf !== undefined) {
console.warn(
`oneOf and anyOf are not supported on the same level. Skipping anyOf at ${this.pointer}`,
);
}
return;
}
if (schema.anyOf !== undefined) {
this.initOneOf(schema.anyOf, parser);
this.oneOfType = 'Any of';
return;
}
if (this.type === 'object') {
this.fields = buildFields(parser, schema, this.pointer, this.options);
} else if (this.type === 'array' && schema.items) {
this.items = new SchemaModel(parser, schema.items, this.pointer + '/items', this.options);
this.displayType = this.items.displayType;
this.displayFormat = this.items.format;
this.typePrefix = this.items.typePrefix + 'Array of ';
this.title = this.title || this.items.title;
this.isPrimitive = this.items.isPrimitive;
if (this.example === undefined && this.items.example !== undefined) {
this.example = [this.items.example];
}
if (this.items.isPrimitive) {
this.enum = this.items.enum;
}
}
}
private initOneOf(oneOf: OpenAPISchema[], parser: OpenAPIParser) {
this.oneOf = oneOf!.map(
(variant, idx) =>
new SchemaModel(
parser,
{
// merge base schema into each of oneOf's subschemas
allOf: [variant, { ...this.schema, oneOf: undefined, anyOf: undefined }],
} as OpenAPISchema,
this.pointer + '/oneOf/' + idx,
this.options,
),
);
this.displayType = this.oneOf.map(schema => schema.displayType).join(' or ');
}
private initDiscriminator(
schema: OpenAPISchema & {
parentRefs?: string[];
},
parser: OpenAPIParser,
) {
const discriminator = getDiscriminator(schema)!;
this.discriminatorProp = discriminator.propertyName;
const derived = parser.findDerived([...(schema.parentRefs || []), this.pointer]);
if (schema.oneOf) {
for (const variant of schema.oneOf) {
if (variant.$ref === undefined) {
continue;
}
const name = JsonPointer.dirName(variant.$ref);
derived[variant.$ref] = name;
}
}
const mapping = discriminator.mapping || {};
for (const name in mapping) {
derived[mapping[name]] = name;
}
const refs = Object.keys(derived);
this.oneOf = refs.map(ref => {
const innerSchema = new SchemaModel(parser, parser.byRef(ref)!, ref, this.options, true);
innerSchema.title = derived[ref];
return innerSchema;
});
}
}
function buildFields(
parser: OpenAPIParser,
schema: OpenAPISchema,
$ref: string,
options: RedocNormalizedOptions,
): FieldModel[] {
const props = schema.properties || {};
const additionalProps = schema.additionalProperties;
const defaults = schema.default || {};
const fields = Object.keys(props || []).map(fieldName => {
let field = props[fieldName];
if (!field) {
console.warn(
`Field "${fieldName}" is invalid, skipping.\n Field must be an object but got ${typeof field} at "${$ref}"`,
);
field = {};
}
const required =
schema.required === undefined ? false : schema.required.indexOf(fieldName) > -1;
return new FieldModel(
parser,
{
name: fieldName,
required,
schema: {
...field,
default: field.default === undefined ? defaults[fieldName] : field.default,
},
},
$ref + '/properties/' + fieldName,
options,
);
});
if (options.requiredPropsFirst) {
sortByRequired(fields, schema.required);
}
if (typeof additionalProps === 'object' || additionalProps === true) {
fields.push(
new FieldModel(
parser,
{
name: 'property name *',
required: false,
schema: additionalProps === true ? {} : additionalProps,
kind: 'additionalProperties',
},
$ref + '/additionalProperties',
options,
),
);
}
return fields;
}
function getDiscriminator(schema: OpenAPISchema): OpenAPISchema['discriminator'] {
return schema.discriminator || schema['x-discriminator'];
}