Skip to content

Commit 1abe744

Browse files
committed
fix: 对象属性未生成注释
1 parent a7c9621 commit 1abe744

File tree

7 files changed

+57
-6
lines changed

7 files changed

+57
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"homepage": "https://foca.js.org",
5656
"license": "MIT",
5757
"volta": {
58-
"node": "18.19.0",
58+
"node": "20.13.0",
5959
"pnpm": "9.5.0"
6060
},
6161
"packageManager": "[email protected]",

src/lib/parse-parameters.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { OpenAPIV3 } from 'openapi-types';
22
import { refToObject } from './ref-to-object';
33
import { generateComments } from './generate-comments';
4-
import { parseSchemaType } from './parse-schema-type';
4+
import { parseSchemaType } from './parse-schema';
55

66
export const parseParameters = (
77
docs: OpenAPIV3.Document,

src/lib/parse-request-body.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { OpenAPIV3 } from 'openapi-types';
2-
import { parseSchemaType } from './parse-schema-type';
2+
import { parseSchemaType } from './parse-schema';
33
import { refToObject } from './ref-to-object';
44

55
export const parseRequestBody = (

src/lib/parse-response.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { OpenAPIV3 } from 'openapi-types';
2-
import { parseSchemaType } from './parse-schema-type';
2+
import { parseSchemaType } from './parse-schema';
33
import { refToObject } from './ref-to-object';
44

55
export const parseResponse = (

src/lib/parse-schema-type.ts renamed to src/lib/parse-schema.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { OpenAPIV3 } from 'openapi-types';
22
import { refToObject } from './ref-to-object';
3+
import { generateComments } from './generate-comments';
34

45
export const parseSchemaType = (
56
docs: OpenAPIV3.Document,
@@ -18,7 +19,8 @@ export const parseSchemaType = (
1819
case 'object':
1920
const requiredProperties = parsed.required || [];
2021
const properties = Object.entries(parsed.properties || {}).map(([key, schema]) => {
21-
return `${key}${requiredProperties.includes(key) ? '' : '?'}: ${parseSchemaType(docs, schema)}`;
22+
const schemaObj = refToObject(docs, schema);
23+
return `${generateComments(schemaObj)}${key}${requiredProperties.includes(key) ? '' : '?'}: ${parseSchemaType(docs, schemaObj)}`;
2224
});
2325
return `{ ${properties.join(';')} }`;
2426
case 'string':

test/lib/parse-parameters.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,38 @@ test('所有结构都不是必填时,optional=true', () => {
169169
}
170170
`);
171171
});
172+
173+
test('包含描述', () => {
174+
const docs = getBasicDocument({
175+
'/': {
176+
get: {
177+
parameters: [
178+
{
179+
name: 'foo',
180+
in: 'query',
181+
required: false,
182+
description: 'foo-bar',
183+
deprecated: true,
184+
schema: { type: 'string' },
185+
},
186+
],
187+
responses: {},
188+
},
189+
},
190+
});
191+
const result = parseParameters(docs, docs.paths['/']!, docs.paths['/']!.get!, 'query');
192+
expect(result).toMatchInlineSnapshot(`
193+
{
194+
"optional": true,
195+
"types": [
196+
"{
197+
/**
198+
* foo-bar
199+
* @deprecated
200+
*/
201+
foo?: string
202+
}",
203+
],
204+
}
205+
`);
206+
});

test/lib/parse-schema.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, test } from 'vitest';
2-
import { parseSchemaType } from '../../src/lib/parse-schema-type';
2+
import { parseSchemaType } from '../../src/lib/parse-schema';
33
import { getBasicDocument } from '../mocks/get-basic-document';
44

55
const docs = getBasicDocument();
@@ -40,6 +40,20 @@ test('对象', () => {
4040
expect(type).toMatchInlineSnapshot(`"{ foo?: string }"`);
4141
});
4242

43+
test('对象属性包含描述', () => {
44+
const type = parseSchemaType(docs, {
45+
type: 'object',
46+
properties: { foo: { type: 'string', description: 'foo=bar' } },
47+
});
48+
expect(type).toMatchInlineSnapshot(`
49+
"{
50+
/**
51+
* foo=bar
52+
*/
53+
foo?: string }"
54+
`);
55+
});
56+
4357
test('空对象', () => {
4458
const type = parseSchemaType(docs, { type: 'object' });
4559
expect(type).toMatchInlineSnapshot(`"{ }"`);

0 commit comments

Comments
 (0)