Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/preserve-openapi-raw-json.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Preserve `JSON.rawJSON` values when cloning cached OpenAPI specs.
6 changes: 5 additions & 1 deletion packages/effect/src/unstable/httpapi/OpenApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,15 @@ const compileSchemas: CompileSchemas = (asts) =>
)
)

const jsonWithRawJSON = JSON as unknown as { isRawJSON?: (value: unknown) => boolean }

const isJsonRawJSON = (value: unknown): boolean => jsonWithRawJSON.isRawJSON?.(value) === true

const cloneOpenAPISpec = <A>(value: A): A => {
if (Array.isArray(value)) {
return value.map(cloneOpenAPISpec) as A
}
if (value !== null && typeof value === "object") {
if (value !== null && typeof value === "object" && !isJsonRawJSON(value)) {
const out: Record<string, unknown> = {}
for (const key of Object.keys(value)) {
InternalRecord.assignProperty(out, key, cloneOpenAPISpec((value as Record<string, unknown>)[key]))
Expand Down
16 changes: 16 additions & 0 deletions packages/effect/test/unstable/httpapi/OpenApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ describe("OpenApi", () => {
assert.isUndefined(third.paths["/resource"]!.get!.summary)
})

it("preserves JSON.rawJSON values when cloning cached specs", () => {
const rawJson = (JSON as unknown as { rawJSON: (value: string) => unknown }).rawJSON("9223372036854775807")
const Api = HttpApi.make("Api").annotate(OpenApi.Transform, (spec) => ({
...spec,
info: {
...spec.info,
"x-raw": rawJson
}
}))

const expected = `{"title":"Api","version":"0.0.1","x-raw":9223372036854775807}`

assert.strictEqual(JSON.stringify(OpenApi.fromApi(Api).info), expected)
assert.strictEqual(JSON.stringify(OpenApi.fromApi(Api).info), expected)
})

it("isolates the cached spec from external override mutations", () => {
const info = { title: "Api", version: "1.0.0" }
const Api = HttpApi.make("Api").annotate(OpenApi.Override, { info })
Expand Down