Skip to content

Commit 2e82db5

Browse files
Validate that x-route-path actually exists in routes
1 parent 8bbcf4a commit 2e82db5

File tree

2 files changed

+67
-15
lines changed

2 files changed

+67
-15
lines changed

src/lib/blueprint.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -306,14 +306,15 @@ export const createBlueprint = async (
306306
formatCode,
307307
}
308308

309-
const resources = createResources(openapi.components.schemas)
309+
const routes = await createRoutes(openapi.paths, context)
310+
const resources = createResources(openapi.components.schemas, routes)
310311

311312
return {
312313
title: openapi.info.title,
313-
routes: await createRoutes(openapi.paths, context),
314+
routes,
314315
resources,
315-
events: createEvents(openapi.components.schemas, resources),
316-
actionAttempts: createActionAttempts(openapi.components.schemas),
316+
events: createEvents(openapi.components.schemas, resources, routes),
317+
actionAttempts: createActionAttempts(openapi.components.schemas, routes),
317318
}
318319
}
319320

@@ -788,6 +789,7 @@ const createParameter = (
788789

789790
export const createResources = (
790791
schemas: Openapi['components']['schemas'],
792+
routes: Route[],
791793
): Record<string, Resource> => {
792794
return Object.entries(schemas).reduce<Record<string, Resource>>(
793795
(resources, [schemaName, schema]) => {
@@ -804,7 +806,7 @@ export const createResources = (
804806
}
805807
return {
806808
...resources,
807-
[schemaName]: createResource(schemaName, eventSchema),
809+
[schemaName]: createResource(schemaName, eventSchema, routes),
808810
}
809811
}
810812

@@ -813,7 +815,7 @@ export const createResources = (
813815
if (isValidResourceSchema) {
814816
return {
815817
...resources,
816-
[schemaName]: createResource(schemaName, schema),
818+
[schemaName]: createResource(schemaName, schema, routes),
817819
}
818820
}
819821

@@ -826,11 +828,9 @@ export const createResources = (
826828
const createResource = (
827829
schemaName: string,
828830
schema: OpenapiSchema,
831+
routes: Route[],
829832
): Resource => {
830-
const routePath = schema['x-route-path']
831-
if (routePath == null || routePath.length === 0) {
832-
throw new Error(`Missing route path for ${schemaName}`)
833-
}
833+
const routePath = validateRoutePath(schema['x-route-path'], routes)
834834

835835
return {
836836
resourceType: schemaName,
@@ -846,6 +846,20 @@ const createResource = (
846846
}
847847
}
848848

849+
const validateRoutePath = (
850+
routePath: string | undefined,
851+
routes: Route[],
852+
): string => {
853+
if (routePath == null || routePath.length === 0) {
854+
throw new Error('Missing route path')
855+
}
856+
if (!routes.some((r) => r.path === routePath)) {
857+
throw new Error(`Route path ${routePath} not found in routes`)
858+
}
859+
860+
return routePath
861+
}
862+
849863
const createResponse = (
850864
operation: OpenapiOperation,
851865
path: string,
@@ -1095,6 +1109,7 @@ export const getPreferredMethod = (
10951109
const createEvents = (
10961110
schemas: Openapi['components']['schemas'],
10971111
resources: Record<string, Resource>,
1112+
routes: Route[],
10981113
): EventResource[] => {
10991114
const eventSchema = schemas['event']
11001115
if (
@@ -1121,7 +1136,7 @@ const createEvents = (
11211136
)
11221137

11231138
return {
1124-
...createResource('event', schema as OpenapiSchema),
1139+
...createResource('event', schema as OpenapiSchema, routes),
11251140
eventType,
11261141
targetResourceType: targetResourceType ?? null,
11271142
}
@@ -1131,6 +1146,7 @@ const createEvents = (
11311146

11321147
const createActionAttempts = (
11331148
schemas: Openapi['components']['schemas'],
1149+
routes: Route[],
11341150
): ActionAttempt[] => {
11351151
const actionAttemptSchema = schemas['action_attempt']
11361152
if (
@@ -1176,6 +1192,7 @@ const createActionAttempts = (
11761192
const resource = createResource(
11771193
'action_attempt',
11781194
schemaWithStandardStatus,
1195+
routes,
11791196
)
11801197

11811198
return {

test/fixtures/types/openapi.ts

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export default {
8484
},
8585
},
8686
required: ['plane_id', 'name'],
87-
'x-route-path': '/planes',
87+
'x-route-path': '/transport/air/planes',
8888
},
8989
deprecated_resource: {
9090
type: 'object',
@@ -99,7 +99,7 @@ export default {
9999
required: ['deprecated_resource_id'],
100100
deprecated: true,
101101
'x-deprecated': 'This resource is deprecated',
102-
'x-route-path': '/deprecated/resources',
102+
'x-route-path': '/deprecated/undocumented',
103103
},
104104
draft_resource: {
105105
type: 'object',
@@ -113,7 +113,7 @@ export default {
113113
},
114114
required: ['draft_resource_id'],
115115
'x-draft': 'This resource is draft',
116-
'x-route-path': '/draft/resources',
116+
'x-route-path': '/draft',
117117
},
118118
undocumented_resource: {
119119
type: 'object',
@@ -127,7 +127,7 @@ export default {
127127
},
128128
required: ['undocumented_resource_id'],
129129
'x-undocumented': 'This resource is undocumented',
130-
'x-route-path': '/undocumented/resources',
130+
'x-route-path': '/deprecated/undocumented',
131131
},
132132
event: {
133133
'x-route-path': '/events',
@@ -476,5 +476,40 @@ export default {
476476
'x-title': 'Draft endpoint',
477477
},
478478
},
479+
'/action_attempts/get': {
480+
post: {
481+
operationId: 'actionAttemptsGetPost',
482+
responses: {
483+
200: {
484+
content: {
485+
'application/json': {
486+
schema: {
487+
properties: {
488+
ok: { type: 'boolean' },
489+
action_attempt: {
490+
$ref: '#/components/schemas/action_attempt',
491+
},
492+
},
493+
required: ['action_attempt', 'ok'],
494+
type: 'object',
495+
},
496+
},
497+
},
498+
description: 'Get an action attempt.',
499+
},
500+
400: { description: 'Bad Request' },
501+
401: { description: 'Unauthorized' },
502+
},
503+
security: [
504+
{
505+
api_key: [],
506+
},
507+
],
508+
summary: '/action_attempts/get',
509+
tags: ['/action_attempts'],
510+
'x-response-key': 'action_attempt',
511+
'x-title': 'Get an action attempt',
512+
},
513+
},
479514
},
480515
}

0 commit comments

Comments
 (0)