Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: multi line descriptions for the pydantic preset in python #1592

Merged
merged 2 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
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()
"
`;
Loading