Skip to content

Commit bb5ec77

Browse files
authored
fix(openapi): serialize the document before downgrading to OpenAPI 3.1/3.0 (#2439)
## Problem `OpenAPIGenerator.generate` downgraded the 3.2 document to the requested version first and serialized it afterwards. That meant the downgraders worked on non-JSON values. For example, a schema with `const: Infinity` downgraded to 3.0 became `{ type: 'number', enum: [Infinity] }`. The serializer then wrote that as `enum: [null]` on a non-nullable `number`, which is an invalid schema and disagrees with what the runtime serializer sends. ## Fix Serialize the 3.2 document first, then downgrade the JSON form. `const: Infinity` now goes through the downgrader as `const: null`, the same as an explicit `const: null`. Earlier commits also dropped `undefined` fields before serializing, so a serializer configured with `omitUndefinedProperties: false` couldn't write `summary: null`. That option was removed in #2441, so the serializer always omits undefined properties and the extra pass is gone again. ## Tests `downgrades non-JSON schema values as their serialized form in OpenAPI %s`: fails on `main` for 3.0.4 and passes with this change. `pnpm check` and `pnpm test` pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01VVaw7RubDmUVdyQ2K8XBxp
1 parent 12df131 commit bb5ec77

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

‎packages/openapi/src/openapi-generator.test.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { oc } from '@orpc/contract'
2+
import type { JsonSchema } from '@orpc/json-schema'
23
import { visitJsonSchemaRefs } from '@orpc/json-schema'
34
import * as arktype from 'arktype'
45
import * as z from 'zod'
@@ -883,7 +884,7 @@ describe('openAPIGenerator version', () => {
883884
},
884885
)
885886

886-
it('serializes non-JSON values after downgrading', async () => {
887+
it('serializes non-JSON values', async () => {
887888
const doc = await generator.generate(
888889
{},
889890
{
@@ -900,4 +901,20 @@ describe('openAPIGenerator version', () => {
900901

901902
expect(doc.info['x-generated-at']).toBe('2020-01-01T00:00:00.000Z')
902903
})
904+
905+
it.each(['3.1.0', '3.0.4'] as const)(
906+
'downgrades non-JSON schema values as their serialized form in OpenAPI %s',
907+
async (version) => {
908+
const routerWith = (value: JsonSchema) => ({
909+
ping: oc
910+
.meta(openapi({ method: 'GET', path: '/ping' }))
911+
.output(testSchema({ type: 'object', properties: { value } })),
912+
})
913+
914+
const infinity = await generator.generate(routerWith({ const: Infinity }), { version })
915+
const serialized = await generator.generate(routerWith({ const: null }), { version })
916+
917+
expect(infinity).toEqual(serialized)
918+
},
919+
)
903920
})

‎packages/openapi/src/openapi-generator.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,9 @@ export class OpenAPIGenerator {
262262
)
263263
}
264264

265-
const versioned = toVersionedOpenAPIDocument(doc, version)
266-
return this.serializer.serializeJson(versioned).json as OpenAPIDocument<TVersion>
265+
// serialize first so the downgraders work on JSON values, e.g. `Infinity` as `null`
266+
const serialized = this.serializer.serializeJson(doc).json as OpenAPIV3_2.OpenAPIObject
267+
return toVersionedOpenAPIDocument(serialized, version) as OpenAPIDocument<TVersion>
267268
}
268269

269270
private convertSchema(

0 commit comments

Comments
 (0)