Skip to content

Commit

Permalink
fix: multi line descriptions for the pydantic preset in python (#1592)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethaasan authored Nov 2, 2023
1 parent 4c2eec1 commit 2c46b94
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
exports[`Should be able to render python models and should log expected output to console: class-model 1`] = `
Array [
"class Root(BaseModel):
optionalField: Optional[str] = Field(alias='this field is optional')
requiredField: str = Field(alias='this field is required')
optionalField: Optional[str] = Field(alias='''this field is optional''')
requiredField: str = Field(alias='''this field is required''')
noDescription: Optional[str] = Field()
options: Optional[Options] = Field()
",
Expand Down
2 changes: 1 addition & 1 deletion src/generators/python/presets/Pydantic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const PYTHON_PYDANTIC_CLASS_PRESET: ClassPresetType<PythonOptions> = {
? params.property.property.type
: `Optional[${params.property.property.type}]`;
const alias = params.property.property.originalInput['description']
? `alias='${params.property.property.originalInput['description']}'`
? `alias='''${params.property.property.originalInput['description']}'''`
: '';

return `${params.property.propertyName}: ${type} = Field(${alias})`;
Expand Down
34 changes: 34 additions & 0 deletions test/generators/python/presets/Pydantic.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
PythonGenerator,
PYTHON_PYDANTIC_PRESET
} from '../../../../src/generators/python';

describe('PYTHON_PYDANTIC_PRESET', () => {
let generator: PythonGenerator;

beforeEach(() => {
generator = new PythonGenerator({
presets: [PYTHON_PYDANTIC_PRESET]
});
});

test('should render pydantic for class', async () => {
const doc = {
title: 'Test',
type: 'object',
properties: {
prop: {
description: `test
multi
line
description`,
type: 'string'
}
}
};

const models = await generator.generate(doc);
expect(models).toHaveLength(1);
expect(models[0].result).toMatchSnapshot();
});
});
11 changes: 11 additions & 0 deletions test/generators/python/presets/__snapshots__/Pydantic.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`PYTHON_PYDANTIC_PRESET should render pydantic for class 1`] = `
"class Test(BaseModel):
prop: Optional[str] = Field(alias='''test
multi
line
description''')
additionalProperties: Optional[dict[Any, Any]] = Field()
"
`;

0 comments on commit 2c46b94

Please sign in to comment.