Skip to content

Commit

Permalink
fix: safeSortKeysByShape
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity committed Mar 25, 2024
1 parent f494008 commit dd9469c
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions packages/ui/app/src/util/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { APIV1Read, DocsV1Read, FdrAPI } from "@fern-api/fdr-sdk";
import type { WithoutQuestionMarks } from "@fern-api/fdr-sdk/dist/converters/utils/WithoutQuestionMarks";
import { isNonNullish, titleCase, visitDiscriminatedUnion } from "@fern-ui/core-utils";
import { mapValues, pick, sortBy } from "lodash-es";
import { emitDatadogError } from "../analytics/datadogRum";
import {
endpointExampleToHttpRequestExample,
sortKeysByShape,
Expand Down Expand Up @@ -536,11 +537,8 @@ async function resolveWebhookDefinition(
availability: undefined,
},
examples: webhook.examples.map((example) => {
const sortedPayload = stripUndefines(sortKeysByShape(example.payload, payloadShape, resolvedTypes));
return {
payload: sortedPayload,
// hast: highlight(highlighter, JSON.stringify(sortedPayload, undefined, 2), "json"),
};
const sortedPayload = safeSortKeysByShape(example.payload, payloadShape, resolvedTypes);
return { payload: sortedPayload };
}),
};
}
Expand Down Expand Up @@ -1129,7 +1127,7 @@ function resolveExampleEndpointRequest(
return visitDiscriminatedUnion(requestBodyV3, "type")._visit<ResolvedExampleEndpointRequest | undefined>({
json: (json) => ({
type: "json",
value: json.value != null ? stripUndefines(sortKeysByShape(json.value, shape, resolvedTypes)) : undefined,
value: json.value != null ? safeSortKeysByShape(json.value, shape, resolvedTypes) : undefined,
}),
form: (form) => ({
type: "form",
Expand Down Expand Up @@ -1198,14 +1196,36 @@ function resolveExampleEndpointResponse(
return visitDiscriminatedUnion(responseBodyV3, "type")._visit<ResolvedExampleEndpointResponse | undefined>({
json: (json) => ({
type: "json",
value: json.value != null ? stripUndefines(sortKeysByShape(json.value, shape, resolvedTypes)) : undefined,
value: json.value != null ? safeSortKeysByShape(json.value, shape, resolvedTypes) : undefined,
}),
filename: (filename) => ({ type: "filename", value: filename.value }),
stream: (stream) => ({ type: "stream", value: stream.value }),
_other: () => undefined,
});
}

function safeSortKeysByShape(
value: unknown,
shape: ResolvedHttpResponseBodyShape | undefined,
resolvedTypes: Record<string, ResolvedTypeDefinition>,
): unknown {
try {
return stripUndefines(sortKeysByShape(value, shape, resolvedTypes));
} catch (e) {
// eslint-disable-next-line no-console
console.error("Failed to sort JSON keys by type shape", e);

emitDatadogError(e, {
context: "ApiPage",
errorSource: "sortKeysByShape",
errorDescription:
"Failed to sort and strip undefines from JSON value, indicating a bug in the resolver. This error should be investigated.",
});

return value;
}
}

function stripUndefines(obj: unknown): unknown {
return JSON.parse(JSON.stringify(obj));
}
Expand Down

0 comments on commit dd9469c

Please sign in to comment.