Skip to content

Commit d41ee67

Browse files
committed
fix(utils): 🐛 format typed dict attribute docs
1 parent b82bcba commit d41ee67

3 files changed

Lines changed: 56 additions & 3 deletions

File tree

src/utils/json-schema-to-typed-dict.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ export function convertToTypedDict(name: string, schema: OpenAPI.SchemaObject |
6060
fields.push(` ${key}: ${typeStr}`);
6161
}
6262
if (desc) {
63-
attrLines.push(`${key}: ${desc.replace(/\n/g, ' ')}`);
63+
const descLines = String(desc).split('\n');
64+
attrLines.push(`${key}:`);
65+
for (const dl of descLines) {
66+
attrLines.push(` ${dl}`);
67+
}
68+
attrLines.push('');
6469
}
6570
}
6671
if (fields.length === 0) {
@@ -73,6 +78,7 @@ export function convertToTypedDict(name: string, schema: OpenAPI.SchemaObject |
7378
docLines.push((flat.description as string).replace(/\n/g, ' '));
7479
}
7580
if (attrLines.length > 0) {
81+
if (attrLines[attrLines.length - 1] === '') attrLines.pop();
7682
if (flat.description) docLines.push('');
7783
docLines.push('Attributes:');
7884
for (const line of attrLines) {
@@ -172,7 +178,12 @@ export function convertToTypedDict(name: string, schema: OpenAPI.SchemaObject |
172178
fields.push(` ${key}: ${typeStr}`);
173179
}
174180
if (desc) {
175-
attrLines.push(`${key}: ${desc.replace(/\n/g, ' ')}`);
181+
const descLines = String(desc).split('\n');
182+
attrLines.push(`${key}:`);
183+
for (const dl of descLines) {
184+
attrLines.push(` ${dl}`);
185+
}
186+
attrLines.push('');
176187
}
177188
}
178189
if (fields.length === 0) {
@@ -185,6 +196,7 @@ export function convertToTypedDict(name: string, schema: OpenAPI.SchemaObject |
185196
docLines.push((flat.description as string).replace(/\n/g, ' '));
186197
}
187198
if (attrLines.length > 0) {
199+
if (attrLines[attrLines.length - 1] === '') attrLines.pop();
188200
if (flat.description) docLines.push('');
189201
docLines.push('Attributes:');
190202
for (const line of attrLines) {

tests/__snapshots__/json-schema-to-typed-dict.spec.ts.snap

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,29 @@ exports[`generate-python-dict > adds descriptions as comments 1`] = `
66
User object
77
88
Attributes:
9-
id: identifier
9+
id:
10+
identifier
1011
"""
1112
id: Required[str]"
1213
`;
1314

15+
exports[`generate-python-dict > formats multiline attribute descriptions 1`] = `
16+
"class Multi(TypedDict, total=False):
17+
"""
18+
Multi attr example
19+
20+
Attributes:
21+
id:
22+
identifier
23+
first line
24+
25+
name:
26+
the name
27+
"""
28+
id: str
29+
name: str"
30+
`;
31+
1432
exports[`generate-python-dict > generates a simple object schema 1`] = `
1533
"from typing import TypedDict, Required
1634
class User(TypedDict, total=False):

tests/json-schema-to-typed-dict.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,29 @@ describe('generate-python-dict', () => {
7474
expect(definition).toMatchSnapshot();
7575
});
7676

77+
it('formats multiline attribute descriptions', () => {
78+
const doc: OpenAPI.Document = {
79+
openapi: '3.1.0',
80+
info: { title: 't', version: '1' },
81+
paths: {},
82+
components: {
83+
schemas: {
84+
Multi: {
85+
type: 'object',
86+
description: 'Multi attr example',
87+
properties: {
88+
id: { type: 'string', description: 'identifier\nfirst line' },
89+
name: { type: 'string', description: 'the name' }
90+
}
91+
}
92+
}
93+
}
94+
};
95+
const schemas = extractSchemas(doc, null);
96+
const { definition } = convertToTypedDict('Multi', schemas.Multi as OpenAPI.SchemaObject);
97+
expect(definition).toMatchSnapshot();
98+
});
99+
77100
it('handles allOf with refs and properties', () => {
78101
const doc: OpenAPI.Document = {
79102
openapi: '3.1.0',

0 commit comments

Comments
 (0)