forked from elastic/elasticsearch-specification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-model.ts
581 lines (533 loc) · 23.3 KB
/
build-model.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { mkdirSync, writeFileSync } from 'fs'
import { join } from 'path'
import { STATUS_CODES } from 'http'
import {
ClassDeclaration,
EnumDeclaration,
InterfaceDeclaration,
Node,
Project,
PropertyDeclaration,
PropertySignature,
ts,
TypeAliasDeclaration
} from 'ts-morph'
import * as model from './metamodel'
import buildJsonSpec from './json-spec'
import {
assert,
customTypes,
getAllBehaviors,
getNameSpace,
hoistRequestAnnotations,
hoistTypeAnnotations,
isKnownBehavior,
modelBehaviors,
modelEnumDeclaration,
modelGenerics,
modelImplements,
modelInherits,
modelProperty,
modelType,
modelTypeAlias,
parseVariantNameTag,
parseVariantsTag,
verifyUniqueness,
parseJsDocTags,
deepEqual,
sourceLocation, sortTypeDefinitions
} from './utils'
const jsonSpec = buildJsonSpec()
export function compileEndpoints (): Record<string, model.Endpoint> {
// Create endpoints and merge them with
// the recorded mappings if present.
const map = {}
for (const [api, spec] of jsonSpec.entries()) {
map[api] = {
name: api,
description: spec.documentation.description,
docUrl: spec.documentation.url,
// Setting these values by default should be removed
// when we no longer use rest-api-spec stubs as the
// source of truth for stability/visibility.
availability: {
stack: {
stability: spec.stability,
visibility: spec.visibility
}
},
stability: spec.stability,
visibility: spec.visibility,
request: null,
requestBodyRequired: Boolean(spec.body?.required),
response: null,
urls: spec.url.paths.map(path => {
return {
path: path.path,
methods: path.methods,
...(path.deprecated != null && { deprecation: path.deprecated })
}
})
}
if (typeof spec.feature_flag === 'string') {
map[api].featureFlag = spec.feature_flag
}
}
return map
}
export function compileSpecification (endpointMappings: Record<string, model.Endpoint>, specsFolder: string, outputFolder: string): model.Model {
const tsConfigFilePath = join(specsFolder, 'tsconfig.json')
const project = new Project({ tsConfigFilePath })
verifyUniqueness(project)
const model: model.Model = {
types: new Array<model.TypeDefinition>(),
endpoints: new Array<model.Endpoint>()
}
// Read and compile source files
const declarations = {
classes: new Array<ClassDeclaration>(),
interfaces: new Array<InterfaceDeclaration>(),
enums: new Array<EnumDeclaration>(),
typeAliases: new Array<TypeAliasDeclaration>()
}
const definedButNeverUsed: string[] = []
for (const sourceFile of project.getSourceFiles()) {
for (const declaration of sourceFile.getClasses()) {
if (customTypes.includes(declaration.getName() ?? '')) continue
declarations.classes.push(declaration)
}
for (const declaration of sourceFile.getInterfaces()) {
declarations.interfaces.push(declaration)
}
for (const declaration of sourceFile.getEnums()) {
declarations.enums.push(declaration)
}
for (const declaration of sourceFile.getTypeAliases()) {
declarations.typeAliases.push(declaration)
}
}
mkdirSync(join(outputFolder, 'dangling-types'), { recursive: true })
writeFileSync(
join(outputFolder, 'dangling-types', 'dangling.csv'),
definedButNeverUsed.join('\n'),
{ encoding: 'utf8', flag: 'w' }
)
for (const api of jsonSpec.keys()) {
model.endpoints.push(endpointMappings[api])
}
// Visit all class, interface, enum and type alias definitions
for (const declaration of declarations.classes) {
model.types.push(compileClassOrInterfaceDeclaration(declaration, endpointMappings, declarations.classes))
}
for (const declaration of declarations.interfaces) {
model.types.push(compileClassOrInterfaceDeclaration(declaration, endpointMappings, declarations.classes))
}
for (const declaration of declarations.enums) {
model.types.push(modelEnumDeclaration(declaration))
}
for (const declaration of declarations.typeAliases) {
model.types.push(modelTypeAlias(declaration))
}
// Sort the types in alphabetical order
sortTypeDefinitions(model.types)
return model
}
function compileClassOrInterfaceDeclaration (declaration: ClassDeclaration | InterfaceDeclaration, mappings: Record<string, model.Endpoint>, allClasses: ClassDeclaration[]): model.Request | model.Response | model.Interface {
const name = declaration.getName()
assert(declaration, name != null, 'Anonymous definitions should not exists')
if (name === 'Request') {
assert(
declaration,
Node.isInterfaceDeclaration(declaration),
`Request definitions must be declared as interfaces: ${name}`
)
}
if (name === 'Response') {
assert(
declaration,
Node.isClassDeclaration(declaration),
`Response definitions must be declared as classes: ${name}`
)
}
// Request and Response definitions needs to be handled
// differently from normal classes
if (name === 'Request' || name === 'Response') {
let type: model.Request | model.Response
const namespace = getNameSpace(declaration)
if (name === 'Request') {
type = {
specLocation: sourceLocation(declaration),
kind: 'request',
name: { name, namespace },
path: new Array<model.Property>(),
query: new Array<model.Property>(),
body: { kind: 'no_body' }
}
const response: model.TypeName = {
name: 'Response',
namespace: getNameSpace(declaration)
}
hoistRequestAnnotations(type, declaration.getJsDocs(), mappings, response)
const mapping = mappings[namespace.includes('_global') ? namespace.slice(8) : namespace]
if (mapping == null) {
throw new Error(`Cannot find url template for ${namespace}, very likely the specification folder does not follow the rest-api-spec`)
}
// list of unique dynamic parameters
const urlTemplateParams = [...new Set(
mapping.urls.flatMap(url => url.path.split('/')
.filter(part => part.includes('{'))
.map(part => part.slice(1, -1))
)
)]
const methods = [...new Set(mapping.urls.flatMap(url => url.methods))]
let pathMember: Node | null = null
let bodyProperties: model.Property[] = []
let bodyValue: model.ValueOf | null = null
let bodyMember: Node | null = null
// collect path/query/body properties
for (const member of declaration.getMembers()) {
// we are visiting `path_parts, `query_parameters` or `body`
assert(
member,
Node.isPropertyDeclaration(member) || Node.isPropertySignature(member),
'Class and interfaces can only have property declarations or signatures'
)
const property = visitRequestOrResponseProperty(member)
if (property.name === 'path_parts') {
assert(member, property.properties.length > 0, 'There is no need to declare an empty object path_parts, just remove the path_parts declaration.')
pathMember = member
type.path = property.properties
} else if (property.name === 'query_parameters') {
assert(member, property.properties.length > 0, 'There is no need to declare an empty object query_parameters, just remove the query_parameters declaration.')
type.query = property.properties
} else if (property.name === 'body') {
bodyMember = member
assert(
member,
methods.some(method => ['POST', 'PUT', 'DELETE'].includes(method)),
`${namespace}.${name} can't have a body, allowed methods: ${methods.join(', ')}`
)
if (property.valueOf != null) {
bodyValue = property.valueOf
} else {
assert(member, property.properties.length > 0, 'There is no need to declare an empty object body, just remove the body declaration.')
bodyProperties = property.properties
}
} else {
assert(member, false, `Unknown request property: ${property.name}`)
}
}
// validate path properties
for (const part of type.path) {
assert(
pathMember as Node,
urlTemplateParams.includes(part.name),
`The property '${part.name}' does not exist in the rest-api-spec ${namespace} url template`
)
if (type.query.map(p => p.name).includes(part.name)) {
const queryType = type.query.find(property => property != null && property.name === part.name) as model.Property
if (!deepEqual(queryType.type, part.type)) {
assert(pathMember as Node, part.codegenName != null, `'${part.name}' already exist in the query_parameters with a different type, you should define an @codegen_name.`)
assert(pathMember as Node, !type.query.map(p => p.codegenName ?? p.name).includes(part.codegenName), `The codegen_name '${part.codegenName}' already exists as parameter in query_parameters.`)
}
}
if (bodyProperties.map(p => p.name).includes(part.name)) {
const bodyType = bodyProperties.find(property => property != null && property.name === part.name) as model.Property
if (!deepEqual(bodyType.type, part.type)) {
assert(pathMember as Node, part.codegenName != null, `'${part.name}' already exist in the body with a different type, you should define an @codegen_name.`)
assert(pathMember as Node, !bodyProperties.map(p => p.codegenName ?? p.name).includes(part.codegenName), `The codegen_name '${part.codegenName}' already exists as parameter in body.`)
}
}
}
// validate body
// the body can either be a value (eg Array<string> or an object with properties)
if (bodyValue != null) {
// Propagate required body value nature based on TS question token being present.
// Overrides the value set by spec files.
mapping.requestBodyRequired = !(bodyMember as PropertySignature).hasQuestionToken()
if (bodyValue.kind === 'instance_of' && bodyValue.type.name === 'Void') {
assert(bodyMember as Node, false, 'There is no need to use Void in requets definitions, just remove the body declaration.')
} else {
const tags = parseJsDocTags((bodyMember as PropertySignature).getJsDocs())
assert(
bodyMember as Node,
tags.codegen_name != null,
'You should configure a body @codegen_name'
)
assert(
(bodyMember as PropertySignature).getJsDocs(),
!type.path.map(p => p.codegenName ?? p.name).concat(type.query.map(p => p.codegenName ?? p.name)).includes(tags.codegen_name),
`The codegen_name '${tags.codegen_name}' already exists as a property in the path or query.`
)
type.body = {
kind: 'value',
value: bodyValue,
codegenName: tags.codegen_name
}
}
} else if (bodyProperties.length > 0) {
type.body = { kind: 'properties', properties: bodyProperties }
}
// The interface is extended, an extended interface could accept generics as well,
// Implements will be caught here as well, they can be differentiated by looking as `node.token`
// which can either be `ts.SyntaxKind.ExtendsKeyword` or `ts.SyntaxKind.ImplementsKeyword`
// In case of `ts.SyntaxKind.ImplementsKeyword`, we need to check
// if it's a normal implements or a behavior, in such case, the behaviors
// need to be collected and added to the type.
if (declaration.getHeritageClauses().length > 0) {
// check if the current node or one of the ancestor
// has one or more behaviors attached
const attachedBehaviors = getAllBehaviors(declaration)
if (attachedBehaviors.length > 0) {
type.attachedBehaviors = Array.from(
new Set((type.attachedBehaviors ?? []).concat(attachedBehaviors))
)
}
}
for (const inherit of declaration.getHeritageClauses()) {
const extended = inherit.getTypeNodes()
.map(t => t.getExpression())
.map(t => t.getType().getSymbol()?.getDeclarations()[0])[0]
assert(inherit, Node.isClassDeclaration(extended) || Node.isInterfaceDeclaration(extended), 'Should extend from a class or interface')
type.inherits = modelInherits(extended, inherit)
}
// If the body wasn't set and we have a parent class, then it's a property body with no additional properties
if (type.body.kind === 'no_body' && type.inherits != null) {
const parent = type.inherits.type
// RequestBase is special as it's a "marker" base class that doesn't imply a property body type. We should get rid of it.
if (parent.name === 'RequestBase' && parent.namespace === '_types') {
// nothing to do
// CatRequestBase is special as it's a "marker" base class that doesn't imply a property body type. We should get rid of it.
} else if (parent.name === 'CatRequestBase' && parent.namespace === 'cat._types') {
// nothing to do
} else {
type.body = { kind: 'properties', properties: new Array<model.Property>() }
}
}
} else {
type = {
specLocation: sourceLocation(declaration),
kind: 'response',
name: { name, namespace: getNameSpace(declaration) },
body: { kind: 'no_body' }
}
for (const member of declaration.getMembers()) {
// we are visiting `path_parts, `query_parameters` or `body`
assert(
member,
Node.isPropertyDeclaration(member) || Node.isPropertySignature(member),
'Class and interfaces can only have property declarations or signatures'
)
if (member.getName() === 'body') {
const property = visitRequestOrResponseProperty(member)
// the body can either by a value (eg Array<string> or an object with properties)
if (property.valueOf != null) {
if (property.valueOf.kind === 'instance_of' && property.valueOf.type.name === 'Void') {
type.body = { kind: 'no_body' }
} else {
type.body = { kind: 'value', value: property.valueOf }
}
} else {
type.body = { kind: 'properties', properties: property.properties }
}
} else if (member.getName() === 'exceptions') {
const exceptions: model.ResponseException[] = []
const property = member.getTypeNode()
assert(
property,
Node.isTupleTypeNode(property),
'Exceptionlures should be an array.'
)
for (const element of property.getElements()) {
const exception: model.ResponseException = {
statusCodes: [],
body: { kind: 'no_body' }
}
element.forEachChild(child => {
assert(
child,
Node.isPropertySignature(child) || Node.isPropertyDeclaration(child),
`Children should be ${ts.SyntaxKind[ts.SyntaxKind.PropertySignature]} or ${ts.SyntaxKind[ts.SyntaxKind.PropertyDeclaration]} but is ${ts.SyntaxKind[child.getKind()]} instead`
)
const jsDocs = child.getJsDocs()
if (jsDocs.length > 0) {
exception.description = jsDocs[0].getDescription().replace(/\r/g, '')
}
if (child.getName() === 'statusCodes') {
const value = child.getTypeNode()
assert(value, Node.isTupleTypeNode(value), 'statusCodes should be an array.')
for (const code of value.getElements()) {
assert(code, Node.isLiteralTypeNode(code) && Number.isInteger(Number(code.getText())), 'Status code values should a valid integer')
assert(code, STATUS_CODES[code.getText()] != null, `${code.getText()} is not a valid status code`)
exception.statusCodes.push(Number(code.getText()))
}
} else if (child.getName() === 'body') {
const property = visitRequestOrResponseProperty(child)
// the body can either by a value (eg Array<string> or an object with properties)
if (property.valueOf != null) {
if (property.valueOf.kind === 'instance_of' && property.valueOf.type.name === 'Void') {
exception.body = { kind: 'no_body' }
} else {
exception.body = { kind: 'value', value: property.valueOf }
}
} else {
exception.body = { kind: 'properties', properties: property.properties }
}
} else {
assert(child, false, 'Exception.body and Exception.statusCode are the only Exception properties supported')
}
})
exceptions.push(exception)
}
type.exceptions = exceptions
} else {
assert(member, false, 'Response.body and Response.exceptions are the only Response properties supported')
}
}
assert(
declaration,
declaration.getHeritageClauses().length === 0,
'Responses cannot be extended'
)
}
for (const typeParameter of declaration.getTypeParameters()) {
type.generics = (type.generics ?? []).concat({
name: modelGenerics(typeParameter),
namespace: type.name.namespace
})
}
return type
// Every other class or interface will be handled here
} else {
const type: model.Interface = {
specLocation: sourceLocation(declaration),
kind: 'interface',
name: { name, namespace: getNameSpace(declaration) },
properties: new Array<model.Property>()
}
hoistTypeAnnotations(type, declaration.getJsDocs())
const variant = parseVariantNameTag(declaration.getJsDocs())
if (typeof variant === 'string') {
type.variantName = variant
}
const variants = parseVariantsTag(declaration.getJsDocs())
if (variants != null) {
assert(declaration.getJsDocs(), variants.kind === 'container', 'Interfaces can only use `container` variant kind')
type.variants = variants
}
for (const member of declaration.getMembers()) {
// Any property definition
assert(
member,
Node.isPropertyDeclaration(member) || Node.isPropertySignature(member),
'Class and interfaces can only have property declarations or signatures'
)
const property = modelProperty(member)
if (type.variants?.kind === 'container' && property.containerProperty == null) {
assert(
member,
!property.required,
'All @variants container properties must be optional'
)
}
type.properties.push(property)
}
// The class or interface is extended, an extended class or interface could
// accept generics as well, Implements will be caught here as well,
// they can be differentiated by looking as `node.token`, which can either be
// `ts.SyntaxKind.ExtendsKeyword` or `ts.SyntaxKind.ImplementsKeyword`
// In case of `ts.SyntaxKind.ImplementsKeyword`, we need to check
// if it's a normal implements or a behavior, in such case, the behaviors
// need to be collected and added to the type.
if (declaration.getHeritageClauses().length > 0) {
// check if the current node or one of the ancestor
// has one or more behaviors attached
const attachedBehaviors = getAllBehaviors(declaration)
if (attachedBehaviors.length > 0) {
type.attachedBehaviors = Array.from(
new Set((type.attachedBehaviors ?? []).concat(attachedBehaviors))
)
}
}
for (const inherit of declaration.getHeritageClauses()) {
if (inherit.getToken() === ts.SyntaxKind.ExtendsKeyword) {
const extended = inherit.getTypeNodes()
.map(t => t.getExpression())
.map(t => t.getType().getSymbol()?.getDeclarations()[0])[0]
assert(inherit, Node.isClassDeclaration(extended) || Node.isInterfaceDeclaration(extended), 'Should extend from a class or interface')
type.inherits = modelInherits(extended, inherit)
}
}
// Only classes can implement interfaces
if (Node.isClassDeclaration(declaration)) {
for (const implement of declaration.getImplements()) {
if (isKnownBehavior(implement)) {
type.behaviors = (type.behaviors ?? []).concat(modelBehaviors(implement))
} else {
type.implements = (type.implements ?? []).concat(modelImplements(implement))
}
}
}
for (const typeParameter of declaration.getTypeParameters()) {
type.generics = (type.generics ?? []).concat({
name: modelGenerics(typeParameter),
namespace: type.name.namespace
})
}
return type
}
}
/**
* Utility wrapper around `modelProperty`, as Request classes needs to be handled
* differently as are described as nested objects, and the body could have two
* different types, `model.Property[]` (a normal object) or `model.ValueOf` (eg: an array or generic)
*/
function visitRequestOrResponseProperty (member: PropertyDeclaration | PropertySignature): { name: string, properties: model.Property[], valueOf: model.ValueOf | null } {
const properties: model.Property[] = []
let valueOf: model.ValueOf | null = null
const name = member.getName()
const value = member.getTypeNode()
assert(member, value != null, `The property ${name} is not defined`)
// Request classes have three top level properties:
// - path_parts
// - query_parameters
// - body
//
// In most of the cases all of those are PropertyDeclarations, eg (path_parts: { foo: string }),
// but in some cases they can be a TypeReference eg (body: Array<string>)
// PropertySignatures and PropertyDeclarations must be iterated via `ts.forEachChild`, as every
// declaration is a child of the top level properties, while TypeReference should
// directly by "unwrapped" with `modelType` because if you navigate the children of a TypeReference
// you will lose the context and crafting the types becomes really hard.
if (Node.isTypeReference(value) || Node.isUnionTypeNode(value)) {
valueOf = modelType(value)
} else {
value.forEachChild(child => {
assert(
child,
Node.isPropertySignature(child) || Node.isPropertyDeclaration(child),
`Children should be ${ts.SyntaxKind[ts.SyntaxKind.PropertySignature]} or ${ts.SyntaxKind[ts.SyntaxKind.PropertyDeclaration]} but is ${ts.SyntaxKind[child.getKind()]} instead`
)
properties.push(modelProperty(child))
})
}
return { name, properties, valueOf }
}