Skip to content

Commit 3193202

Browse files
authored
Merge pull request #819 from BitGo/DX-551-jsdocs-for-declarations
feat: allow for `JSDoc`'s in `request` declarations
2 parents 53f5e00 + 5e475fe commit 3193202

File tree

3 files changed

+122
-18
lines changed

3 files changed

+122
-18
lines changed

packages/openapi-generator/src/codec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,13 @@ export function parseCodecInitializer(
374374
if (E.isLeft(initE)) {
375375
return initE;
376376
}
377-
const [newSourceFile, init] = initE.right;
378-
return parsePlainInitializer(project, newSourceFile, init);
377+
const [newSourceFile, init, comment] = initE.right;
378+
const plainInitE = parsePlainInitializer(project, newSourceFile, init);
379+
380+
if (E.isLeft(plainInitE)) return plainInitE;
381+
if (comment !== undefined) plainInitE.right.comment = comment;
382+
383+
return plainInitE;
379384
}
380385
}
381386
const args = init.arguments.map<E.Either<string, Schema>>(({ expression }) => {

packages/openapi-generator/src/openapi.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -260,25 +260,13 @@ function routeToOpenAPI(route: Route): [string, string, OpenAPIV3.OperationObjec
260260
{},
261261
);
262262

263-
const stripTopLevelComment = (schema: Schema) => {
264-
const copy = { ...schema };
265-
266-
if (copy.comment?.description !== undefined && copy.comment?.description !== '') {
267-
copy.comment.description = '';
268-
}
269-
270-
return copy;
271-
};
272-
273-
const topLevelStripped = stripTopLevelComment(route.body!);
274-
275263
const requestBody =
276264
route.body === undefined
277265
? {}
278266
: {
279267
requestBody: {
280268
content: {
281-
'application/json': { schema: schemaToOpenAPI(topLevelStripped) },
269+
'application/json': { schema: schemaToOpenAPI(route.body) },
282270
},
283271
},
284272
};

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

Lines changed: 114 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
type Route,
1313
type Schema,
1414
} from '../src';
15+
import { SourceFile } from '../src/sourceFile';
1516

1617
async function testCase(
1718
description: string,
@@ -28,8 +29,8 @@ async function testCase(
2829
if (sourceFile === undefined) {
2930
throw new Error('Failed to parse source file');
3031
}
31-
32-
const project = new Project();
32+
const files: Record<string, SourceFile> = { './index.ts': sourceFile };
33+
const project = new Project(files);
3334
const routes: Route[] = [];
3435
const schemas: Record<string, Schema> = {};
3536
const errors: string[] = [];
@@ -3454,4 +3455,114 @@ testCase('route with reduntant response schemas', SCHEMA_WITH_REDUNDANT_UNIONS,
34543455
components: {
34553456
schemas: {}
34563457
}
3457-
});
3458+
});
3459+
3460+
const SCHEMA_WITH_TITLES_IN_REQUEST_BODIES = `
3461+
import * as t from 'io-ts';
3462+
import * as h from '@api-ts/io-ts-http';
3463+
3464+
/**
3465+
* @title Some Readable BodyFoo Title
3466+
*/
3467+
const BodyFoo = t.type({
3468+
/** a foo description */
3469+
foo: t.string,
3470+
});
3471+
3472+
/**
3473+
* @title Some Readable ParamsFoo Title
3474+
*/
3475+
const ParamsFoo = { someId: t.string };
3476+
3477+
export const route = h.httpRoute({
3478+
path: '/foo',
3479+
method: 'POST',
3480+
request: h.httpRequest({
3481+
params: {},
3482+
body: h.httpRequest({ params: ParamsFoo, body: BodyFoo, })
3483+
}),
3484+
response: {
3485+
200: t.literal('OK'),
3486+
},
3487+
});
3488+
`
3489+
3490+
testCase("route with titles in request bodies", SCHEMA_WITH_TITLES_IN_REQUEST_BODIES, {
3491+
openapi: '3.0.3',
3492+
info: {
3493+
title: 'Test',
3494+
version: '1.0.0'
3495+
},
3496+
paths: {
3497+
'/foo': {
3498+
post: {
3499+
parameters: [],
3500+
requestBody: {
3501+
content: {
3502+
'application/json': {
3503+
schema: {
3504+
type: 'object',
3505+
properties: {
3506+
params: {
3507+
type: 'object',
3508+
title: "Some Readable ParamsFoo Title",
3509+
properties: {
3510+
someId: { type: 'string' }
3511+
},
3512+
required: [ 'someId' ]
3513+
},
3514+
body: {
3515+
type: 'object',
3516+
title: 'Some Readable BodyFoo Title',
3517+
properties: {
3518+
foo: {
3519+
type: 'string',
3520+
description: 'a foo description'
3521+
}
3522+
},
3523+
required: [ 'foo' ]
3524+
}
3525+
},
3526+
required: [ 'params', 'body' ]
3527+
}
3528+
}
3529+
}
3530+
},
3531+
responses: {
3532+
'200': {
3533+
description: 'OK',
3534+
content: {
3535+
'application/json': {
3536+
schema: {
3537+
type: 'string',
3538+
enum: [ 'OK' ]
3539+
}
3540+
}
3541+
}
3542+
}
3543+
}
3544+
}
3545+
}
3546+
},
3547+
components: {
3548+
schemas: {
3549+
ParamsFoo: {
3550+
title: 'Some Readable ParamsFoo Title',
3551+
type: 'object',
3552+
properties: { someId: { type: 'string' } },
3553+
required: [ 'someId' ]
3554+
},
3555+
BodyFoo: {
3556+
title: 'Some Readable BodyFoo Title',
3557+
type: 'object',
3558+
properties: {
3559+
foo: {
3560+
type: 'string',
3561+
description: 'a foo description'
3562+
}
3563+
},
3564+
required: [ 'foo' ]
3565+
}
3566+
}
3567+
}
3568+
});

0 commit comments

Comments
 (0)