Skip to content

Commit 74055e2

Browse files
authored
Merge pull request #24 from adrianhdezm/codex/fix-dict-comments-indentation-for-description
Fix typed dict docstring indentation
2 parents 6758d9c + d41ee67 commit 74055e2

3 files changed

Lines changed: 60 additions & 7 deletions

File tree

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

Lines changed: 16 additions & 4 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,14 +78,15 @@ 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) {
7985
docLines.push(` ${line}`);
8086
}
8187
}
8288
if (docLines.length > 0) {
83-
header.push(` """\n ${docLines.join('\n')}\n """`);
89+
header.push(` """\n ${docLines.join('\n ')}\n """`);
8490
}
8591
extraDefs.push(`${header.join('\n')}\n${fields.join('\n')}`);
8692
}
@@ -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,14 +196,15 @@ 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) {
191203
docLines.push(` ${line}`);
192204
}
193205
}
194206
if (docLines.length > 0) {
195-
header.push(` """\n ${docLines.join('\n')}\n """`);
207+
header.push(` """\n ${docLines.join('\n ')}\n """`);
196208
}
197209
definition = `${header.join('\n')}\n${fields.join('\n')}`;
198210
} else {

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,31 @@ exports[`generate-python-dict > adds descriptions as comments 1`] = `
44
"class User(TypedDict, total=False):
55
"""
66
User object
7-
8-
Attributes:
9-
id: identifier
7+
8+
Attributes:
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)