Skip to content

Commit 10f2b05

Browse files
fix: Merge all schemas with the same action_type in action attempts (#164)
Co-authored-by: Seam Bot <seambot@getseam.com>
1 parent e5b3bdd commit 10f2b05

File tree

3 files changed

+1128
-149
lines changed

3 files changed

+1128
-149
lines changed

src/lib/blueprint.ts

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,42 +1265,78 @@ const createActionAttempts = (
12651265
return []
12661266
}
12671267

1268-
const processedActionTypes = new Set<string>()
1268+
const schemasByActionType = new Map<string, OpenapiSchema[]>()
12691269

1270-
return actionAttemptSchema.oneOf
1271-
.map((schema) => {
1272-
if (
1273-
typeof schema !== 'object' ||
1274-
schema.properties?.['action_type']?.enum?.[0] == null
1275-
) {
1276-
return null
1270+
for (const schema of actionAttemptSchema.oneOf) {
1271+
if (
1272+
typeof schema !== 'object' ||
1273+
schema.properties?.['action_type']?.enum?.[0] == null
1274+
) {
1275+
continue
1276+
}
1277+
1278+
const actionType = schema.properties['action_type'].enum[0]
1279+
const currentSchemas = schemasByActionType.get(actionType)
1280+
1281+
if (currentSchemas == null) {
1282+
schemasByActionType.set(actionType, [schema])
1283+
} else {
1284+
currentSchemas.push(schema)
1285+
}
1286+
}
1287+
1288+
return Array.from(schemasByActionType.entries()).map(
1289+
([actionType, schemas]) => {
1290+
const mergedProperties: Record<string, OpenapiSchema> = {}
1291+
1292+
const allPropertyKeys = new Set<string>()
1293+
for (const schema of schemas) {
1294+
if (schema.properties != null) {
1295+
Object.keys(schema.properties).forEach((key) =>
1296+
allPropertyKeys.add(key),
1297+
)
1298+
}
12771299
}
12781300

1279-
const actionType = schema.properties['action_type'].enum[0]
1301+
for (const propKey of allPropertyKeys) {
1302+
const propDefinitions = schemas
1303+
.filter((schema) => schema.properties?.[propKey] != null)
1304+
.map((schema) => schema.properties?.[propKey])
12801305

1281-
if (processedActionTypes.has(actionType)) {
1282-
return null
1306+
if (propDefinitions.length === 0) continue
1307+
1308+
const nonNullableDefinition = propDefinitions.find((prop) => {
1309+
if (prop == null) return false
1310+
1311+
return !(
1312+
'nullable' in prop &&
1313+
prop.nullable === true &&
1314+
Object.keys(prop).length <= 1
1315+
)
1316+
})
1317+
1318+
mergedProperties[propKey] =
1319+
nonNullableDefinition ?? propDefinitions[0] ?? {}
12831320
}
1284-
processedActionTypes.add(actionType)
12851321

1286-
const schemaWithStandardStatus: OpenapiSchema = {
1287-
...(actionAttemptSchema['x-route-path'] !== null && {
1322+
// Ensure standard status field
1323+
mergedProperties['status'] = {
1324+
...mergedProperties['status'],
1325+
type: 'string',
1326+
enum: ['success', 'pending', 'error'],
1327+
}
1328+
1329+
const schemaWithMergedProperties: OpenapiSchema = {
1330+
...(actionAttemptSchema['x-route-path'] != null && {
12881331
'x-route-path': actionAttemptSchema['x-route-path'],
12891332
}),
1290-
...schema,
1291-
properties: {
1292-
...schema.properties,
1293-
status: {
1294-
...schema.properties['status'],
1295-
type: 'string',
1296-
enum: ['success', 'pending', 'error'],
1297-
},
1298-
},
1333+
...schemas[0],
1334+
properties: mergedProperties,
12991335
}
13001336

13011337
const resource = createResource(
13021338
'action_attempt',
1303-
schemaWithStandardStatus,
1339+
schemaWithMergedProperties,
13041340
routes,
13051341
)
13061342

@@ -1309,6 +1345,6 @@ const createActionAttempts = (
13091345
resourceType: 'action_attempt',
13101346
actionAttemptType: actionType,
13111347
}
1312-
})
1313-
.filter((attempt): attempt is ActionAttempt => attempt !== null)
1348+
},
1349+
)
13141350
}

0 commit comments

Comments
 (0)