Skip to content

Commit

Permalink
fix: threading api types bug (#1642)
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity authored Oct 10, 2024
1 parent 040ccce commit 8f4fbec
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/fdr-sdk/src/api-definition/endpoint-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function createWebSocketContext(
channel,
auth: channel.auth?.map((id) => api.auths[id])[0],
globalHeaders: api.globalHeaders ?? [],
types: api.auths,
types: api.types,
};
}

Expand All @@ -77,6 +77,6 @@ export function createWebhookContext(
return {
node,
webhook,
types: api.auths,
types: api.types,
};
}
19 changes: 14 additions & 5 deletions packages/fdr-sdk/src/api-definition/unwrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export function unwrapReference(
const visitedTypeIds: Latest.TypeId[] = [];
let internalTypeRef: TypeShapeOrReference | undefined = typeRef;
let loop = 0;
let circularReference = false;
while (internalTypeRef != null) {
if (loop > LOOP_TOLERANCE) {
// infinite loop detected
Expand All @@ -94,6 +95,7 @@ export function unwrapReference(
visitedTypeIds.push(internalTypeRef.id);
// circular reference detected
internalTypeRef = undefined;
circularReference = true;
break;
}

Expand All @@ -102,12 +104,12 @@ export function unwrapReference(
}
const typeDef: Latest.TypeDefinition | undefined = types[internalTypeRef.id];
visitedTypeIds.push(internalTypeRef.id);
internalTypeRef = typeDef?.shape;
if (typeDef != null) {
if (typeDef.availability) {
availabilities.push(typeDef.availability);
}

internalTypeRef = typeDef.shape;
if (typeDef.description != null) {
descriptions.push(typeDef.description);
}
Expand All @@ -121,10 +123,17 @@ export function unwrapReference(

if (internalTypeRef == null) {
// Note: this should be a fatal error, but we're handling it gracefully for now
// eslint-disable-next-line no-console
console.error(
`Type reference is invalid. Falling back to unknown type.${visitedTypeIds.length > 0 ? ` path=[${visitedTypeIds.join(", ")}]` : ""}`,
);
if (circularReference) {
// eslint-disable-next-line no-console
console.error(
`Circular reference detected. Falling back to unknown type. path=[${visitedTypeIds.join(", ")}]`,
);
} else {
// eslint-disable-next-line no-console
console.error(
`Type reference is invalid. Falling back to unknown type. path=[${visitedTypeIds.join(", ")}]`,
);
}
}

const toRet = {
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/app/src/api-reference/web-socket/WebSocket.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ function CardedSection({
}
function flattenWebSocketShape(
subscribeMessages: ApiDefinition.WebSocketMessage[],
types: Record<string, ApiDefinition.TypeDefinition>,
types: Record<ApiDefinition.TypeId, ApiDefinition.TypeDefinition>,
) {
return subscribeMessages.flatMap((message): ApiDefinition.UndiscriminatedUnionVariant[] => {
const unwrapped = ApiDefinition.unwrapReference(message.body, types);
Expand Down

0 comments on commit 8f4fbec

Please sign in to comment.