Skip to content

Commit d7c4772

Browse files
authored
fix(openapi): spec generator in case commonSchemas + GET + compact + no params (#920)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Corrected OpenAPI generation for GET endpoints using referenced query schemas, ensuring parameters are accurately expanded and validated. * Improved handling of complex query objects (e.g., deepObject style), producing precise parameter definitions in the spec. * **Tests** * Added a test case for a GET route with a referenced query object to verify correct parameter generation and response schema handling. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 683afe7 commit d7c4772

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,7 @@ describe('openAPIGenerator', () => {
10821082
detailedStructure: oc.route({ path: '/detailed/{pet}', inputStructure: 'detailed', outputStructure: 'detailed' })
10831083
.input(InputDetailedStructure)
10841084
.output(OutputDetailedStructure),
1085+
getWithoutParams: oc.route({ method: 'GET' }).input(Query),
10851086
}, {
10861087
commonSchemas: {
10871088
User: {
@@ -1515,5 +1516,42 @@ describe('openAPIGenerator', () => {
15151516
},
15161517
})
15171518
})
1519+
1520+
it('work with method=GET, inputStructure=compact, and without params', async () => {
1521+
expect(spec.paths!['/getWithoutParams']).toEqual({
1522+
get: {
1523+
operationId: 'getWithoutParams',
1524+
parameters: [
1525+
{
1526+
allowEmptyValue: true,
1527+
allowReserved: true,
1528+
name: 'user',
1529+
in: 'query',
1530+
explode: true,
1531+
required: true,
1532+
schema: {
1533+
$ref: '#/components/schemas/User',
1534+
},
1535+
style: 'deepObject',
1536+
},
1537+
],
1538+
responses: {
1539+
200: {
1540+
description: 'OK',
1541+
content: {
1542+
'application/json': {
1543+
schema: {
1544+
anyOf: [
1545+
{},
1546+
{ not: {} },
1547+
],
1548+
},
1549+
},
1550+
},
1551+
},
1552+
},
1553+
},
1554+
})
1555+
})
15181556
})
15191557
})

packages/openapi/src/openapi-generator.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,16 @@ export class OpenAPIGenerator {
323323
}
324324

325325
if (method === 'GET') {
326-
if (!isObjectSchema(schema)) {
326+
const resolvedSchema = resolveOpenAPIJsonSchemaRef(doc, schema)
327+
328+
if (!isObjectSchema(resolvedSchema)) {
327329
throw new OpenAPIGeneratorError(
328330
'When method is "GET", input schema must satisfy: object | any | unknown',
329331
)
330332
}
331333

332334
ref.parameters ??= []
333-
ref.parameters.push(...toOpenAPIParameters(schema, 'query'))
335+
ref.parameters.push(...toOpenAPIParameters(resolvedSchema, 'query'))
334336
}
335337
else {
336338
ref.requestBody = {

0 commit comments

Comments
 (0)