-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathgenerator.ts
More file actions
903 lines (849 loc) · 32.8 KB
/
Copy pathgenerator.ts
File metadata and controls
903 lines (849 loc) · 32.8 KB
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
import { omit } from '@sim/utils/object'
import Ajv2020 from 'ajv/dist/2020'
import { z } from 'zod'
import type { AnyApiRouteContract, ApiSchema } from '@/lib/api/contracts/types'
import type {
OpenApiDocumentDefinition,
OpenApiOperationMetadata,
OpenApiRouteDefinition,
OpenApiStatusSuccessMetadata,
} from '@/lib/api/openapi/types'
type JsonObject = Record<string, unknown>
type SchemaIo = 'input' | 'output'
const HTTP_SUCCESS_MIN = 200
const HTTP_SUCCESS_MAX = 299
const HTTP_ERROR_MIN = 400
const HTTP_ERROR_MAX = 599
const SCHEMA_SHAPE_KEYS = new Set([
'$ref',
'type',
'properties',
'items',
'prefixItems',
'oneOf',
'anyOf',
'allOf',
'enum',
'const',
'not',
'additionalProperties',
])
const SCHEMA_DOCUMENTATION_KEYS = new Set([
'title',
'description',
'examples',
'example',
'deprecated',
])
const outputExampleValidator = new Ajv2020({
strict: false,
allErrors: true,
validateFormats: false,
})
const comparableSchemaCache = new WeakMap<ApiSchema, Map<SchemaIo, unknown>>()
const generatedSchemaCache = new WeakMap<ApiSchema, Map<SchemaIo, JsonObject>>()
const outputValidatorCache = new WeakMap<
ApiSchema,
ReturnType<typeof outputExampleValidator.compile>
>()
function invariant(condition: unknown, message: string): asserts condition {
if (!condition) throw new Error(message)
}
function nonEmpty(value: string, label: string): void {
invariant(value.trim().length > 0, `${label} is required`)
}
function schemaMetadata(schema: ApiSchema, label: string): z.core.GlobalMeta {
const metadata = z.globalRegistry.get(schema)
invariant(metadata, `${label} is missing Zod metadata`)
nonEmpty(metadata.title ?? '', `${label} metadata.title`)
nonEmpty(metadata.description ?? '', `${label} metadata.description`)
return metadata
}
function rewriteRefs(value: unknown): unknown {
if (Array.isArray(value)) return value.map(rewriteRefs)
if (!value || typeof value !== 'object') return value
const result: JsonObject = {}
for (const [key, entry] of Object.entries(value)) {
if (key === '$ref' && typeof entry === 'string' && entry.startsWith('#/$defs/')) {
result[key] = entry.replace('#/$defs/', '#/components/schemas/')
continue
}
result[key] = rewriteRefs(entry)
}
return result
}
function sanitizeSchema(value: unknown): JsonObject {
invariant(value && typeof value === 'object' && !Array.isArray(value), 'Expected JSON Schema')
return omit(rewriteRefs(value) as JsonObject, ['$schema', '$id', 'id'])
}
function stripSchemaDocumentation(value: unknown): unknown {
if (Array.isArray(value)) return value.map(stripSchemaDocumentation)
if (!value || typeof value !== 'object') return value
return Object.fromEntries(
Object.entries(value)
.filter(([key]) => !SCHEMA_DOCUMENTATION_KEYS.has(key))
.map(([key, entry]) => [key, stripSchemaDocumentation(entry)])
)
}
function stripLegacySchemaIds(value: unknown): unknown {
if (Array.isArray(value)) return value.map(stripLegacySchemaIds)
if (!value || typeof value !== 'object') return value
return Object.fromEntries(
Object.entries(value)
.filter(([key, entry]) => key !== 'id' || typeof entry !== 'string')
.map(([key, entry]) => [key, stripLegacySchemaIds(entry)])
)
}
function omitEnumValuesFromOpenApi(
metadata: z.core.GlobalMeta | undefined,
schema: JsonObject,
label: string
): void {
const omittedValues = metadata?.omitEnumValuesFromOpenApi
if (omittedValues === undefined) return
invariant(
Array.isArray(omittedValues) && omittedValues.length > 0,
`${label} omitEnumValuesFromOpenApi must be a non-empty array`
)
invariant(
Array.isArray(schema.enum),
`${label} omitEnumValuesFromOpenApi requires an enum schema`
)
for (const value of omittedValues) {
invariant(schema.enum.includes(value), `${label} omits an enum value that does not exist`)
}
schema.enum = schema.enum.filter((value) => !omittedValues.includes(value))
invariant(schema.enum.length > 0, `${label} cannot omit every enum value`)
Reflect.deleteProperty(schema, 'omitEnumValuesFromOpenApi')
}
function comparableSchema(schema: ApiSchema, io: SchemaIo): unknown {
const cached = comparableSchemaCache.get(schema)?.get(io)
if (cached) return cached
const comparable = stripSchemaDocumentation(
sanitizeSchema(
z.toJSONSchema(schema, {
io,
target: 'draft-2020-12',
unrepresentable: 'any',
cycles: 'ref',
reused: 'inline',
})
)
)
const byIo = comparableSchemaCache.get(schema) ?? new Map<SchemaIo, unknown>()
byIo.set(io, comparable)
comparableSchemaCache.set(schema, byIo)
return comparable
}
function addComponent(
components: JsonObject,
name: string,
schema: JsonObject,
label: string
): void {
const existing = components[name]
if (existing !== undefined) {
invariant(
JSON.stringify(existing) === JSON.stringify(schema),
`${label} conflicts with the existing ${name} component`
)
return
}
components[name] = schema
}
function validateNoSilentOpaqueSchemas(schema: JsonObject, label: string, path = '<root>'): void {
const keys = Object.keys(schema)
if (!keys.some((key) => SCHEMA_SHAPE_KEYS.has(key))) {
nonEmpty(
typeof schema.description === 'string' ? schema.description : '',
`${label} opaque schema at ${path} description`
)
}
const properties = schema.properties
if (properties && typeof properties === 'object' && !Array.isArray(properties)) {
for (const [name, property] of Object.entries(properties as JsonObject)) {
invariant(property && typeof property === 'object', `${label}.${name} is not a schema`)
const propertySchema = property as JsonObject
if (!propertySchema.$ref) {
nonEmpty(
typeof propertySchema.description === 'string' ? propertySchema.description : '',
`${label} property at ${path}.${name} description`
)
}
validateNoSilentOpaqueSchemas(propertySchema, label, `${path}.${name}`)
}
}
if (schema.items && typeof schema.items === 'object' && !Array.isArray(schema.items)) {
validateNoSilentOpaqueSchemas(schema.items as JsonObject, label, `${path}[]`)
}
for (const keyword of ['prefixItems', 'oneOf', 'anyOf', 'allOf'] as const) {
const variants = schema[keyword]
if (!Array.isArray(variants)) continue
for (const [index, variant] of variants.entries()) {
invariant(variant && typeof variant === 'object', `${label} ${keyword} entry is not a schema`)
validateNoSilentOpaqueSchemas(variant as JsonObject, label, `${path}.${keyword}[${index}]`)
}
}
if (
schema.additionalProperties &&
typeof schema.additionalProperties === 'object' &&
!Array.isArray(schema.additionalProperties)
) {
validateNoSilentOpaqueSchemas(
schema.additionalProperties as JsonObject,
label,
`${path}.additionalProperties`
)
}
}
function generateSchema(
schema: ApiSchema,
io: SchemaIo,
components: JsonObject,
label: string,
includeRootComponent = true
): { name: string; schema: JsonObject; metadata: z.core.GlobalMeta } {
const metadata = schemaMetadata(schema, label)
let generatedWithDefinitions = generatedSchemaCache.get(schema)?.get(io)
if (!generatedWithDefinitions) {
generatedWithDefinitions = z.toJSONSchema(schema, {
io,
target: 'draft-2020-12',
unrepresentable: 'any',
cycles: 'ref',
reused: 'inline',
override: ({ zodSchema, jsonSchema, path }) => {
const current = zodSchema as ApiSchema
const metadata = z.globalRegistry.get(current)
const schemaLabel = `${label} at ${path.join('.') || '<root>'}`
validateExamples(current, metadata?.examples, io, schemaLabel)
omitEnumValuesFromOpenApi(metadata, jsonSchema as JsonObject, schemaLabel)
},
}) as JsonObject
const byIo = generatedSchemaCache.get(schema) ?? new Map<SchemaIo, JsonObject>()
byIo.set(io, generatedWithDefinitions)
generatedSchemaCache.set(schema, byIo)
}
const { $defs: definitions, ...generated } = generatedWithDefinitions
if (definitions !== undefined) {
invariant(
definitions && typeof definitions === 'object' && !Array.isArray(definitions),
`${label} emitted invalid $defs`
)
for (const [name, definition] of Object.entries(definitions as JsonObject)) {
const component = sanitizeSchema(definition)
validateNoSilentOpaqueSchemas(component, `${label} component ${name}`)
addComponent(components, name, component, label)
}
}
const name = metadata.id
nonEmpty(name ?? '', `${label} metadata.id`)
const output = sanitizeSchema(generated)
validateNoSilentOpaqueSchemas(output, label)
if (includeRootComponent) addComponent(components, name as string, output, label)
return { name: name as string, schema: output, metadata }
}
function objectProperties(
schema: ApiSchema,
components: JsonObject,
label: string
): { properties: JsonObject; required: Set<string> } {
const generatedSchema = generateSchema(schema, 'input', components, label, false)
const generated = generatedSchema.schema
invariant(generated.type === 'object', `${label} must generate an object schema`)
invariant(
generated.properties &&
typeof generated.properties === 'object' &&
!Array.isArray(generated.properties),
`${label} must generate object properties`
)
return {
properties: generated.properties as JsonObject,
required: new Set((generated.required as string[] | undefined) ?? []),
}
}
/**
* Whether a request slice declares no keys at all, i.e. `z.object({}).strict()`.
*
* A v2 contract states that an endpoint takes no query params by declaring
* `query: noInputSchema` rather than by omitting `query`, because an omitted
* slice is the one `parseRequest` skips validating entirely. That distinction is
* load-bearing at runtime and invisible to the spec: either way the operation
* publishes zero parameters.
*/
function declaresNoKeys(schema: ApiSchema): boolean {
const def = (schema as { def?: { type?: string; shape?: Record<string, unknown> } }).def
return def?.type === 'object' && Object.keys(def.shape ?? {}).length === 0
}
function parametersFor(
schema: ApiSchema | undefined,
location: 'path' | 'query' | 'header',
components: JsonObject,
label: string
): JsonObject[] {
if (!schema) return []
/**
* Short-circuited ahead of `objectProperties`, which would otherwise demand
* the `.meta({ id })` and the non-empty `properties` an empty schema has
* nothing to supply, and would register a component no operation references.
*/
if (declaresNoKeys(schema)) return []
const { properties, required } = objectProperties(schema, components, label)
return Object.entries(properties).map(([name, property]) => {
invariant(property && typeof property === 'object', `${label}.${name} is not a schema`)
const description = (property as JsonObject).description
nonEmpty(typeof description === 'string' ? description : '', `${label}.${name} description`)
return {
name,
in: location,
required: location === 'path' || required.has(name),
description,
schema: property,
}
})
}
function contractPathToOpenApi(path: string): string {
nonEmpty(path, 'Contract path')
const converted = path.replace(/\[([^\]]+)\]/g, '{$1}')
invariant(!converted.includes('[') && !converted.includes(']'), `Invalid contract path ${path}`)
return converted
}
function contractStatuses(contract: AnyApiRouteContract): number[] {
const configured = contract.response.status
const statuses =
configured === undefined ? [200] : Array.isArray(configured) ? [...configured] : [configured]
invariant(statuses.length > 0, `${contract.method} ${contract.path} has no success status`)
const minimum = contract.response.mode === 'redirect' ? 300 : HTTP_SUCCESS_MIN
const maximum = contract.response.mode === 'redirect' ? 399 : HTTP_SUCCESS_MAX
for (const status of statuses) {
invariant(
Number.isInteger(status) && status >= minimum && status <= maximum,
`${contract.method} ${contract.path} has invalid success status ${status}`
)
}
return statuses
}
function defaultSuccessContent(
route: OpenApiRouteDefinition,
components: JsonObject,
label: string
): JsonObject | undefined {
const { response } = route.contract
invariant(!('byStatus' in route.operation.success), `${label} requires status-specific content`)
const contentTypes = route.operation.success.contentTypes
if (response.mode === 'empty' || response.mode === 'redirect') {
invariant(!contentTypes, `${label} ${response.mode} response cannot declare content types`)
if (response.mode === 'redirect') {
invariant(
route.operation.success.headers?.includes('Location'),
`${label} redirect response must document the Location header`
)
}
return undefined
}
if (response.mode === 'json') {
invariant(!contentTypes, `${label} JSON response content type is fixed`)
const schema = route.schemas.response
invariant(schema, `${label} is missing its documented response schema`)
const generated = generateSchema(schema, 'output', components, `${label} response`)
validateExamples(schema, generated.metadata.examples, 'output', `${label} response`)
return {
'application/json': { schema: { $ref: `#/components/schemas/${generated.name}` } },
}
}
invariant(contentTypes?.length, `${label} ${response.mode} response requires content types`)
const schema =
response.mode === 'binary' ? { type: 'string', format: 'binary' } : { type: 'string' }
return Object.fromEntries(contentTypes.map((contentType) => [contentType, { schema }]))
}
function statusSuccessContent(
route: OpenApiRouteDefinition,
status: number,
success: OpenApiStatusSuccessMetadata,
components: JsonObject,
label: string
): JsonObject | undefined {
const { response } = route.contract
if (response.mode !== 'json') {
invariant(
!success.additionalContentTypes,
`${label} non-JSON response cannot declare additional content types`
)
return defaultSuccessContent(
{ ...route, operation: { ...route.operation, success } },
components,
label
)
}
const schema = route.schemas.responses?.[status] ?? route.schemas.response
invariant(schema, `${label} is missing its documented response schema`)
const generated = generateSchema(schema, 'output', components, `${label} response`)
validateExamples(schema, generated.metadata.examples, 'output', `${label} response`)
const content: JsonObject = {
'application/json': { schema: { $ref: `#/components/schemas/${generated.name}` } },
}
for (const contentType of success.additionalContentTypes ?? []) {
invariant(contentType !== 'application/json', `${label} repeats the JSON content type`)
content[contentType] = { schema: { type: 'string' } }
}
return content
}
function validateExamples(schema: ApiSchema, examples: unknown, io: SchemaIo, label: string): void {
if (examples === undefined) return
invariant(
Array.isArray(examples) && examples.length > 0,
`${label} examples must be a non-empty array`
)
for (const [index, example] of examples.entries()) {
if (io === 'input') {
const result = schema.safeParse(example)
invariant(
result.success,
`${label} example ${index + 1} is invalid for the input schema: ${result.error?.issues[0]?.message ?? 'validation failed'}`
)
continue
}
let validate = outputValidatorCache.get(schema)
if (!validate) {
const outputSchema = z.toJSONSchema(schema, {
io: 'output',
target: 'draft-2020-12',
unrepresentable: 'any',
cycles: 'ref',
reused: 'inline',
})
validate = outputExampleValidator.compile(stripLegacySchemaIds(outputSchema))
outputValidatorCache.set(schema, validate)
}
invariant(
validate(example),
`${label} example ${index + 1} is invalid for the output schema: ${outputExampleValidator.errorsText(validate.errors)}`
)
}
}
function requestBodyFor(
route: OpenApiRouteDefinition,
components: JsonObject,
label: string
): JsonObject | undefined {
const requestBody = route.schemas.requestBody
const schema = route.schemas.body ?? requestBody?.schema
if (!schema) return undefined
const generated = generateSchema(schema, 'input', components, `${label} body`)
validateExamples(schema, generated.metadata.examples, 'input', `${label} body`)
const contentTypes = requestBody?.contentTypes ?? ['application/json']
invariant(contentTypes.length > 0, `${label} request body must declare a content type`)
invariant(
new Set(contentTypes).size === contentTypes.length,
`${label} request body repeats a content type`
)
for (const contentType of contentTypes) {
nonEmpty(contentType, `${label} request body content type`)
}
return {
required: !schema.safeParse(undefined).success,
description: generated.metadata.description,
content: Object.fromEntries(
contentTypes.map((contentType) => [
contentType,
{ schema: { $ref: `#/components/schemas/${generated.name}` } },
])
),
}
}
function referencedHeaders(headerNames: readonly string[] | undefined): JsonObject | undefined {
if (!headerNames?.length) return undefined
return Object.fromEntries(
headerNames.map((name) => [name, { $ref: `#/components/headers/${name}` }])
)
}
/** Public APIs reject MCP resource-bound grants; API read access also satisfies Search reads. */
function publicApiOAuthScope(
scope: OpenApiOperationMetadata['applicationOperation']['oauthScope']
) {
return scope === 'search:read' ? 'api:read' : scope
}
function operationFor(
route: OpenApiRouteDefinition,
definition: OpenApiDocumentDefinition,
components: JsonObject
): JsonObject {
const { contract, operation, schemas } = route
const label = `${contract.method} ${contract.path}`
validateOperationMetadata(operation, definition, label)
invariant(
Boolean(contract.params) === Boolean(schemas.params),
`${label} params metadata mismatch`
)
invariant(Boolean(contract.query) === Boolean(schemas.query), `${label} query metadata mismatch`)
invariant(Boolean(contract.body) === Boolean(schemas.body), `${label} body metadata mismatch`)
invariant(
!(contract.body && schemas.requestBody),
`${label} cannot declare both a contract body and a documentation-only request body`
)
invariant(
Boolean(contract.headers) === Boolean(schemas.headers),
`${label} headers metadata mismatch`
)
invariant(
(contract.response.mode === 'json') === Boolean(schemas.response),
`${label} response metadata mismatch`
)
const documentedContractSchemas = [
['params', contract.params, schemas.params, 'input'],
['query', contract.query, schemas.query, 'input'],
['body', contract.body, schemas.body, 'input'],
['headers', contract.headers, schemas.headers, 'input'],
[
'response',
contract.response.mode === 'json' ? contract.response.schema : undefined,
schemas.response,
'output',
],
] as const
for (const [name, contractSchema, documentedSchema, io] of documentedContractSchemas) {
if (!contractSchema) continue
invariant(documentedSchema, `${label} is missing its documented ${name} schema`)
invariant(
contractSchema === documentedSchema ||
JSON.stringify(comparableSchema(contractSchema, io)) ===
JSON.stringify(comparableSchema(documentedSchema, io)),
`${label} documented ${name} schema does not match the contract schema`
)
}
invariant(
!schemas.responses || contract.response.mode === 'json',
`${label} non-JSON response cannot declare status-specific schemas`
)
if (schemas.response) {
schemaMetadata(schemas.response, `${label} contract response schema`)
}
const contractStatusSchemas =
contract.response.mode === 'json' ? contract.response.statusSchemas : undefined
invariant(
Boolean(contractStatusSchemas) === Boolean(schemas.responses),
`${label} status-specific contract schema metadata mismatch`
)
if (contractStatusSchemas && schemas.responses) {
const contractSchemaStatuses = Object.keys(contractStatusSchemas).map(Number)
const documentedSchemaStatuses = Object.keys(schemas.responses).map(Number)
const expectedStatuses = contractStatuses(contract)
for (const [name, statuses] of [
['contract', contractSchemaStatuses],
['documented', documentedSchemaStatuses],
] as const) {
invariant(
JSON.stringify([...statuses].sort((a, b) => a - b)) ===
JSON.stringify([...expectedStatuses].sort((a, b) => a - b)),
`${label} ${name} status-specific schemas do not match the contract statuses`
)
}
for (const status of expectedStatuses) {
invariant(
contractStatusSchemas[status] === schemas.responses[status] ||
JSON.stringify(comparableSchema(contractStatusSchemas[status], 'output')) ===
JSON.stringify(comparableSchema(schemas.responses[status], 'output')),
`${label} documented schema for status ${status} does not match the contract schema`
)
}
}
const parameters = [
...parametersFor(schemas.params, 'path', components, `${label} params`),
...parametersFor(schemas.query, 'query', components, `${label} query`),
...parametersFor(schemas.headers, 'header', components, `${label} headers`),
]
const pathParameterNames = new Set(
[...contract.path.matchAll(/\[([^\]]+)\]/g)].map((match) => match[1])
)
const documentedPathNames = new Set(
parameters.filter((parameter) => parameter.in === 'path').map((parameter) => parameter.name)
)
invariant(
JSON.stringify([...pathParameterNames].sort()) ===
JSON.stringify([...documentedPathNames].sort()),
`${label} path parameters do not match the contract path`
)
const responses: JsonObject = {}
const statuses = contractStatuses(contract)
if ('byStatus' in operation.success) {
const documentedStatuses = Object.keys(operation.success.byStatus)
.map(Number)
.sort((a, b) => a - b)
invariant(
JSON.stringify(documentedStatuses) === JSON.stringify([...statuses].sort((a, b) => a - b)),
`${label} status-specific responses do not match the contract statuses`
)
const schemaStatuses = Object.keys(schemas.responses ?? {}).map(Number)
invariant(
schemaStatuses.every((status) => documentedStatuses.includes(status)),
`${label} status-specific schemas include an undocumented status`
)
for (const status of statuses) {
const success = operation.success.byStatus[status]
invariant(success, `${label} is missing success metadata for status ${status}`)
const content = statusSuccessContent(route, status, success, components, `${label} ${status}`)
responses[String(status)] = {
description: success.description,
...(referencedHeaders(success.headers)
? { headers: referencedHeaders(success.headers) }
: {}),
...(content ? { content } : {}),
}
}
} else {
invariant(
!schemas.responses,
`${label} cannot declare status-specific schemas without status-specific success metadata`
)
const content = defaultSuccessContent(route, components, label)
for (const status of statuses) {
responses[String(status)] = {
description: operation.success.description,
...(referencedHeaders(operation.success.headers)
? { headers: referencedHeaders(operation.success.headers) }
: {}),
...(content ? { content } : {}),
}
}
}
for (const errorId of operation.errors) {
const error = definition.errorResponses[errorId]
invariant(error, `${label} references unknown error response ${errorId}`)
responses[String(error.status)] = { $ref: `#/components/responses/${errorId}` }
}
const requestBody = requestBodyFor(route, components, label)
const oauthScope = publicApiOAuthScope(operation.applicationOperation.oauthScope)
return {
operationId: operation.operationId,
summary: operation.summary,
description: oauthScope
? `${operation.description}\n\nOAuth scope: \`${oauthScope}\`.`
: operation.description,
'x-sim-operation': operation.applicationOperation.id,
...(oauthScope ? { 'x-oauth-scope': oauthScope } : {}),
tags: [...operation.tags],
...(operation.deprecated === undefined ? {} : { deprecated: operation.deprecated }),
...(operation.security === undefined ? {} : { security: operation.security }),
...(parameters.length === 0 ? {} : { parameters }),
...(requestBody ? { requestBody } : {}),
responses,
}
}
function validateOperationMetadata(
operation: OpenApiOperationMetadata,
definition: OpenApiDocumentDefinition,
label: string
): void {
nonEmpty(operation.applicationOperation.id, `${label} application operation id`)
const security = operation.security ?? definition.security
if (security.some((requirement) => 'oauthBearer' in requirement)) {
const oauthScope = publicApiOAuthScope(operation.applicationOperation.oauthScope)
invariant(
oauthScope === 'api:read' || oauthScope === 'api:write',
`${label} must declare its canonical application's OAuth scope`
)
}
nonEmpty(operation.operationId, `${label} operationId`)
nonEmpty(operation.summary, `${label} summary`)
nonEmpty(operation.description, `${label} description`)
if ('byStatus' in operation.success) {
invariant(
Object.keys(operation.success.byStatus).length > 0,
`${label} must declare status-specific success responses`
)
for (const [status, success] of Object.entries(operation.success.byStatus)) {
nonEmpty(success.description, `${label} ${status} success description`)
}
} else {
nonEmpty(operation.success.description, `${label} success description`)
}
invariant(operation.tags.length > 0, `${label} must declare at least one tag`)
for (const tag of operation.tags) {
invariant(
definition.tags.some((entry) => entry.name === tag),
`${label} references unknown tag ${tag}`
)
}
invariant(operation.errors.length > 0, `${label} must declare error responses`)
const errorStatuses = operation.errors.map((errorId) => {
const response = definition.errorResponses[errorId]
invariant(response, `${label} references unknown error response ${errorId}`)
return response.status
})
invariant(
new Set(errorStatuses).size === errorStatuses.length,
`${label} repeats an error status`
)
invariant(errorStatuses.includes(401), `${label} must document a 401 response`)
invariant(errorStatuses.includes(429), `${label} must document a 429 response`)
const successResponses =
'byStatus' in operation.success
? Object.values(operation.success.byStatus)
: [operation.success]
for (const success of successResponses) {
for (const header of success.headers ?? []) {
invariant(definition.headers[header], `${label} references unknown success header ${header}`)
}
}
validateSecurity(operation.security ?? definition.security, definition, label, true)
}
function validateSecurity(
requirements: readonly Readonly<Record<string, readonly string[]>>[],
definition: OpenApiDocumentDefinition,
label: string,
allowAnonymous = false
): void {
invariant(requirements.length > 0, `${label} must declare a security requirement`)
for (const requirement of requirements) {
invariant(
allowAnonymous || Object.keys(requirement).length > 0,
`${label} has an empty security requirement`
)
for (const [scheme, scopes] of Object.entries(requirement)) {
invariant(
definition.securitySchemes[scheme],
`${label} references unknown security scheme ${scheme}`
)
invariant(
Array.isArray(scopes) && scopes.length === 0,
`${label} ${scheme} security requirement must use an empty scope array`
)
}
}
}
/**
* The error responses this document's operations actually reference, as `components.responses`.
*
* Only the referenced ones are built. An error response carries a worked example, and the
* message in one is often the domain's rather than the surface's — a `423` reads `Workflow is
* locked` in one document and `This table is insert-locked` in another. Emitting the whole set
* everywhere would ship each document a body for a status it never answers, phrased by a
* domain it does not contain.
*/
function errorComponents(
definition: OpenApiDocumentDefinition,
schemas: JsonObject,
referencedErrors: ReadonlySet<string>
): JsonObject {
const generated = generateSchema(
definition.errorSchema,
'output',
schemas,
'OpenAPI error schema'
)
validateExamples(
definition.errorSchema,
generated.metadata.examples,
'output',
'OpenAPI error schema'
)
const responses: JsonObject = {}
for (const [id, response] of Object.entries(definition.errorResponses)) {
if (!referencedErrors.has(id)) continue
nonEmpty(id, 'Error response id')
nonEmpty(response.description, `${id} description`)
invariant(
Number.isInteger(response.status) &&
response.status >= HTTP_ERROR_MIN &&
response.status <= HTTP_ERROR_MAX,
`${id} has invalid error status ${response.status}`
)
for (const header of response.headers ?? []) {
invariant(definition.headers[header], `${id} references unknown response header ${header}`)
}
/**
* Held to the same standard as every other documented example: it must parse against
* the error schema, so a documented body cannot describe a shape the API never sends.
*/
validateExamples(definition.errorSchema, [response.example], 'output', `${id} example`)
responses[id] = {
description: response.description,
...(referencedHeaders(response.headers)
? { headers: referencedHeaders(response.headers) }
: {}),
content: {
'application/json': {
schema: { $ref: `#/components/schemas/${generated.name}` },
/**
* Sits beside the `$ref` rather than on the shared schema: one schema serves every
* status, so a schema-level example is necessarily one status's body shown under
* all of them. A Media Type Object example overrides the schema's, which is what
* makes each status tab show its own.
*/
example: response.example,
},
},
}
}
return responses
}
export function generateOpenApiDocument(definition: OpenApiDocumentDefinition): JsonObject {
nonEmpty(definition.output, 'OpenAPI output')
nonEmpty(definition.info.title, 'OpenAPI info.title')
nonEmpty(definition.info.description, 'OpenAPI info.description')
nonEmpty(definition.info.version, 'OpenAPI info.version')
invariant(definition.routes.length > 0, 'OpenAPI document has no routes')
validateSecurity(definition.security, definition, 'OpenAPI document')
const schemas: JsonObject = {}
const referencedErrors = new Set(
definition.routes.flatMap((route) => [...route.operation.errors])
)
const responses = errorComponents(definition, schemas, referencedErrors)
const paths: JsonObject = {}
const operationIds = new Set<string>()
const routeKeys = new Set<string>()
for (const route of definition.routes) {
const openApiPath = contractPathToOpenApi(route.contract.path)
const method = route.contract.method.toLowerCase()
const routeKey = `${method.toUpperCase()} ${openApiPath}`
invariant(!routeKeys.has(routeKey), `Duplicate OpenAPI route ${routeKey}`)
routeKeys.add(routeKey)
invariant(
!operationIds.has(route.operation.operationId),
`Duplicate operationId ${route.operation.operationId}`
)
operationIds.add(route.operation.operationId)
const pathItem = (paths[openApiPath] ?? {}) as JsonObject
pathItem[method] = operationFor(route, definition, schemas)
paths[openApiPath] = pathItem
}
for (const [name, header] of Object.entries(definition.headers)) {
schemaMetadata(header.schema, `${name} header schema`)
}
return {
openapi: '3.1.0',
info: definition.info,
servers: definition.servers,
tags: definition.tags,
security: definition.security,
paths,
components: {
securitySchemes: definition.securitySchemes,
headers: Object.fromEntries(
Object.entries(definition.headers).map(([name, header]) => {
const metadata = schemaMetadata(header.schema, `${name} header schema`)
return [
name,
{
description: metadata.description,
schema: sanitizeSchema(
z.toJSONSchema(header.schema, {
io: 'output',
unrepresentable: 'throw',
cycles: 'throw',
})
),
},
]
})
),
responses,
schemas,
},
'x-generated-by': 'scripts/generate-openapi.ts',
}
}
export function serializeOpenApiDocument(definition: OpenApiDocumentDefinition): string {
return `${JSON.stringify(generateOpenApiDocument(definition), null, 2)}\n`
}
export { contractPathToOpenApi }